|
1 |
| -import assert, { CallTracker } from 'assert' |
2 |
| -import fs from 'fs' |
3 |
| -import { pick } from 'lodash' |
4 |
| -import { basename, extname, resolve } from 'path' |
5 |
| -import { |
6 |
| - any, |
7 |
| - assert as assertValue, |
8 |
| - Context, |
9 |
| - create as createValue, |
10 |
| - deprecated, |
11 |
| - StructError, |
12 |
| -} from '../src' |
13 |
| - |
14 |
| -describe('superstruct', () => { |
15 |
| - describe('api', () => { |
16 |
| - require('./api/assert') |
17 |
| - require('./api/create') |
18 |
| - require('./api/is') |
19 |
| - require('./api/mask') |
20 |
| - require('./api/validate') |
21 |
| - }) |
22 |
| - |
23 |
| - describe('validation', () => { |
24 |
| - const kindsDir = resolve(__dirname, 'validation') |
25 |
| - const kinds = fs |
26 |
| - .readdirSync(kindsDir) |
27 |
| - .filter((t) => t[0] !== '.') |
28 |
| - .map((t) => basename(t, extname(t))) |
29 |
| - |
30 |
| - for (const kind of kinds) { |
31 |
| - describe(kind, () => { |
32 |
| - const testsDir = resolve(kindsDir, kind) |
33 |
| - const tests = fs |
34 |
| - .readdirSync(testsDir) |
35 |
| - .filter((t) => t[0] !== '.') |
36 |
| - .map((t) => basename(t, extname(t))) |
37 |
| - |
38 |
| - for (const name of tests) { |
39 |
| - const module = require(resolve(testsDir, name)) |
40 |
| - const { Struct, data, create, only, skip, output, failures } = module |
41 |
| - const run = only ? it.only : skip ? it.skip : it |
42 |
| - run(name, () => { |
43 |
| - let actual |
44 |
| - let err |
45 |
| - |
46 |
| - try { |
47 |
| - if (create) { |
48 |
| - actual = createValue(data, Struct) |
49 |
| - } else { |
50 |
| - assertValue(data, Struct) |
51 |
| - actual = data |
52 |
| - } |
53 |
| - } catch (e) { |
54 |
| - if (!(e instanceof StructError)) { |
55 |
| - throw e |
56 |
| - } |
57 |
| - |
58 |
| - err = e |
59 |
| - } |
60 |
| - |
61 |
| - if ('output' in module) { |
62 |
| - if (err) { |
63 |
| - throw new Error( |
64 |
| - `Expected "${name}" fixture not to throw an error but it did:\n\n${err}` |
65 |
| - ) |
66 |
| - } |
67 |
| - |
68 |
| - assert.deepStrictEqual(actual, output) |
69 |
| - } else if ('failures' in module) { |
70 |
| - if (!err) { |
71 |
| - throw new Error( |
72 |
| - `Expected "${name}" fixture to throw an error but it did not.` |
73 |
| - ) |
74 |
| - } |
75 |
| - |
76 |
| - const props = ['type', 'path', 'refinement', 'value', 'branch'] |
77 |
| - const actualFailures = err |
78 |
| - .failures() |
79 |
| - .map((failure) => pick(failure, ...props)) |
80 |
| - |
81 |
| - assert.deepStrictEqual(actualFailures, failures) |
82 |
| - assert.deepStrictEqual(pick(err, ...props), failures[0]) |
83 |
| - } else { |
84 |
| - throw new Error( |
85 |
| - `The "${name}" fixture did not define an \`output\` or \`failures\` export.` |
86 |
| - ) |
87 |
| - } |
88 |
| - }) |
89 |
| - } |
90 |
| - }) |
91 |
| - } |
92 |
| - }) |
93 |
| - |
94 |
| - describe('deprecated', () => { |
95 |
| - it('does not log deprecated type if value is undefined', () => { |
96 |
| - const tracker = new CallTracker() |
97 |
| - const logSpy = buildSpyWithZeroCalls(tracker) |
98 |
| - assertValue(undefined, deprecated(any(), logSpy)) |
99 |
| - tracker.verify() |
100 |
| - }) |
101 |
| - |
102 |
| - it('logs deprecated type to passed function if value is present', () => { |
103 |
| - const tracker = new CallTracker() |
104 |
| - const fakeLog = (value: unknown, ctx: Context) => {} |
105 |
| - const logSpy = tracker.calls(fakeLog, 1) |
106 |
| - assertValue('present', deprecated(any(), logSpy)) |
107 |
| - tracker.verify() |
108 |
| - }) |
109 |
| - }) |
110 |
| -}) |
111 |
| - |
112 | 1 | /**
|
113 | 2 | * A helper for testing type signatures.
|
114 | 3 | */
|
115 | 4 |
|
116 | 5 | export function test<T>(fn: (x: unknown) => T) {}
|
117 |
| - |
118 |
| -/** |
119 |
| - * This emulates `tracker.calls(0)`. |
120 |
| - * |
121 |
| - * `CallTracker.calls` doesn't support passing `0`, therefore we expect it |
122 |
| - * to be called once which is our call in this test. This proves that |
123 |
| - * the following action didn't call it. |
124 |
| - */ |
125 |
| -function buildSpyWithZeroCalls(tracker: CallTracker) { |
126 |
| - const logSpy = tracker.calls(1) |
127 |
| - logSpy() |
128 |
| - return logSpy |
129 |
| -} |
0 commit comments