Skip to content

Commit 2c1249e

Browse files
fix: jest --onlyFailures --listTests now correctly lists only failed tests (#15700)
1 parent c88d1f1 commit 2c1249e

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- `[jest-config]` Fix ESM TS config loading in a CJS project ([#15694](https://github.com/jestjs/jest/pull/15694))
12+
- `[jest-core]` jest --onlyFailures --listTests now correctly lists only failed tests([#15700](https://github.com/jestjs/jest/pull/15700))
1213

1314
### Features
1415

e2e/__tests__/listTests.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import * as path from 'path';
99
import * as fs from 'graceful-fs';
1010
import runJest from '../runJest';
11+
import {tmpdir} from 'os';
12+
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
1113

1214
const testRootDir = path.resolve(__dirname, '..', '..');
1315

@@ -85,4 +87,54 @@ describe('--listTests flag', () => {
8587
).toMatchSnapshot();
8688
});
8789
});
90+
91+
describe('--onlyFailures flag', () => {
92+
const tempDir = path.resolve(tmpdir(), 'temp-only-failures');
93+
94+
beforeEach(() => {
95+
// Clean up any existing temp directory and create a new one
96+
cleanup(tempDir);
97+
createEmptyPackage(tempDir, {jest: {testEnvironment: 'node'}});
98+
99+
// Create test files
100+
writeFiles(tempDir, {
101+
'failing.test.js':
102+
'test("failing test", () => { expect(true).toBe(false); });',
103+
'passing.test.js':
104+
'test("passing test", () => { expect(true).toBe(true); });',
105+
});
106+
});
107+
108+
afterEach(() => {
109+
cleanup(tempDir);
110+
});
111+
112+
it('lists only failed tests after a test run', () => {
113+
runJest(tempDir);
114+
115+
const {exitCode, stdout} = runJest(tempDir, [
116+
'--listTests',
117+
'--onlyFailures',
118+
]);
119+
120+
expect(exitCode).toBe(0);
121+
const listedTests = stdout
122+
.trim()
123+
.split('\n')
124+
.filter(line => line.length > 0);
125+
expect(listedTests).toHaveLength(1);
126+
expect(listedTests[0]).toMatch(/failing\.test\.js$/);
127+
expect(stdout).not.toMatch(/passing\.test\.js$/);
128+
});
129+
130+
it('lists no tests when no failed tests exist', () => {
131+
const {exitCode, stdout} = runJest('list-tests', [
132+
'--listTests',
133+
'--onlyFailures',
134+
]);
135+
136+
expect(exitCode).toBe(0);
137+
expect(stdout.trim()).toBe('');
138+
});
139+
});
88140
});

packages/jest-core/src/runJest.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ export default async function runJest({
216216

217217
allTests = await sequencer.sort(allTests);
218218

219+
if (globalConfig.onlyFailures) {
220+
if (failedTestsCache) {
221+
allTests = failedTestsCache.filterTests(allTests);
222+
} else {
223+
allTests = await sequencer.allFailedTests(allTests);
224+
}
225+
}
226+
219227
if (globalConfig.listTests) {
220228
const testsPaths = [...new Set(allTests.map(test => test.path))];
221229
let testsListOutput;
@@ -238,14 +246,6 @@ export default async function runJest({
238246
return;
239247
}
240248

241-
if (globalConfig.onlyFailures) {
242-
if (failedTestsCache) {
243-
allTests = failedTestsCache.filterTests(allTests);
244-
} else {
245-
allTests = await sequencer.allFailedTests(allTests);
246-
}
247-
}
248-
249249
const hasTests = allTests.length > 0;
250250

251251
if (!hasTests) {

0 commit comments

Comments
 (0)