Skip to content

Commit c3b95f8

Browse files
KhaledElmorsynicojs
authored andcommitted
fix(expect, jest-snapshot): Pass test.failing tests when containing failing snapshot matchers (#14313)
1 parent f3e41ef commit c3b95f8

File tree

21 files changed

+220
-5
lines changed

21 files changed

+220
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
2020
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
2121
- `[jest-circus]` [**BREAKING**] Prevent false test failures caused by promise rejections handled asynchronously ([#14315](https://github.com/jestjs/jest/pull/14315))
22+
- `[jest-circus, jest-expect, jest-snapshot]` Pass `test.failing` tests when containing failing snapshot matchers ([#14313](https://github.com/jestjs/jest/pull/14313))
2223
- `[jest-config]` Make sure to respect `runInBand` option ([#14578](https://github.com/facebook/jest/pull/14578))
2324
- `[@jest/expect-utils]` Fix comparison of `DataView` ([#14408](https://github.com/jestjs/jest/pull/14408))
2425
- `[jest-leak-detector]` Make leak-detector more aggressive when running GC ([#14526](https://github.com/jestjs/jest/pull/14526))
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 {skipSuiteOnJasmine} from '@jest/test-utils';
11+
import runJest from '../runJest';
12+
13+
skipSuiteOnJasmine();
14+
15+
describe('test.failing', () => {
16+
describe('should pass when', () => {
17+
test.failing('snapshot matchers fails', () => {
18+
expect('0').toMatchSnapshot();
19+
});
20+
21+
test.failing('snapshot doesnt exist', () => {
22+
expect('0').toMatchSnapshot();
23+
});
24+
25+
test.failing('inline snapshot matchers fails', () => {
26+
expect('0').toMatchInlineSnapshot('0');
27+
});
28+
29+
test.failing('at least one snapshot fails', () => {
30+
expect('1').toMatchSnapshot();
31+
expect('0').toMatchSnapshot();
32+
});
33+
});
34+
35+
describe('should fail when', () => {
36+
test.each([
37+
['snapshot', 'snapshot'],
38+
['inline snapshot', 'inlineSnapshot'],
39+
])('%s matchers pass', (_, fileName) => {
40+
const dir = path.resolve(__dirname, '../test-failing-snapshot-all-pass');
41+
const result = runJest(dir, [`./__tests__/${fileName}.test.js`]);
42+
expect(result.exitCode).toBe(1);
43+
});
44+
});
45+
46+
it('doesnt update or remove snapshots', () => {
47+
const dir = path.resolve(__dirname, '../test-failing-snapshot');
48+
const result = runJest(dir, ['-u']);
49+
expect(result.exitCode).toBe(0);
50+
expect(result.stdout).not.toMatch(/snapshots? (written|removed|obsolete)/);
51+
52+
const snapshot = fs
53+
.readFileSync(
54+
path.resolve(dir, './__tests__/__snapshots__/snapshot.test.js.snap'),
55+
)
56+
.toString();
57+
expect(snapshot).toMatchSnapshot();
58+
59+
const inlineSnapshot = fs
60+
.readFileSync(path.resolve(dir, './__tests__/inlineSnapshot.test.js'))
61+
.toString();
62+
expect(inlineSnapshot).toMatchSnapshot();
63+
});
64+
});
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)