-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathtry-snapshot.js
88 lines (74 loc) · 2.17 KB
/
try-snapshot.js
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
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {test} from 'tap';
import './helper/chalk0.js'; // eslint-disable-line import/no-unassigned-import
import ContextRef from '../lib/context-ref.js';
import * as snapshotManager from '../lib/snapshot-manager.js';
import Test from '../lib/test.js';
import {set as setOptions} from '../lib/worker/options.cjs';
setOptions({});
function setup(title, manager, fn) {
return new Test({
experiments: {},
fn,
failWithoutAssertions: true,
metadata: {type: 'test'},
contextRef: new ContextRef(),
registerUniqueTitle: () => true,
title,
compareTestSnapshot: options => manager.compare(options),
});
}
test(async t => {
// Set to `true` to update the snapshot, then run:
// npx tap -R spec test-tap/try-snapshot.js
//
// Ignore errors and make sure not to run tests with the `-b` (bail) option.
const updating = false;
const projectDir = fileURLToPath(new URL('fixture', import.meta.url));
const manager = snapshotManager.load({
file: path.join(projectDir, 'try-snapshot.cjs'),
projectDir,
fixedLocation: null,
updating,
recordNewSnapshots: updating,
});
await t.test('try-commit snapshots serially', async t => {
const ava = setup('serial', manager, async a => {
a.snapshot('hello');
const first = await a.try(t2 => {
t2.snapshot(true);
t2.snapshot({boo: 'far'});
});
first.commit();
const second = await a.try(t2 => {
t2.snapshot({foo: 'bar'});
});
second.commit();
});
const result = await ava.run();
t.ok(result.passed);
});
await t.test('try-commit snapshots concurrently', async t => {
const ava = setup('concurrent', manager, async a => {
a.snapshot('hello');
const [first, second] = await Promise.all([
a.try(t2 => {
t2.snapshot(true);
t2.snapshot({boo: 'far'});
}),
a.try(t2 => {
t2.snapshot({foo: 'bar'});
}),
]);
first.commit();
second.commit();
});
const result = await ava.run();
t.notOk(result.passed);
t.ok(result.error);
t.match(result.error.message, /not run concurrent snapshot assertions when using `t\.try\(\)`/);
t.equal(result.error.name, 'Error');
});
await manager.save();
});