|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. 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 | + |
| 8 | +import {tmpdir} from 'os'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as fs from 'graceful-fs'; |
| 11 | +import {cleanup, writeFiles} from '../Utils'; |
| 12 | +import runJest from '../runJest'; |
| 13 | + |
| 14 | +const DIR = path.resolve(tmpdir(), 'non-watch-mode-onlyFailures'); |
| 15 | + |
| 16 | +beforeEach(() => cleanup(DIR)); |
| 17 | +afterEach(() => cleanup(DIR)); |
| 18 | + |
| 19 | +test('onlyFailures flag works in non-watch mode', () => { |
| 20 | + writeFiles(DIR, { |
| 21 | + '__tests__/a.js': ` |
| 22 | + test('bar', () => { expect('bar').toBe('foo'); }); |
| 23 | + `, |
| 24 | + '__tests__/b.js': ` |
| 25 | + test('foo', () => { expect('foo').toBe('foo'); }); |
| 26 | + `, |
| 27 | + 'package.json': JSON.stringify({ |
| 28 | + jest: { |
| 29 | + testEnvironment: 'node', |
| 30 | + }, |
| 31 | + }), |
| 32 | + }); |
| 33 | + |
| 34 | + let stdout, stderr; |
| 35 | + |
| 36 | + ({stdout, stderr} = runJest(DIR)); |
| 37 | + expect(stdout).toBe(''); |
| 38 | + expect(stderr).toMatch('FAIL __tests__/a.js'); |
| 39 | + expect(stderr).toMatch('PASS __tests__/b.js'); |
| 40 | + |
| 41 | + // only the failed test should run and it should fail |
| 42 | + ({stdout, stderr} = runJest(DIR, ['--onlyFailures'])); |
| 43 | + expect(stdout).toBe(''); |
| 44 | + expect(stderr).toMatch('FAIL __tests__/a.js'); |
| 45 | + expect(stderr).not.toMatch('__tests__/b.js'); |
| 46 | + |
| 47 | + // fix the failing test |
| 48 | + const data = "test('bar 1', () => { expect('bar').toBe('bar'); })"; |
| 49 | + fs.writeFileSync(path.join(DIR, '__tests__/a.js'), data); |
| 50 | + |
| 51 | + // only the failed test should run and it should pass |
| 52 | + ({stdout, stderr} = runJest(DIR, ['--onlyFailures'])); |
| 53 | + expect(stdout).toBe(''); |
| 54 | + expect(stderr).toMatch('PASS __tests__/a.js'); |
| 55 | + expect(stderr).not.toMatch('__tests__/b.js'); |
| 56 | + |
| 57 | + // No test should run |
| 58 | + ({stdout, stderr} = runJest(DIR, ['--onlyFailures'])); |
| 59 | + expect(stdout).toBe('No failed test found.'); |
| 60 | + expect(stderr).toBe(''); |
| 61 | +}); |
0 commit comments