Skip to content

Commit 34d15d1

Browse files
authored
feat: add test.failing method (#12610)
1 parent 8433c5c commit 34d15d1

23 files changed

+888
-5
lines changed

CHANGELOG.md

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

33
### Features
44

5+
- `[jest-circus]` Add `failing` test modifier that inverts the behaviour of tests ([#12610](https://github.com/facebook/jest/pull/12610))
56
- `[jest-environment-node, jest-environment-jsdom]` Allow specifying `customExportConditions` ([#12774](https://github.com/facebook/jest/pull/12774))
67

78
### Fixes

docs/GlobalAPI.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,62 @@ test.each`
731731
});
732732
```
733733

734+
### `test.failing(name, fn, timeout)`
735+
736+
Also under the alias: `it.failing(name, fn, timeout)`
737+
738+
:::note
739+
740+
This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.
741+
742+
:::
743+
744+
Use `test.failing` when you are writing a test and expecting it to fail. These tests will behave the other way normal tests do. If `failing` test will throw any errors then it will pass. If it does not throw it will fail.
745+
746+
:::tip
747+
748+
You can use this type of tests i.e. when writing code in a BDD way. In that case the tests will not show up as failing until they pass. Then you can just remove the `failing` modifier to make them pass.
749+
750+
It can also be a nice way to contribute failing tests to a project, even if you don't know how to fix the bug.
751+
752+
:::
753+
754+
Example:
755+
756+
```js
757+
test.failing('it is not equal', () => {
758+
expect(5).toBe(6); // this test will pass
759+
});
760+
761+
test.failing('it is equal', () => {
762+
expect(10).toBe(10); // this test will fail
763+
});
764+
```
765+
766+
### `test.only.failing(name, fn, timeout)`
767+
768+
Also under the aliases: `it.only.failing(name, fn, timeout)`, `fit.failing(name, fn, timeout)`
769+
770+
:::note
771+
772+
This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.
773+
774+
:::
775+
776+
Use `test.only.failing` if you want to only run a specific failing test.
777+
778+
### `test.skip.failing(name, fn, timeout)`
779+
780+
Also under the aliases: `it.skip.failing(name, fn, timeout)`, `xit.failing(name, fn, timeout)`, `xtest.failing(name, fn, timeout)`
781+
782+
:::note
783+
784+
This is only available with the default [jest-circus](https://github.com/facebook/jest/tree/main/packages/jest-circus) runner.
785+
786+
:::
787+
788+
Use `test.skip.failing` if you want to skip running a specific failing test.
789+
734790
### `test.only(name, fn, timeout)`
735791

736792
Also under the aliases: `it.only(name, fn, timeout)`, and `fit(name, fn, timeout)`

e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
1616
14 | });
1717
15 | });
1818
19-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:145:11)
19+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:153:11)
2020
at test (__tests__/asyncDefinition.test.js:12:5)
2121
2222
● Test suite failed to run
@@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = `
4646
20 | });
4747
21 |
4848
49-
at eventHandler (../../packages/jest-circus/build/eventHandler.js:145:11)
49+
at eventHandler (../../packages/jest-circus/build/eventHandler.js:153:11)
5050
at test (__tests__/asyncDefinition.test.js:18:3)
5151
5252
● Test suite failed to run
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`works with all statuses 1`] = `
4+
"FAIL __tests__/statuses.test.js
5+
✓ passes
6+
✕ fails
7+
✓ failing fails = passes
8+
✓ failing fails = passes with test syntax
9+
✕ failing passes = fails
10+
○ skipped skips
11+
○ skipped skipped failing 1
12+
○ skipped skipped failing 2
13+
○ skipped skipped failing with different syntax
14+
○ skipped skipped failing with another different syntax
15+
✎ todo todo
16+
17+
● fails
18+
19+
expect(received).toBe(expected) // Object.is equality
20+
21+
Expected: 101
22+
Received: 10
23+
24+
11 |
25+
12 | it('fails', () => {
26+
> 13 | expect(10).toBe(101);
27+
| ^
28+
14 | });
29+
15 |
30+
16 | it.skip('skips', () => {
31+
32+
at Object.toBe (__tests__/statuses.test.js:13:14)
33+
34+
failing passes = fails
35+
36+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
37+
38+
36 | });
39+
37 |
40+
> 38 | it.failing('failing passes = fails', () => {
41+
| ^
42+
39 | expect(10).toBe(10);
43+
40 | });
44+
41 |
45+
46+
at Object.failing (__tests__/statuses.test.js:38:4)"
47+
`;
48+
49+
exports[`works with concurrent and only mode 1`] = `
50+
"FAIL __tests__/worksWithConcurrentOnlyMode.test.js
51+
block with concurrent
52+
✕ failing passes = fails
53+
✓ failing fails = passes
54+
○ skipped skipped failing test
55+
○ skipped skipped failing fails
56+
57+
● block with concurrent › failing passes = fails
58+
59+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
60+
61+
11 | });
62+
12 |
63+
> 13 | it.concurrent.only.failing('failing passes = fails', () => {
64+
| ^
65+
14 | expect(10).toBe(10);
66+
15 | });
67+
16 |
68+
69+
at failing (__tests__/worksWithConcurrentOnlyMode.test.js:13:22)
70+
at Object.describe (__tests__/worksWithConcurrentOnlyMode.test.js:8:1)"
71+
`;
72+
73+
exports[`works with concurrent mode 1`] = `
74+
"FAIL __tests__/worksWithConcurrentMode.test.js
75+
block with concurrent
76+
✕ failing test
77+
✕ failing passes = fails
78+
✓ failing fails = passes
79+
○ skipped skipped failing fails
80+
81+
● block with concurrent › failing test
82+
83+
expect(received).toBe(expected) // Object.is equality
84+
85+
Expected: 101
86+
Received: 10
87+
88+
8 | describe('block with concurrent', () => {
89+
9 | it('failing test', () => {
90+
> 10 | expect(10).toBe(101);
91+
| ^
92+
11 | });
93+
12 |
94+
13 | it.concurrent.failing('failing passes = fails', () => {
95+
96+
at Object.toBe (__tests__/worksWithConcurrentMode.test.js:10:16)
97+
98+
block with concurrentfailing passes = fails
99+
100+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
101+
102+
11 | });
103+
12 |
104+
> 13 | it.concurrent.failing('failing passes = fails', () => {
105+
| ^
106+
14 | expect(10).toBe(10);
107+
15 | });
108+
16 |
109+
110+
at failing (__tests__/worksWithConcurrentMode.test.js:13:17)
111+
at Object.describe (__tests__/worksWithConcurrentMode.test.js:8:1)"
112+
`;
113+
114+
exports[`works with only mode 1`] = `
115+
"FAIL __tests__/worksWithOnlyMode.test.js
116+
block with only, should pass
117+
✓ failing fails = passes, should pass
118+
○ skipped failing test but skipped
119+
○ skipped passing test but skipped
120+
block with only, should fail
121+
✕ failing passes = fails, should fail
122+
○ skipped failing test but skipped
123+
○ skipped passing test but skipped
124+
block with only in other it, should skip
125+
✕ failing test
126+
○ skipped failing passes = fails, should fail but skipped
127+
○ skipped passing test but skipped
128+
block with only with different syntax, should fail
129+
✕ failing passes = fails, should fail 1
130+
✕ failing passes = fails, should fail 2
131+
○ skipped failing test but skipped
132+
○ skipped passing test but skipped
133+
134+
● block with only, should fail › failing passes = fails, should fail
135+
136+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
137+
138+
21 |
139+
22 | describe('block with only, should fail', () => {
140+
> 23 | it.only.failing('failing passes = fails, should fail', () => {
141+
| ^
142+
24 | expect(10).toBe(10);
143+
25 | });
144+
26 |
145+
146+
at failing (__tests__/worksWithOnlyMode.test.js:23:11)
147+
at Object.describe (__tests__/worksWithOnlyMode.test.js:22:1)
148+
149+
block with only in other it, should skipfailing test
150+
151+
expect(received).toBe(expected) // Object.is equality
152+
153+
Expected: 101
154+
Received: 10
155+
156+
41 | // eslint-disable-next-line jest/no-focused-tests
157+
42 | it.only('failing test', () => {
158+
> 43 | expect(10).toBe(101);
159+
| ^
160+
44 | });
161+
45 |
162+
46 | it('passing test but skipped', () => {
163+
164+
at Object.toBe (__tests__/worksWithOnlyMode.test.js:43:16)
165+
166+
block with only with different syntax, should failfailing passes = fails, should fail 1
167+
168+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
169+
170+
50 |
171+
51 | describe('block with only with different syntax, should fail', () => {
172+
> 52 | fit.failing('failing passes = fails, should fail 1', () => {
173+
| ^
174+
53 | expect(10).toBe(10);
175+
54 | });
176+
55 |
177+
178+
at failing (__tests__/worksWithOnlyMode.test.js:52:7)
179+
at Object.describe (__tests__/worksWithOnlyMode.test.js:51:1)
180+
181+
block with only with different syntax, should failfailing passes = fails, should fail 2
182+
183+
Failing test passed even though it was supposed to fail. Remove \`.failing\` to remove error.
184+
185+
54 | });
186+
55 |
187+
> 56 | test.only.failing('failing passes = fails, should fail 2', () => {
188+
| ^
189+
57 | expect(10).toBe(10);
190+
58 | });
191+
59 |
192+
193+
at failing (__tests__/worksWithOnlyMode.test.js:56:13)
194+
at Object.describe (__tests__/worksWithOnlyMode.test.js:51:1)"
195+
`;
196+
197+
exports[`works with skip mode 1`] = `
198+
"FAIL __tests__/worksWithSkipMode.test.js
199+
block with only, should pass
200+
✕ failing test
201+
✓ failing fails = passes
202+
○ skipped skipped failing fails = passes, should pass
203+
○ skipped passing test
204+
block with only, should fail
205+
✓ passing test
206+
✓ failing passes = fails
207+
○ skipped failing passes = fails, should fail
208+
○ skipped failing test
209+
210+
● block with only, should pass › failing test
211+
212+
expect(received).toBe(expected) // Object.is equality
213+
214+
Expected: 101
215+
Received: 10
216+
217+
12 |
218+
13 | it('failing test', () => {
219+
> 14 | expect(10).toBe(101);
220+
| ^
221+
15 | });
222+
16 |
223+
17 | it.skip('passing test', () => {
224+
225+
at Object.toBe (__tests__/worksWithSkipMode.test.js:14:16)"
226+
`;

0 commit comments

Comments
 (0)