|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import fs from 'fs'; |
| 13 | +import os from 'os'; |
| 14 | +import path from 'path'; |
| 15 | +import {cleanup, extractSummaries, writeFiles} from '../Utils'; |
| 16 | +import {until as runJestUntil} from '../runJest'; |
| 17 | + |
| 18 | +const DIR = path.resolve(os.tmpdir(), 'watch_mode_no_access'); |
| 19 | + |
| 20 | +const sleep = time => new Promise(resolve => setTimeout(resolve, time)); |
| 21 | + |
| 22 | +beforeEach(() => cleanup(DIR)); |
| 23 | +afterAll(() => cleanup(DIR)); |
| 24 | + |
| 25 | +const setupFiles = () => { |
| 26 | + writeFiles(DIR, { |
| 27 | + '__tests__/foo.spec.js': ` |
| 28 | + const foo = require('../foo'); |
| 29 | + test('foo', () => { expect(typeof foo).toBe('number'); }); |
| 30 | + `, |
| 31 | + 'foo.js': ` |
| 32 | + module.exports = 0; |
| 33 | + `, |
| 34 | + 'package.json': JSON.stringify({ |
| 35 | + jest: {}, |
| 36 | + }), |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +test('does not re-run tests when only access time is modified', async () => { |
| 41 | + setupFiles(); |
| 42 | + |
| 43 | + const testRun = runJestUntil( |
| 44 | + DIR, |
| 45 | + ['--watchAll', '--no-watchman'], |
| 46 | + '1 failed, 1 total', |
| 47 | + ); |
| 48 | + |
| 49 | + await sleep(1000); |
| 50 | + |
| 51 | + // Should re-run the test |
| 52 | + const modulePath = path.join(DIR, 'foo.js'); |
| 53 | + const stat = fs.lstatSync(modulePath); |
| 54 | + fs.utimesSync(modulePath, stat.atime, stat.mtime); |
| 55 | + |
| 56 | + await sleep(1000); |
| 57 | + |
| 58 | + // Should not re-run the test |
| 59 | + const fakeATime = 1541723621; |
| 60 | + fs.utimesSync(modulePath, fakeATime, stat.mtime); |
| 61 | + |
| 62 | + await sleep(1000); |
| 63 | + |
| 64 | + // Should re-run the test |
| 65 | + fs.writeFileSync(modulePath, 'module.exports = 1;', {encoding: 'utf-8'}); |
| 66 | + |
| 67 | + await sleep(1000); |
| 68 | + |
| 69 | + // Should make the test fail and finish the process |
| 70 | + fs.writeFileSync(modulePath, 'module.exports = undefined;', { |
| 71 | + encoding: 'utf-8', |
| 72 | + }); |
| 73 | + |
| 74 | + const {stderr} = await testRun; |
| 75 | + const results = extractSummaries(stderr); |
| 76 | + |
| 77 | + // initial YES, mtime YES, atime NO, mtime YES, fail YES |
| 78 | + expect(results).toHaveLength(4); |
| 79 | + expect(results.map(result => result.summary)).toMatchSnapshot(); |
| 80 | +}); |
0 commit comments