Skip to content

Commit 625cea5

Browse files
Use more idiomatic Vitest in the testing suite (#1245)
* Use vitest except where possible and organize tests * Use vi.fn() instead of CallTracker in 'deprecated' helper - CallTracker has been deprecated in Node.js - vi.fn() is probably more familiar to Vitest users --------- Co-authored-by: Artur Müller <[email protected]>
1 parent b64f64c commit 625cea5

File tree

7 files changed

+83
-112
lines changed

7 files changed

+83
-112
lines changed

test/api/assert.test.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
import { throws, doesNotThrow } from 'assert'
2-
import { describe, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
32
import { assert, string, StructError } from '../../src'
43

54
describe('assert', () => {
65
it('valid as helper', () => {
7-
doesNotThrow(() => {
8-
assert('valid', string())
9-
})
6+
expect(() => assert('valid', string())).not.toThrow(StructError)
107
})
118

129
it('valid as method', () => {
13-
doesNotThrow(() => {
14-
// @ts-ignore
15-
string().assert('valid')
16-
})
10+
expect(() => string().assert('valid')).not.toThrow(StructError)
1711
})
1812

1913
it('invalid as helper', () => {
20-
throws(() => {
21-
assert(42, string())
22-
}, StructError)
14+
expect(() => assert(42, string())).toThrow(StructError)
2315
})
2416

2517
it('invalid as method', () => {
26-
throws(() => {
27-
// @ts-ignore
28-
string().assert(42)
29-
}, StructError)
18+
expect(() => string().assert(42)).toThrow(StructError)
3019
})
3120

3221
it('custom error message', () => {
33-
throws(() => string().assert(42, 'Not a string!'), {
34-
cause: 'Expected a string, but received: 42',
35-
message: 'Not a string!',
36-
})
22+
expect(() => string().assert(42, 'Not a string!')).toThrow(
23+
expect.objectContaining({
24+
cause: 'Expected a string, but received: 42',
25+
message: 'Not a string!',
26+
})
27+
)
3728
})
3829
})

test/api/create.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { strictEqual, deepEqual, deepStrictEqual, throws } from 'assert'
2-
import { describe, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
32
import {
43
type,
54
optional,
@@ -13,22 +12,22 @@ import {
1312
describe('create', () => {
1413
it('missing as helper', () => {
1514
const S = defaulted(string(), 'default')
16-
strictEqual(create(undefined, S), 'default')
15+
expect(create(undefined, S)).toBe('default')
1716
})
1817

1918
it('missing as method', () => {
2019
const S = defaulted(string(), 'default')
21-
strictEqual(S.create(undefined), 'default')
20+
expect(S.create(undefined)).toBe('default')
2221
})
2322

2423
it('not missing as helper', () => {
2524
const S = defaulted(string(), 'default')
26-
strictEqual(create('string', S), 'string')
25+
expect(create('string', S)).toBe('string')
2726
})
2827

2928
it('not missing as method', () => {
3029
const S = defaulted(string(), 'default')
31-
strictEqual(S.create('string'), 'string')
30+
expect(S.create('string')).toBe('string')
3231
})
3332

3433
it('missing optional fields remain missing', () => {
@@ -37,7 +36,7 @@ describe('create', () => {
3736
b: optional(string()),
3837
c: optional(type({ d: string() })),
3938
})
40-
deepEqual(S.create({ a: 'a' }), { a: 'a' })
39+
expect(S.create({ a: 'a' })).toStrictEqual({ a: 'a' })
4140
})
4241

4342
it('explicit undefined values are kept', () => {
@@ -46,17 +45,19 @@ describe('create', () => {
4645
b: coerce(optional(string()), literal(null), () => undefined),
4746
c: optional(type({ d: string() })),
4847
})
49-
deepStrictEqual(S.create({ a: 'a', b: null, c: undefined }), {
48+
expect(S.create({ a: 'a', b: null, c: undefined })).toStrictEqual({
5049
a: 'a',
5150
b: undefined,
5251
c: undefined,
5352
})
5453
})
5554

5655
it('custom error message', () => {
57-
throws(() => string().create(42, 'Not a string!'), {
58-
cause: 'Expected a string, but received: 42',
59-
message: 'Not a string!',
60-
})
56+
expect(() => string().create(42, 'Not a string!')).toThrow(
57+
expect.objectContaining({
58+
cause: 'Expected a string, but received: 42',
59+
message: 'Not a string!',
60+
})
61+
)
6162
})
6263
})

test/api/is.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import { strictEqual } from 'assert'
2-
import { describe, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
32
import { is, string } from '../../src'
43

54
describe('is', () => {
65
it('valid as helper', () => {
7-
strictEqual(is('valid', string()), true)
6+
expect(is('valid', string())).toBe(true)
87
})
98

109
it('valid as method', () => {
11-
strictEqual(string().is('valid'), true)
10+
expect(string().is('valid')).toBe(true)
1211
})
1312

1413
it('invalid as helper', () => {
15-
strictEqual(is(42, string()), false)
14+
expect(is(42, string())).toBe(false)
1615
})
1716

1817
it('invalid as method', () => {
19-
strictEqual(string().is(42), false)
18+
expect(string().is(42)).toBe(false)
2019
})
2120
})

test/api/mask.test.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { deepStrictEqual, throws } from 'assert'
2-
import { describe, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
32
import {
43
mask,
54
object,
@@ -14,21 +13,19 @@ describe('mask', () => {
1413
it('object as helper', () => {
1514
const S = object({ id: string() })
1615
const value = { id: '1', unknown: true }
17-
deepStrictEqual(mask(value, S), { id: '1' })
16+
expect(mask(value, S)).toStrictEqual({ id: '1' })
1817
})
1918

2019
it('non-object as helper', () => {
2120
const S = object({ id: string() })
2221
const value = 'invalid'
23-
throws(() => {
24-
mask(value, S)
25-
}, StructError)
22+
expect(() => mask(value, S)).toThrow(StructError)
2623
})
2724

2825
it('coercing', () => {
2926
const S = defaulted(object({ id: string() }), { id: '0' })
3027
const value = { unknown: true }
31-
deepStrictEqual(mask(value, S), { id: '0' })
28+
expect(mask(value, S)).toStrictEqual({ id: '0' })
3229
})
3330

3431
it('deep masking of objects', () => {
@@ -41,7 +38,7 @@ describe('mask', () => {
4138
unknown: true,
4239
sub: [{ prop: '2', unknown: true }],
4340
}
44-
deepStrictEqual(mask(value, S), { id: '1', sub: [{ prop: '2' }] })
41+
expect(mask(value, S)).toStrictEqual({ id: '1', sub: [{ prop: '2' }] })
4542
})
4643

4744
it('masking of a nested type', () => {
@@ -54,7 +51,7 @@ describe('mask', () => {
5451
unknown: true,
5552
sub: [{ prop: '2', unknown: true }],
5653
}
57-
deepStrictEqual(mask(value, S), {
54+
expect(mask(value, S)).toStrictEqual({
5855
id: '1',
5956
sub: [{ prop: '2', unknown: true }],
6057
})
@@ -70,7 +67,7 @@ describe('mask', () => {
7067
unknown: true,
7168
sub: [{ prop: '2', unknown: true }],
7269
}
73-
deepStrictEqual(mask(value, S), {
70+
expect(mask(value, S)).toStrictEqual({
7471
id: '1',
7572
unknown: true,
7673
sub: [{ prop: '2' }],
@@ -80,14 +77,16 @@ describe('mask', () => {
8077
it('masking does not change the original value', () => {
8178
const S = object({ id: string() })
8279
const value = { id: '1', unknown: true }
83-
deepStrictEqual(mask(value, S), { id: '1' })
84-
deepStrictEqual(value, { id: '1', unknown: true })
80+
expect(mask(value, S)).toStrictEqual({ id: '1' })
81+
expect(value).toStrictEqual({ id: '1', unknown: true })
8582
})
8683

8784
it('custom error message', () => {
88-
throws(() => string().mask(42, 'Not a string!'), {
89-
cause: 'Expected a string, but received: 42',
90-
message: 'Not a string!',
91-
})
85+
expect(() => string().mask(42, 'Not a string!')).toThrow(
86+
expect.objectContaining({
87+
cause: 'Expected a string, but received: 42',
88+
message: 'Not a string!',
89+
})
90+
)
9291
})
9392
})

test/api/validate.test.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { deepStrictEqual, strictEqual } from 'assert'
2-
import { describe, it } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
32
import {
43
validate,
54
string,
@@ -13,20 +12,20 @@ import {
1312
describe('validate', () => {
1413
it('valid as helper', () => {
1514
const S = string()
16-
deepStrictEqual(validate('valid', S), [undefined, 'valid'])
15+
expect(validate('valid', S)).toStrictEqual([undefined, 'valid'])
1716
})
1817

1918
it('valid as method', () => {
2019
const S = string()
21-
deepStrictEqual(S.validate('valid'), [undefined, 'valid'])
20+
expect(S.validate('valid')).toStrictEqual([undefined, 'valid'])
2221
})
2322

2423
it('invalid as helper', () => {
2524
const S = string()
2625
const [err, value] = validate(42, S)
27-
strictEqual(value, undefined)
28-
strictEqual(err instanceof StructError, true)
29-
deepStrictEqual(Array.from((err as StructError).failures()), [
26+
expect(value).toStrictEqual(undefined)
27+
expect(err).toBeInstanceOf(StructError)
28+
expect(Array.from((err as StructError).failures())).toStrictEqual([
3029
{
3130
value: 42,
3231
key: undefined,
@@ -43,9 +42,9 @@ describe('validate', () => {
4342
it('invalid as method', () => {
4443
const S = string()
4544
const [err, value] = S.validate(42)
46-
strictEqual(value, undefined)
47-
strictEqual(err instanceof StructError, true)
48-
deepStrictEqual(Array.from((err as StructError).failures()), [
45+
expect(value).toStrictEqual(undefined)
46+
expect(err).toBeInstanceOf(StructError)
47+
expect(Array.from((err as StructError).failures())).toStrictEqual([
4948
{
5049
value: 42,
5150
key: undefined,
@@ -62,17 +61,16 @@ describe('validate', () => {
6261
it('error message path', () => {
6362
const S = object({ author: object({ name: string() }) })
6463
const [err] = S.validate({ author: { name: 42 } })
65-
strictEqual(
66-
(err as StructError).message,
64+
expect(err?.message).toBe(
6765
'At path: author.name -- Expected a string, but received: 42'
6866
)
6967
})
7068

7169
it('custom error message', () => {
7270
const S = string()
7371
const [err] = S.validate(42, { message: 'Validation failed!' })
74-
strictEqual(err?.message, 'Validation failed!')
75-
strictEqual(err?.cause, 'Expected a string, but received: 42')
72+
expect(err?.message).toBe('Validation failed!')
73+
expect(err?.cause).toBe('Expected a string, but received: 42')
7674
})
7775

7876
it('early exit', () => {
@@ -91,8 +89,8 @@ describe('validate', () => {
9189

9290
const S = object({ a: A, b: B })
9391
S.validate({ a: null, b: null })
94-
strictEqual(ranA, true)
95-
strictEqual(ranB, false)
92+
expect(ranA).toBe(true)
93+
expect(ranB).toBe(false)
9694
})
9795

9896
it('refiners after children', () => {
@@ -109,7 +107,7 @@ describe('validate', () => {
109107
})
110108

111109
B.validate({ a: null })
112-
deepStrictEqual(order, ['validator', 'refiner'])
110+
expect(order).toStrictEqual(['validator', 'refiner'])
113111
})
114112

115113
it('refiners even if nested refiners fail', () => {
@@ -127,7 +125,7 @@ describe('validate', () => {
127125
const [error] = B.validate({ a: null })
128126
// Collect all failures. Ensures all validation runs.
129127
error?.failures()
130-
strictEqual(ranOuterRefiner, true)
128+
expect(ranOuterRefiner).toBe(true)
131129
})
132130

133131
it('skips refiners if validators return errors', () => {
@@ -145,6 +143,6 @@ describe('validate', () => {
145143
const [error] = B.validate({ a: null })
146144
// Collect all failures. Ensures all validation runs.
147145
error?.failures()
148-
strictEqual(ranRefiner, false)
146+
expect(ranRefiner).toBe(false)
149147
})
150148
})

test/deprecated.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { any, assert, deprecated } from '../src'
3+
4+
describe('deprecated', () => {
5+
it('does not log deprecated type if value is undefined', () => {
6+
const spy = vi.fn()
7+
expect(spy).not.toHaveBeenCalled()
8+
assert(undefined, deprecated(any(), spy))
9+
expect(spy).not.toHaveBeenCalled()
10+
})
11+
12+
it('logs deprecated type to passed function if value is present', () => {
13+
const spy = vi.fn()
14+
expect(spy).not.toHaveBeenCalled()
15+
assert('present', deprecated(any(), spy))
16+
expect(spy).toHaveBeenCalledOnce()
17+
})
18+
})

0 commit comments

Comments
 (0)