question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expect test to fail

See original GitHub issue

🚀 Feature Proposal

Hi folks, I would like there to be a way to write a test that is expected to fail. Essentially, this would flip the outcome of the test from success to failure and vice versa.

Motivation

This came up when I was writing some code to auto-fail tests that call console.error. I want to verify that tests that call console.error fail, but there is no way to do so–the test is supposed to fail, but failed tests would in turn break my build. Note that unfortunately there is no way to write this using an expect and condition–I am programmatically failing the tests using expect.assertions(Infinity), so there’s nothing I could mock out.

Example

describe('Ensure tests fail when they're supposed to', {
  itShouldFail('because 1 is not 0', () => {
    expect(1).toBe(0);
  });
});

Pitch

One of the scariest things as an engineer is a test that passes when it should fail. This is unfortunately pretty common when dealing with asynchronous Javascript. As such it’s occasionally a good idea to ensure that a certain kind of behavior always generates a test failure. While the above example may seem trivial, it’s actually useful to ensure that tests that are supposed to fail actually fail.

This would also be useful for testing the internal behavior of Jest.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
alistairnewparkcommented, Sep 17, 2019

XFAIL is often used to mark a bug that is unfixed. The test is in place, but the fix is not done yet. I imagine something like:

test.xfail(…);

Similar to the test.todo(…); place holder and test.skip(…);.

test.xfail(…); would show as a separate row in the test results, although it is treated as a pass for the purpose of continuing the test run. When an XFAIL test passes then it is marked as XPASS in the test results, but a fail for the test run. This highlights the test is now working and the test should be changed from test.xfail(…); to test(…);.

For TAP output an XFAIL is shown as: not ok 10 # TODO See issue #123456789 and an XPASS as: ok 10 # TODO See issue #123456789 For reference see https://testanything.org/tap-version-13-specification.html#todo-tests

This would be a good feature to have in Jest.

This issue gives a negative test as an example and I agree that should not be a reason for using XFAIL.

7reactions
vithcommented, Dec 30, 2019

ava also has such a feature:

// See: github.com/user/repo/issues/1234
test.failing('demonstrate some bug', t => {
	t.fail(); // Test will count as passed
});

quite useful. i must have checked the jest docs for it on 4 or 5 occasions now 😦

the ability to parse the test results with some external tool is not really a replacement for the use case of contributing a known-failing test as a way of reporting a bug.

https://github.com/avajs/ava/blob/master/docs/01-writing-tests.md#failing-tests https://github.com/avajs/ava

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Jest, how can I make a test fail? - Stack Overflow
Method-1. You can wrap your promise function within expect and tell jest the function should reject with the given error. If the someOperation() ......
Read more >
Expected Failures | Apple Developer Documentation
If any assertions fail in a test after you call XCTExpectFailure , Xcode marks the test as an expected failure instead of a...
Read more >
Expect - Jest
If you mix them up, your tests will still work, but the error messages on failing tests will look strange.
Read more >
Jest explicitly or arbitrarily force fail() a test - Code with Hugo
When testing code with Jest, it can sometimes be useful to fail a test arbitrarily. This post goes through a few scenarios where...
Read more >
Marking Tests as Expecting to Fail - Python Testing with pytest ...
Marking Tests as Expecting to Fail With the skip and skipif markers, a test isn't even attempted if skipped. With the xfail marker,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found