-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
try-snapshot.js
85 lines (73 loc) 路 2.09 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
'use strict';
require('../lib/chalk').set();
require('../lib/worker/options').set({chalkOptions: {level: 0}});
const path = require('path');
const {test} = require('tap');
const snapshotManager = require('../lib/snapshot-manager');
const Test = require('../lib/test');
const ContextRef = require('../lib/context-ref');
function setup(title, manager, fn) {
return new Test({
experiments: {},
fn,
failWithoutAssertions: true,
metadata: {type: 'test', callback: false},
contextRef: new ContextRef(),
registerUniqueTitle: () => true,
title,
compareTestSnapshot: options => manager.compare(options)
});
}
test(async t => {
// Set to `true` to update the snapshot, then run:
// "$(npm bin)"/tap --no-cov -R spec test/try-snapshot.js
//
// Ignore errors and make sure not to run tests with the `-b` (bail) option.
const updating = false;
const projectDir = path.join(__dirname, 'fixture');
const manager = snapshotManager.load({
file: path.join(projectDir, 'try-snapshot.js'),
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.true(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.false(result.passed);
t.ok(result.error);
t.match(result.error.message, /not run concurrent snapshot assertions when using `t\.try\(\)`/);
t.is(result.error.name, 'Error');
});
manager.save();
});