Skip to content

Commit c718869

Browse files
committed
fix(expect, jest-snapshot, jest-circus, jest-types): Pass test.failing tests when containing failing snapshot matchers
1 parent 70b17d2 commit c718869

File tree

21 files changed

+233
-5
lines changed

21 files changed

+233
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
### Fixes
2727

28+
- `[expect, jest-snapshot]` Pass `test.failing` tests when containing failing snapshot matchers ([#14313](https://github.com/jestjs/jest/pull/14313))
2829
- `[jest-circus]` Prevent false test failures caused by promise rejections handled asynchronously ([#14110](https://github.com/jestjs/jest/pull/14110))
2930
- `[jest-config]` Handle frozen config object ([#14054](https://github.com/facebook/jest/pull/14054))
3031
- `[jest-config]` Allow `coverageDirectory` and `collectCoverageFrom` in project config ([#14180](https://github.com/jestjs/jest/pull/14180))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`test.failing doesnt update or remove snapshots 1`] = `
4+
"// Jest Snapshot v1, https://goo.gl/fbAQLP
5+
6+
exports[\`snapshots not updated nor removed 1\`] = \`"1"\`;
7+
8+
exports[\`snapshots not updated nor removed 2\`] = \`"1"\`;
9+
10+
exports[\`snapshots not updated nor removed 3\`] = \`"1"\`;
11+
"
12+
`;
13+
14+
exports[`test.failing doesnt update or remove snapshots 2`] = `
15+
"/**
16+
* Copyright (c) Meta Platforms, Inc. and affiliates.
17+
*
18+
* This source code is licensed under the MIT license found in the
19+
* LICENSE file in the root directory of this source tree.
20+
*/
21+
22+
test.failing('inline snapshot not updated', () => {
23+
// eslint-disable-next-line quotes
24+
expect('0').toMatchInlineSnapshot(\`"1"\`);
25+
});
26+
"
27+
`;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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 * as path from 'path';
9+
import * as fs from 'graceful-fs';
10+
import runJest from '../runJest';
11+
12+
describe('test.failing', () => {
13+
// Skip running tests with Jasmine
14+
if (process.env.JEST_JASMINE) {
15+
test.skip('No Jasmine runner', () => {});
16+
return;
17+
}
18+
19+
describe('should pass when', () => {
20+
test.failing('snapshot matchers fails', () => {
21+
expect('0').toMatchSnapshot();
22+
});
23+
24+
test.failing('snapshot doesnt exist', () => {
25+
expect('0').toMatchSnapshot();
26+
});
27+
28+
test.failing('inline snapshot matchers fails', () => {
29+
expect('0').toMatchInlineSnapshot('0');
30+
});
31+
32+
test.failing('at least one snapshot fails', () => {
33+
expect('1').toMatchSnapshot();
34+
expect('0').toMatchSnapshot();
35+
});
36+
});
37+
38+
describe('should fail when', () => {
39+
test.each([
40+
['snapshot', 'snapshot'],
41+
['inline snapshot', 'inlineSnapshot'],
42+
])('%s matchers pass', (_, fileName) => {
43+
const dir = path.resolve(__dirname, '../test-failing-snapshot-all-pass');
44+
const result = runJest(dir, [`./__tests__/${fileName}.test.js`]);
45+
expect(result.exitCode).toBe(1);
46+
});
47+
});
48+
49+
it('doesnt update or remove snapshots', () => {
50+
const dir = path.resolve(__dirname, '../test-failing-snapshot');
51+
const result = runJest(dir, ['-u']);
52+
expect(result.exitCode).toBe(0);
53+
expect(result.stdout).not.toMatch(/snapshots? (written|removed|obsolete)/);
54+
55+
const snapshot = fs
56+
.readFileSync(
57+
path.resolve(dir, './__tests__/__snapshots__/snapshot.test.js.snap'),
58+
)
59+
.toString();
60+
expect(snapshot).toMatchSnapshot();
61+
62+
const inlineSnapshot = fs
63+
.readFileSync(path.resolve(dir, './__tests__/inlineSnapshot.test.js'))
64+
.toString();
65+
expect(inlineSnapshot).toMatchSnapshot();
66+
});
67+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshots not updated 1`] = `"1"`;
4+
5+
exports[`snapshots not updated 2`] = `"1"`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
test.failing('inline snapshot not updated', () => {
9+
// eslint-disable-next-line quotes
10+
expect('1').toMatchInlineSnapshot(`"1"`);
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
test.failing('snapshots not updated', () => {
9+
expect('1').toMatchSnapshot();
10+
expect('1').toMatchSnapshot();
11+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`snapshots not updated nor removed 1`] = `"1"`;
4+
5+
exports[`snapshots not updated nor removed 2`] = `"1"`;
6+
7+
exports[`snapshots not updated nor removed 3`] = `"1"`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
test.failing('inline snapshot not updated', () => {
9+
// eslint-disable-next-line quotes
10+
expect('0').toMatchInlineSnapshot(`"1"`);
11+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
test.failing('snapshots not updated nor removed', () => {
9+
expect('1').toMatchSnapshot();
10+
expect('0').toMatchSnapshot();
11+
expect('0').toMatchSnapshot();
12+
});

0 commit comments

Comments
 (0)