test-fns 1.7.0 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/usePrep.d.ts +2 -2
- package/dist/usePrep.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +98 -0
package/dist/usePrep.d.ts
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
export declare const usePrep: <T extends Record<string, any>>(setup: () => Promise<T>, options?: {
|
|
9
9
|
mode: 'beforeAll' | 'beforeEach';
|
|
10
10
|
}) => T;
|
|
11
|
-
export declare const useBeforeAll: <T extends Record<string, any>>(setup: Parameters<typeof usePrep<T>>[0]) => ReturnType<typeof usePrep
|
|
12
|
-
export declare const useBeforeEach: <T extends Record<string, any>>(setup: Parameters<typeof usePrep<T>>[0]) => ReturnType<typeof usePrep
|
|
11
|
+
export declare const useBeforeAll: <T extends Record<string, any>>(setup: Parameters<typeof usePrep<T>>[0]) => ReturnType<typeof usePrep<T>>;
|
|
12
|
+
export declare const useBeforeEach: <T extends Record<string, any>>(setup: Parameters<typeof usePrep<T>>[0]) => ReturnType<typeof usePrep<T>>;
|
package/dist/usePrep.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrep.js","sourceRoot":"","sources":["../src/usePrep.ts"],"names":[],"mappings":";;;AAAA,mDAAyD;AAEzD;;;;;;GAMG;AACI,MAAM,OAAO,GAAG,CACrB,KAAuB,EACvB,UAEI,EAAE,IAAI,EAAE,WAAW,EAAE,EACtB,EAAE;IACL,wEAAwE;IACxE,MAAM,MAAM,GAAe,EAAE,CAAC,CAAC,8BAA8B;IAC7D,IAAI,OAAsB,CAAC,CAAC,uCAAuC;IAEnE,qDAAqD;IACrD,MAAM,YAAY,GAAsB;QACtC,GAAG,CAAC,CAAC,EAAE,IAAI;YACT,IAAI,OAAO,KAAK,SAAS;gBACvB,MAAM,IAAI,wCAAuB,CAC/B,uDAAuD,CACxD,CAAC;YACJ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": "test-fns",
|
|
3
3
|
"author": "ehmpathy",
|
|
4
4
|
"description": "write usecase driven tests systematically for simpler, safer, and more readable code",
|
|
5
|
-
"version": "1.7.
|
|
5
|
+
"version": "1.7.2",
|
|
6
6
|
"repository": "ehmpathy/test-fns",
|
|
7
7
|
"homepage": "https://github.com/ehmpathy/test-fns",
|
|
8
8
|
"keywords": [
|
package/readme.md
CHANGED
|
@@ -72,6 +72,10 @@ describe('your test', () => {
|
|
|
72
72
|
|
|
73
73
|
prepare test scenarios within a .given/.when block asynchronously, without any `let`s or `beforeAll`s
|
|
74
74
|
|
|
75
|
+
`usePrep` accepts a `mode` option to control when setup runs:
|
|
76
|
+
- `mode: 'beforeAll'` - runs setup once for all tests (default)
|
|
77
|
+
- `mode: 'beforeEach'` - runs setup fresh before each test
|
|
78
|
+
|
|
75
79
|
```ts
|
|
76
80
|
given('an overdue invoice', () => {
|
|
77
81
|
const invoice = usePrep(async () => {
|
|
@@ -85,3 +89,97 @@ given('an overdue invoice', () => {
|
|
|
85
89
|
})
|
|
86
90
|
})
|
|
87
91
|
```
|
|
92
|
+
|
|
93
|
+
**useBeforeAll and useBeforeEach are convenience wrappers** around `usePrep`:
|
|
94
|
+
- `useBeforeAll(setup)` is equivalent to `usePrep(setup, { mode: 'beforeAll' })`
|
|
95
|
+
- `useBeforeEach(setup)` is equivalent to `usePrep(setup, { mode: 'beforeEach' })`
|
|
96
|
+
|
|
97
|
+
Use the named functions for clarity about when setup runs.
|
|
98
|
+
|
|
99
|
+
### useBeforeAll
|
|
100
|
+
|
|
101
|
+
prepare test resources once for all tests in a suite, optimizing setup time for expensive operations
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
describe('spaceship refueling system', () => {
|
|
105
|
+
given('a spaceship that needs to refuel', () => {
|
|
106
|
+
const spaceship = useBeforeAll(async () => {
|
|
107
|
+
// This runs once before all tests in this suite
|
|
108
|
+
const ship = await prepareExampleSpaceship();
|
|
109
|
+
await ship.dock();
|
|
110
|
+
return ship;
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
when('no changes are made', () => {
|
|
114
|
+
then('it should be docked', async () => {
|
|
115
|
+
expect(spaceship.isDocked).toEqual(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
then('it should need fuel', async () => {
|
|
119
|
+
expect(spaceship.fuelLevel).toBeLessThan(spaceship.fuelCapacity);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
when('it connects to the fuel station', () => {
|
|
124
|
+
const result = useBeforeAll(async () => await spaceship.connectToFuelStation());
|
|
125
|
+
|
|
126
|
+
then('it should be connected', async () => {
|
|
127
|
+
expect(result.connected).toEqual(true);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
then('it should calculate required fuel', async () => {
|
|
131
|
+
expect(result.fuelNeeded).toBeGreaterThan(0);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### useBeforeEach
|
|
139
|
+
|
|
140
|
+
prepare fresh test resources before each test, ensuring test isolation
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
describe('spaceship combat system', () => {
|
|
144
|
+
given('a spaceship in battle', () => {
|
|
145
|
+
// This runs before each test, ensuring a fresh spaceship
|
|
146
|
+
const spaceship = useBeforeEach(async () => {
|
|
147
|
+
const ship = await prepareExampleSpaceship();
|
|
148
|
+
await ship.resetShields();
|
|
149
|
+
return ship;
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
when('no changes are made', () => {
|
|
153
|
+
then('it should have full shields', async () => {
|
|
154
|
+
expect(spaceship.shields).toEqual(100);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
then('it should be ready for combat', async () => {
|
|
158
|
+
expect(spaceship.status).toEqual('READY');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
when('it takes damage', () => {
|
|
163
|
+
const result = useBeforeEach(async () => await spaceship.takeDamage(25));
|
|
164
|
+
|
|
165
|
+
then('it should reduce shield strength', async () => {
|
|
166
|
+
expect(spaceship.shields).toEqual(75);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
then('it should return damage report', async () => {
|
|
170
|
+
expect(result.damageReceived).toEqual(25);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**When to use each:**
|
|
178
|
+
- `useBeforeAll`: Use when setup is expensive (database connections, API calls) and tests don't modify the resource
|
|
179
|
+
- `useBeforeEach`: Use when tests modify the resource and need isolation between runs
|
|
180
|
+
- `usePrep`: The base function that powers both - use when you want explicit control over the mode or need to dynamically choose between `beforeAll` and `beforeEach`
|
|
181
|
+
|
|
182
|
+
**Key differences:**
|
|
183
|
+
- All three functions (`usePrep`, `useBeforeAll`, `useBeforeEach`) create a proxy that defers setup until the test framework's lifecycle hooks run
|
|
184
|
+
- `useBeforeAll` and `useBeforeEach` are just clearer, more readable shortcuts for `usePrep` with a specific mode
|
|
185
|
+
- Choose based on readability: use `useBeforeAll`/`useBeforeEach` for explicit intent, or `usePrep` when mode needs to be configurable
|