/
jest-fast-check.spec.ts
336 lines (279 loc) · 11.9 KB
/
jest-fast-check.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import * as path from 'path';
import { promises as fs } from 'fs';
import { promisify } from 'util';
import { execFile as _execFile } from 'child_process';
const execFile = promisify(_execFile);
import _fc from 'fast-check';
import { testProp as _testProp, itProp as _itProp } from '@fast-check/jest';
declare const fc: typeof _fc;
declare const runnerProp: typeof _testProp | typeof _itProp;
const generatedTestsDirectoryName = 'generated-tests';
const generatedTestsDirectory = path.join(__dirname, generatedTestsDirectoryName);
type RunnerType = 'testProp' | 'itProp';
jest.setTimeout(60_000);
beforeAll(async () => {
await fs.mkdir(generatedTestsDirectory, { recursive: true });
});
afterAll(async () => {
await fs.rm(generatedTestsDirectory, { recursive: true });
});
describe.each<{ runner: RunnerType }>([{ runner: 'testProp' }, { runner: 'itProp' }])('$runner', ({ runner }) => {
it.concurrent('should pass on truthy synchronous property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property pass sync', [fc.string(), fc.string(), fc.string()], (a, b, c) => {
return `${a}${b}${c}`.includes(b);
});
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectPass(out, specFileName);
expect(out).toMatch(/[√✓] property pass sync \(with seed=-?\d+\)/);
});
it.concurrent('should pass on truthy asynchronous property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property pass async', [fc.string(), fc.string(), fc.string()], async (a, b, c) => {
await new Promise((resolve) => setTimeout(resolve, 0));
return `${a}${b}${c}`.includes(b);
});
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectPass(out, specFileName);
expect(out).toMatch(/[√✓] property pass async \(with seed=-?\d+\)/);
});
it.concurrent('should fail on falsy synchronous property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property fail sync', [fc.nat()], (a) => {
return a === 0;
});
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out);
expect(out).toMatch(/[×✕] property fail sync \(with seed=-?\d+\)/);
});
it.concurrent('should fail on falsy asynchronous property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property fail async', [fc.nat()], async (a) => {
await new Promise((resolve) => setTimeout(resolve, 0));
return a === 0;
});
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out);
expect(out).toMatch(/[×✕] property fail async \(with seed=-?\d+\)/);
});
it.concurrent('should fail with locally requested seed', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property fail with locally requested seed', [fc.constant(null)], (_unused) => false, {
seed: 4242,
});
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out, { noAlignWithJest: true });
expect(out).toMatch(/[×✕] property fail with locally requested seed \(with seed=4242\)/);
});
it.concurrent('should fail with globally requested seed', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
fc.configureGlobal({ seed: 4848 });
runnerProp('property fail with globally requested seed', [fc.constant(null)], (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out, { noAlignWithJest: true });
expect(out).toMatch(/[×✕] property fail with globally requested seed \(with seed=4848\)/);
});
it.concurrent('should fail with seed requested at jest level', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp('property fail with globally requested seed', [fc.constant(null)], (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath, { jestSeed: 6969 });
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out);
expect(out).toMatch(/[×✕] property fail with globally requested seed \(with seed=6969\)/);
});
describe('.skip', () => {
it.concurrent('should never be executed', async () => {
// Arrange
const { jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.skip('property never executed', [fc.constant(null)], (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expect(out).toMatch(/Test Suites:\s+1 skipped, 0 of 1 total/);
expect(out).toMatch(/Tests:\s+1 skipped, 1 total/);
});
});
describe('.failing', () => {
it.concurrent('should pass because failing', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.failing('property pass because failing', [fc.constant(null)], async (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectPass(out, specFileName);
expect(out).toMatch(/[√✓] property pass because failing \(with seed=-?\d+\)/);
});
it.concurrent('should fail because passing', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.failing('property fail because passing', [fc.constant(null)], async (_unused) => true);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expect(out).toMatch(/[×✕] property fail because passing \(with seed=-?\d+\)/);
});
});
describe('.concurrent', () => {
it.concurrent('should pass on truthy property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.concurrent('property pass on truthy property', [fc.constant(null)], (_unused) => true);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectPass(out, specFileName);
expect(out).toMatch(/[√✓] property pass on truthy property \(with seed=-?\d+\)/);
});
it.concurrent('should fail on falsy property', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.concurrent('property fail on falsy property', [fc.constant(null)], (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expectAlignedSeeds(out);
expect(out).toMatch(/[×✕] property fail on falsy property \(with seed=-?\d+\)/);
});
describe('.failing', () => {
it.concurrent('should pass because failing', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.concurrent.failing('property pass because failing', [fc.constant(null)], async (_unused) => false);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectPass(out, specFileName);
expect(out).toMatch(/[√✓] property pass because failing \(with seed=-?\d+\)/);
});
it.concurrent('should fail because passing', async () => {
// Arrange
const { specFileName, jestConfigRelativePath } = await writeToFile(runner, () => {
runnerProp.concurrent.failing('property fail because passing', [fc.constant(null)], async (_unused) => true);
});
// Act
const out = await runSpec(jestConfigRelativePath);
// Assert
expectFail(out, specFileName);
expect(out).toMatch(/[×✕] property fail because passing \(with seed=-?\d+\)/);
});
});
});
});
// Helper
let num = -1;
async function writeToFile(
runner: 'testProp' | 'itProp',
fileContent: () => void
): Promise<{ specFileName: string; jestConfigRelativePath: string }> {
const specFileSeed = Math.random().toString(16).substring(2);
// Prepare test file itself
const specFileName = `generated-${specFileSeed}-${++num}.spec.js`;
const specFilePath = path.join(generatedTestsDirectory, specFileName);
const fileContentString = String(fileContent);
const wrapInDescribeIfNeeded =
runner === 'itProp'
? (testCode: string) => `describe('test suite', () => {\n${testCode}\n});`
: (testCode: string) => testCode;
const specContent =
"const fc = require('fast-check');\n" +
`const {${runner}: runnerProp} = require('@fast-check/jest');\n` +
wrapInDescribeIfNeeded(
fileContentString.substring(fileContentString.indexOf('{') + 1, fileContentString.lastIndexOf('}'))
);
// Prepare jest config itself
const jestConfigName = `jest.config-${specFileSeed}.js`;
const jestConfigRelativePath = `test/${generatedTestsDirectoryName}/${jestConfigName}`;
const jestConfigPath = path.join(generatedTestsDirectory, jestConfigName);
// Write the files
await Promise.all([
fs.writeFile(specFilePath, specContent),
fs.writeFile(jestConfigPath, `module.exports = { testMatch: ['<rootDir>/${specFileName}'], transform: {} };`),
]);
return { specFileName, jestConfigRelativePath };
}
async function runSpec(jestConfigRelativePath: string, opts: { jestSeed?: number } = {}): Promise<string> {
const { stdout: jestBinaryPathCommand } = await execFile('yarn', ['bin', 'jest'], { shell: true });
const jestBinaryPath = jestBinaryPathCommand.split('\n')[0];
try {
const { stderr: specOutput } = await execFile('node', [
jestBinaryPath,
'--config',
jestConfigRelativePath,
'--show-seed',
...(opts.jestSeed !== undefined ? ['--seed', String(opts.jestSeed)] : []),
]);
return specOutput;
} catch (err) {
return (err as any).stderr;
}
}
function expectPass(out: string, specFileName: string): void {
expect(out).toMatch(new RegExp('PASS .*/' + specFileName));
}
function expectFail(out: string, specFileName: string): void {
expect(out).toMatch(new RegExp('FAIL .*/' + specFileName));
}
function expectAlignedSeeds(out: string, opts: { noAlignWithJest?: boolean } = {}): void {
// Seed printed by jest has the shape:
// > Seed: -518086725
// > Test Suites: 1 failed, 1 total
// > Tests: 1 failed, 1 total
// > Snapshots: 0 total
// > Time: 0.952 s
// > Ran all test suites
const JestSeedMatcher = /Seed:\s+(-?\d+)/;
expect(out).toMatch(JestSeedMatcher);
const jestSeed = JestSeedMatcher.exec(out)![1];
// Seed printed by jest-fast-check next to test name has the shape:
// > × property fail on falsy property (with seed=-518086725)
const JestFastCheckSeedMatcher = opts.noAlignWithJest
? /[×✕] .* \(with seed=(-?\d+)\)/
: new RegExp('[×✕] .* \\(with seed=(' + jestSeed + ')\\)');
expect(out).toMatch(JestFastCheckSeedMatcher);
const jestFastCheckSeed = JestFastCheckSeedMatcher.exec(out)![1];
// Seed printed by fast-check in case of failure has the shape:
// > Property failed after 1 tests
// > { seed: -518086725, path: \"0\", endOnFailure: true }
expect(out).toMatch(new RegExp('\\{[^}]*seed\\s*:\\s*' + jestFastCheckSeed + '[^\\d]'));
}