-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
watchFileChanges.test.ts
185 lines (159 loc) · 4.48 KB
/
watchFileChanges.test.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import {tmpdir} from 'os';
import * as path from 'path';
import * as fs from 'graceful-fs';
import rimraf = require('rimraf');
import type {AggregatedResult} from '@jest/test-result';
import {normalize} from 'jest-config';
import HasteMap = require('jest-haste-map');
import Runtime = require('jest-runtime');
import {JestHook} from 'jest-watcher';
describe('Watch mode flows with changed files', () => {
jest.resetModules();
let watch: unknown;
let pipe: NodeJS.ReadStream;
let stdin: MockStdin;
const testDirectory = path.resolve(tmpdir(), 'jest-tmp');
const fileTargetPath = path.resolve(testDirectory, 'lost-file.js');
const fileTargetPath2 = path.resolve(
testDirectory,
'watch-test-fake.test.js',
);
const cacheDirectory = path.resolve(tmpdir(), `tmp${Math.random()}`);
let hasteMapInstance: HasteMap;
beforeEach(async () => {
watch = (await import('../watch')).default;
pipe = {write: jest.fn()} as unknown;
stdin = new MockStdin();
rimraf.sync(cacheDirectory);
rimraf.sync(testDirectory);
fs.mkdirSync(testDirectory);
fs.mkdirSync(cacheDirectory);
});
afterEach(() => {
jest.resetModules();
if (hasteMapInstance) {
hasteMapInstance.end();
}
rimraf.sync(cacheDirectory);
rimraf.sync(testDirectory);
});
it('should correct require new files without legacy cache', async () => {
fs.writeFileSync(
fileTargetPath2,
`
require('./lost-file.js');
describe('Fake test', () => {
it('Hey', () => {
});
});
`,
);
const config = normalize(
{
cache: false,
cacheDirectory,
coverageReporters: [],
maxConcurrency: 1,
maxWorkers: 1,
moduleDirectories: ['node_modules'],
onlyChanged: false,
reporters: [],
rootDir: testDirectory,
silent: true,
testRegex: ['watch-test-fake\\.test\\.js$'],
watch: false,
watchman: false,
},
{} as unknown,
).options;
hasteMapInstance = await Runtime.createHasteMap(config, {
maxWorkers: 1,
resetCache: true,
watch: true,
watchman: false,
});
const realContext = await hasteMapInstance.build().then(hasteMap => ({
config,
hasteFS: hasteMap.hasteFS,
moduleMap: hasteMap.moduleMap,
resolver: Runtime.createResolver(config, hasteMap.moduleMap),
}));
const hook = new JestHook();
const firstErrorPromise = new Promise(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
await watch(
{
...config,
watchPlugins: [],
},
[realContext],
pipe,
[hasteMapInstance],
stdin,
hook,
);
await firstErrorPromise;
const successPromise = new Promise<AggregatedResult>(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
// Create lost file
fs.writeFileSync(
fileTargetPath,
`
describe('Fake group', () => {
it('Fake 1', () => {});
it('Fake 2', () => {});
it('Fake 3', () => {});
});
`,
);
const resultSuccessReport = await successPromise;
expect(resultSuccessReport).toMatchObject({
numFailedTestSuites: 0,
numFailedTests: 0,
numPassedTests: 4,
numRuntimeErrorTestSuites: 0,
success: true,
wasInterrupted: false,
});
expect(resultSuccessReport.testResults[0]).toMatchObject({
failureMessage: null,
});
const errorPromise = new Promise<AggregatedResult>(resolve => {
hook.getSubscriber().onTestRunComplete(resolve);
});
// Remove again to ensure about no legacy cache
fs.unlinkSync(fileTargetPath);
const resultErrorReport = await errorPromise;
// After remove file we have to fail tests
expect(resultErrorReport).toMatchObject({
numFailedTestSuites: 1,
numPassedTests: 0,
numRuntimeErrorTestSuites: 1,
success: false,
wasInterrupted: false,
});
});
});
class MockStdin {
private _callbacks: Array<unknown>;
constructor() {
this._callbacks = [];
}
resume() {}
setEncoding() {}
on(_: unknown, callback: unknown) {
this._callbacks.push(callback);
}
emit(key: string) {
this._callbacks.forEach(cb => cb(key));
}
}