forked from jestjs/jest
/
watchModeNoAccess.test.ts
79 lines (63 loc) 路 2.12 KB
/
watchModeNoAccess.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
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow
*/
'use strict';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'graceful-fs';
import {cleanup, writeFiles} from '../Utils';
import {runContinuous} from '../runJest';
const DIR = path.resolve(os.tmpdir(), 'watch_mode_no_access');
const sleep = (time: number) =>
new Promise(resolve => setTimeout(resolve, time));
beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));
const setupFiles = () => {
writeFiles(DIR, {
'__tests__/foo.test.js': `
const foo = require('../foo');
test('foo', () => { expect(typeof foo).toBe('number'); });
`,
'foo.js': `
module.exports = 0;
`,
'package.json': JSON.stringify({
jest: {},
}),
});
};
let testRun: ReturnType<typeof runContinuous>;
afterEach(async () => {
if (testRun) {
await testRun.end();
}
});
test('does not re-run tests when only access time is modified', async () => {
setupFiles();
testRun = runContinuous(DIR, ['--watchAll', '--no-watchman']);
const testCompletedRE = /Ran all test suites./g;
const numberOfTestRuns = (stderr: string): number => {
const matches = stderr.match(testCompletedRE);
return matches ? matches.length : 0;
};
// First run
await testRun.waitUntil(({stderr}) => numberOfTestRuns(stderr) === 1);
// Should re-run the test
const modulePath = path.join(DIR, 'foo.js');
const stat = fs.lstatSync(modulePath);
fs.utimesSync(modulePath, stat.atime.getTime(), stat.mtime.getTime());
await testRun.waitUntil(({stderr}) => numberOfTestRuns(stderr) === 2);
// Should NOT re-run the test
const fakeATime = 1541723621;
fs.utimesSync(modulePath, fakeATime, stat.mtime.getTime());
await sleep(3000);
expect(numberOfTestRuns(testRun.getCurrentOutput().stderr)).toBe(2);
// Should re-run the test
fs.writeFileSync(modulePath, 'module.exports = 1;', {encoding: 'utf-8'});
await testRun.waitUntil(({stderr}) => numberOfTestRuns(stderr) === 3);
});