-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
test-snapshot-warning.js
145 lines (131 loc) Β· 3.7 KB
/
test-snapshot-warning.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
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
'use strict';
// This tests that the warning handler is cleaned up properly
// during snapshot serialization and installed again during
// deserialization.
require('../common');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const { spawnSyncAndAssert, spawnSyncAndExitWithoutError } = require('../common/child_process');
const fs = require('fs');
const warningScript = fixtures.path('snapshot', 'warning.js');
const blobPath = tmpdir.resolve('snapshot.blob');
const empty = fixtures.path('empty.js');
tmpdir.refresh();
{
console.log('\n# Check snapshot scripts that do not emit warnings.');
spawnSyncAndExitWithoutError(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
empty,
], {
cwd: tmpdir.path
});
const stats = fs.statSync(blobPath);
assert(stats.isFile());
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
], {
cwd: tmpdir.path
}, {
stderr(output) {
const match = output.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
return true;
}
});
}
tmpdir.refresh();
{
console.log('\n# Check snapshot scripts that emit ' +
'warnings and --trace-warnings hint.');
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
warningScript,
], {
cwd: tmpdir.path
}, {
stderr(output) {
let match = output.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = output.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
return true;
}
});
const stats = fs.statSync(blobPath);
assert(stats.isFile());
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
warningScript,
], {
cwd: tmpdir.path
}, {
stderr(output) {
// Warnings should not be handled more than once.
let match = output.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = output.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
return true;
}
});
}
tmpdir.refresh();
{
console.log('\n# Check --redirect-warnings');
const warningFile1 = tmpdir.resolve('warnings.txt');
const warningFile2 = tmpdir.resolve('warnings2.txt');
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
warningFile1,
'--build-snapshot',
warningScript,
], {
cwd: tmpdir.path
}, {
stderr(output) {
assert.doesNotMatch(output, /Warning: test warning/);
}
});
const stats = fs.statSync(blobPath);
assert(stats.isFile());
const warnings1 = fs.readFileSync(warningFile1, 'utf8');
console.log(warningFile1, ':', warnings1);
let match = warnings1.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = warnings1.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
fs.rmSync(warningFile1, {
maxRetries: 3, recursive: false, force: true
});
spawnSyncAndAssert(process.execPath, [
'--snapshot-blob',
blobPath,
'--redirect-warnings',
warningFile2,
warningScript,
], {
cwd: tmpdir.path
}, {
stderr(output) {
assert.doesNotMatch(output, /Warning: test warning/);
return true;
}
});
assert(!fs.existsSync(warningFile1));
const warnings2 = fs.readFileSync(warningFile2, 'utf8');
console.log(warningFile2, ':', warnings1);
match = warnings2.match(/Warning: test warning/g);
assert.strictEqual(match.length, 1);
match = warnings2.match(/Use `node --trace-warnings/g);
assert.strictEqual(match.length, 1);
}