@effect/vitest 4.0.0-beta.10 → 4.0.0-beta.101

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.
@@ -1,7 +1,8 @@
1
1
  /**
2
- * @since 1.0.0
2
+ * @since 4.0.0
3
3
  */
4
4
 
5
+ import { getCurrentSuite } from "@vitest/runner"
5
6
  import * as Cause from "effect/Cause"
6
7
  import * as Duration from "effect/Duration"
7
8
  import * as Effect from "effect/Effect"
@@ -48,6 +49,44 @@ export const addEqualityTesters = () => {
48
49
  /** @internal */
49
50
  const testOptions = (timeout?: number | V.TestOptions) => typeof timeout === "number" ? { timeout } : timeout ?? {}
50
51
 
52
+ const hookTimeout = (timeout?: Duration.Input) =>
53
+ timeout === undefined ? undefined : Duration.toMillis(Duration.fromInputUnsafe(timeout))
54
+
55
+ const makeItProxy = <Methods extends object>(
56
+ it: V.TestAPI,
57
+ overrides: Methods
58
+ ): Methods & V.TestAPI =>
59
+ new Proxy(it as Methods & V.TestAPI, {
60
+ apply(target, thisArg, argArray) {
61
+ return Reflect.apply(target, thisArg, argArray)
62
+ },
63
+ get(target, property, receiver) {
64
+ if (property in overrides) {
65
+ return Reflect.get(overrides, property)
66
+ }
67
+ // do not bind: binding would strip vitest's static helpers (e.g. `describe.each`)
68
+ return Reflect.get(target, property, receiver)
69
+ }
70
+ })
71
+
72
+ type CollectedTask = {
73
+ readonly type: string
74
+ readonly mode?: string
75
+ readonly tasks?: ReadonlyArray<CollectedTask>
76
+ }
77
+
78
+ const collectTasks = (tasks: ReadonlyArray<CollectedTask>, acc: Array<V.TestContext["task"]> = []) => {
79
+ for (let i = 0; i < tasks.length; i++) {
80
+ const task = tasks[i]
81
+ if (task.type === "test" && task.mode !== "skip" && task.mode !== "todo") {
82
+ acc.push(task as V.TestContext["task"])
83
+ } else if (task.tasks !== undefined) {
84
+ collectTasks(task.tasks, acc)
85
+ }
86
+ }
87
+ return acc
88
+ }
89
+
51
90
  /** @internal */
52
91
  const makeTester = <R>(
53
92
  mapEffect: <A, E>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, never>,
@@ -109,10 +148,7 @@ const makeTester = <R>(
109
148
  const arbs = fc.record(
110
149
  Object.keys(arbitraries).reduce(function(result, key) {
111
150
  const arb: any = arbitraries[key]
112
- if (Schema.isSchema(arb)) {
113
- result[key] = Schema.toArbitrary(arb)
114
- }
115
- result[key] = arb
151
+ result[key] = Schema.isSchema(arb) ? Schema.toArbitrary(arb) : arb
116
152
  return result
117
153
  }, {} as Record<string, fc.Arbitrary<any>>)
118
154
  )
@@ -207,9 +243,17 @@ export const layer = <R, E>(
207
243
  Effect.cached,
208
244
  Effect.runSync
209
245
  )
246
+ let closed = false
247
+ const closeScope = (ctx?: Vitest.TestContext) => {
248
+ if (closed) {
249
+ return Promise.resolve()
250
+ }
251
+ closed = true
252
+ return runPromise(Scope.close(scope, Exit.void), ctx)
253
+ }
210
254
 
211
255
  const makeIt = (it: V.TestAPI): Vitest.Vitest.MethodsNonLive<R> =>
212
- Object.assign(it, {
256
+ makeItProxy(it, {
213
257
  effect: makeTester<R | Scope.Scope>(
214
258
  (effect) =>
215
259
  Effect.flatMap(contextEffect, (context) =>
@@ -224,30 +268,58 @@ export const layer = <R, E>(
224
268
  layer<R2, E2>(nestedLayer: Layer.Layer<R2, E2, R>, options?: {
225
269
  readonly timeout?: Duration.Input
226
270
  }) {
227
- return layer(Layer.provideMerge(nestedLayer, withTestEnv), { ...options, memoMap, excludeTestServices })
271
+ return layer(Layer.provideMerge(nestedLayer, withTestEnv), {
272
+ ...options,
273
+ memoMap: Layer.forkMemoMapUnsafe(memoMap),
274
+ excludeTestServices
275
+ })
228
276
  }
229
277
  })
230
278
 
231
279
  if (args.length === 1) {
232
- V.beforeAll(
233
- () => runPromise(Effect.asVoid(contextEffect)),
234
- options?.timeout ? Duration.toMillis(Duration.fromInputUnsafe(options.timeout)) : undefined
280
+ const currentSuite = getCurrentSuite()
281
+ const previousTasks = new Set(currentSuite.tasks)
282
+
283
+ args[0](makeIt(V.it))
284
+
285
+ const blockTasks = collectTasks(
286
+ currentSuite.tasks.filter((task) => !previousTasks.has(task)) as ReadonlyArray<CollectedTask>
235
287
  )
236
- V.afterAll(
237
- () => runPromise(Scope.close(scope, Exit.void)),
238
- options?.timeout ? Duration.toMillis(Duration.fromInputUnsafe(options.timeout)) : undefined
288
+ if (blockTasks.length === 0) {
289
+ V.afterAll(() => closeScope(), hookTimeout(options?.timeout))
290
+ return
291
+ }
292
+
293
+ const blockTaskSet = new Set(blockTasks)
294
+ let remaining = blockTasks.length
295
+
296
+ V.beforeEach(
297
+ (ctx) => {
298
+ if (!blockTaskSet.has(ctx.task)) {
299
+ return
300
+ }
301
+ ctx.onTestFinished(() => {
302
+ remaining--
303
+ if (remaining === 0) {
304
+ return closeScope(ctx)
305
+ }
306
+ })
307
+ return runPromise(Effect.asVoid(contextEffect), ctx)
308
+ },
309
+ hookTimeout(options?.timeout)
239
310
  )
240
- return args[0](makeIt(V.it))
311
+ V.afterAll(() => closeScope(), hookTimeout(options?.timeout))
312
+ return
241
313
  }
242
314
 
243
315
  return V.describe(args[0], () => {
244
316
  V.beforeAll(
245
317
  () => runPromise(Effect.asVoid(contextEffect)),
246
- options?.timeout ? Duration.toMillis(Duration.fromInputUnsafe(options.timeout)) : undefined
318
+ hookTimeout(options?.timeout)
247
319
  )
248
320
  V.afterAll(
249
- () => runPromise(Scope.close(scope, Exit.void)),
250
- options?.timeout ? Duration.toMillis(Duration.fromInputUnsafe(options.timeout)) : undefined
321
+ () => closeScope(),
322
+ hookTimeout(options?.timeout)
251
323
  )
252
324
  return args[1](makeIt(V.it))
253
325
  })
@@ -278,7 +350,7 @@ export const flakyTest = <A, E, R>(
278
350
 
279
351
  /** @internal */
280
352
  export const makeMethods = (it: V.TestAPI): Vitest.Vitest.Methods =>
281
- Object.assign(it, {
353
+ makeItProxy(it, {
282
354
  effect: makeTester<Scope.Scope>(flow(Effect.scoped, Effect.provide(TestEnv)), it),
283
355
  live: makeTester<Scope.Scope>(Effect.scoped, it),
284
356
  flakyTest,
package/src/utils.ts CHANGED
@@ -1,4 +1,12 @@
1
1
  /**
2
+ * Provides assertion helpers used by `@effect/vitest` tests.
3
+ *
4
+ * This module defines small assertion functions built on Node's `assert`,
5
+ * Vitest's instance checks, and Effect's equality support. The helpers cover
6
+ * basic equality, thrown errors, defined and undefined values, strings, regular
7
+ * expressions, class instances, `Option`, `Result`, and `Exit`. Most helpers are
8
+ * synchronous; `throwsAsync` handles rejected promises.
9
+ *
2
10
  * @since 4.0.0
3
11
  */
4
12
  import type * as Cause from "effect/Cause"
@@ -15,8 +23,9 @@ import { assert as vassert } from "vitest"
15
23
  // ----------------------------
16
24
 
17
25
  /**
18
- * Throws an `AssertionError` with the provided error message.
26
+ * Fails the current test with the provided error message.
19
27
  *
28
+ * @category testing
20
29
  * @since 4.0.0
21
30
  */
22
31
  export function fail(message: string) {
@@ -24,35 +33,39 @@ export function fail(message: string) {
24
33
  }
25
34
 
26
35
  /**
27
- * Asserts that `actual` is equal to `expected` using the `Equal.equals` trait.
36
+ * Asserts that `actual` is deeply strictly equal to `expected` using Node's `assert.deepStrictEqual`.
28
37
  *
38
+ * @category testing
29
39
  * @since 4.0.0
30
40
  */
31
41
  export function deepStrictEqual<A>(actual: A, expected: A, message?: string, ..._: Array<never>) {
32
- assert.deepStrictEqual(actual, expected, message)
42
+ assert.deepStrictEqual(actual, expected, message as string)
33
43
  }
34
44
 
35
45
  /**
36
- * Asserts that `actual` is not equal to `expected` using the `Equal.equals` trait.
46
+ * Asserts that `actual` is not deeply strictly equal to `expected` using Node's `assert.notDeepStrictEqual`.
37
47
  *
48
+ * @category testing
38
49
  * @since 4.0.0
39
50
  */
40
51
  export function notDeepStrictEqual<A>(actual: A, expected: A, message?: string, ..._: Array<never>) {
41
- assert.notDeepStrictEqual(actual, expected, message)
52
+ assert.notDeepStrictEqual(actual, expected, message as string)
42
53
  }
43
54
 
44
55
  /**
45
- * Asserts that `actual` is equal to `expected` using the `Equal.equals` trait.
56
+ * Asserts that `actual` is strictly equal to `expected` using Node's `assert.strictEqual`.
46
57
  *
58
+ * @category testing
47
59
  * @since 4.0.0
48
60
  */
49
61
  export function strictEqual<A>(actual: A, expected: A, message?: string, ..._: Array<never>) {
50
- assert.strictEqual(actual, expected, message)
62
+ assert.strictEqual(actual, expected, message as string)
51
63
  }
52
64
 
53
65
  /**
54
66
  * Asserts that `actual` is equal to `expected` using the `Equal.equals` trait.
55
67
  *
68
+ * @category testing
56
69
  * @since 4.0.0
57
70
  */
58
71
  export function assertEquals<A>(actual: A, expected: A, message?: string, ..._: Array<never>) {
@@ -65,6 +78,7 @@ export function assertEquals<A>(actual: A, expected: A, message?: string, ..._:
65
78
  /**
66
79
  * Asserts that `thunk` does not throw an error.
67
80
  *
81
+ * @category testing
68
82
  * @since 4.0.0
69
83
  */
70
84
  export function doesNotThrow(thunk: () => void, message?: string, ..._: Array<never>) {
@@ -78,6 +92,7 @@ export function doesNotThrow(thunk: () => void, message?: string, ..._: Array<ne
78
92
  /**
79
93
  * Asserts that `value` is an instance of `constructor`.
80
94
  *
95
+ * @category testing
81
96
  * @since 4.0.0
82
97
  */
83
98
  export function assertInstanceOf<C extends abstract new(...args: any) => any>(
@@ -92,6 +107,7 @@ export function assertInstanceOf<C extends abstract new(...args: any) => any>(
92
107
  /**
93
108
  * Asserts that `self` is `true`.
94
109
  *
110
+ * @category testing
95
111
  * @since 4.0.0
96
112
  */
97
113
  export function assertTrue(self: unknown, message?: string, ..._: Array<never>): asserts self {
@@ -101,6 +117,7 @@ export function assertTrue(self: unknown, message?: string, ..._: Array<never>):
101
117
  /**
102
118
  * Asserts that `self` is `false`.
103
119
  *
120
+ * @category testing
104
121
  * @since 4.0.0
105
122
  */
106
123
  export function assertFalse(self: boolean, message?: string, ..._: Array<never>) {
@@ -110,6 +127,7 @@ export function assertFalse(self: boolean, message?: string, ..._: Array<never>)
110
127
  /**
111
128
  * Asserts that `actual` includes `expected`.
112
129
  *
130
+ * @category testing
113
131
  * @since 4.0.0
114
132
  */
115
133
  export function assertInclude(actual: string | undefined, expected: string, ..._: Array<never>) {
@@ -123,6 +141,7 @@ export function assertInclude(actual: string | undefined, expected: string, ..._
123
141
  /**
124
142
  * Asserts that `actual` matches `regExp`.
125
143
  *
144
+ * @category testing
126
145
  * @since 4.0.0
127
146
  */
128
147
  export function assertMatch(actual: string, regExp: RegExp, ..._: Array<never>) {
@@ -132,8 +151,9 @@ export function assertMatch(actual: string, regExp: RegExp, ..._: Array<never>)
132
151
  }
133
152
 
134
153
  /**
135
- * Asserts that `thunk` throws an error.
154
+ * Asserts that `thunk` throws, optionally checking the thrown value against an expected `Error` or validation function.
136
155
  *
156
+ * @category testing
137
157
  * @since 4.0.0
138
158
  */
139
159
  export function throws(thunk: () => void, error?: Error | ((u: unknown) => undefined), ..._: Array<never>) {
@@ -154,8 +174,9 @@ export function throws(thunk: () => void, error?: Error | ((u: unknown) => undef
154
174
  }
155
175
 
156
176
  /**
157
- * Asserts that `thunk` throws an error.
177
+ * Asserts that `thunk` throws or returns a rejected promise, optionally checking the failure value against an expected `Error` or validation function.
158
178
  *
179
+ * @category testing
159
180
  * @since 4.0.0
160
181
  */
161
182
  export async function throwsAsync(
@@ -184,6 +205,7 @@ export async function throwsAsync(
184
205
  /**
185
206
  * Asserts that `option` is `None`.
186
207
  *
208
+ * @category testing
187
209
  * @since 4.0.0
188
210
  */
189
211
  export function assertNone<A>(option: Option.Option<A>, ..._: Array<never>): asserts option is Option.None<never> {
@@ -193,6 +215,7 @@ export function assertNone<A>(option: Option.Option<A>, ..._: Array<never>): ass
193
215
  /**
194
216
  * Asserts that `a` is not `undefined`.
195
217
  *
218
+ * @category testing
196
219
  * @since 4.0.0
197
220
  */
198
221
  export function assertDefined<A>(
@@ -207,6 +230,7 @@ export function assertDefined<A>(
207
230
  /**
208
231
  * Asserts that `a` is `undefined`.
209
232
  *
233
+ * @category testing
210
234
  * @since 4.0.0
211
235
  */
212
236
  export function assertUndefined<A>(
@@ -219,8 +243,9 @@ export function assertUndefined<A>(
219
243
  }
220
244
 
221
245
  /**
222
- * Asserts that `option` is `Some`.
246
+ * Asserts that `option` is `Some` and contains a value equal to `expected`.
223
247
  *
248
+ * @category testing
224
249
  * @since 4.0.0
225
250
  */
226
251
  export function assertSome<A>(
@@ -236,8 +261,9 @@ export function assertSome<A>(
236
261
  // ----------------------------
237
262
 
238
263
  /**
239
- * Asserts that `result` is `Success`.
264
+ * Asserts that `result` is `Success` and contains a value equal to `expected`.
240
265
  *
266
+ * @category testing
241
267
  * @since 4.0.0
242
268
  */
243
269
  export function assertSuccess<A, E>(
@@ -249,8 +275,9 @@ export function assertSuccess<A, E>(
249
275
  }
250
276
 
251
277
  /**
252
- * Asserts that `result` is `Failure`.
278
+ * Asserts that `result` is `Failure` and contains an error equal to `expected`.
253
279
  *
280
+ * @category testing
254
281
  * @since 4.0.0
255
282
  */
256
283
  export function assertFailure<A, E>(
@@ -266,8 +293,9 @@ export function assertFailure<A, E>(
266
293
  // ----------------------------
267
294
 
268
295
  /**
269
- * Asserts that `exit` is a failure.
296
+ * Asserts that `exit` is a failure with a cause equal to `expected`.
270
297
  *
298
+ * @category testing
271
299
  * @since 4.0.0
272
300
  */
273
301
  export function assertExitFailure<A, E>(
@@ -279,8 +307,9 @@ export function assertExitFailure<A, E>(
279
307
  }
280
308
 
281
309
  /**
282
- * Asserts that `exit` is a success.
310
+ * Asserts that `exit` is a success with a value equal to `expected`.
283
311
  *
312
+ * @category testing
284
313
  * @since 4.0.0
285
314
  */
286
315
  export function assertExitSuccess<A, E>(