Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make expect.hasAssertions ignore skipped tests #3595

Closed
bookman25 opened this issue May 17, 2017 · 8 comments
Closed

make expect.hasAssertions ignore skipped tests #3595

bookman25 opened this issue May 17, 2017 · 8 comments

Comments

@bookman25
Copy link
Contributor

Do you want to request a feature or report a bug?
feature

What is the current behavior?
If you use expect.hasAssertions() inside of a skipped test, the test gets marked as failed. This includes if you use fit on another test in the same spec file.

What is the expected behavior?
It would be nice if expect.hasAssertions() was not enforced for skipped tests.

Our use case is we have expect.hasAssertions() inside our setupTestFrameworkScriptFile so that every test is required to have at least one assertion. However sometimes when debugging an individual test we will use fit and as a result all of the other tests in the spec file get marked as failures.

@anilreddykatta
Copy link
Contributor

anilreddykatta commented May 22, 2017

@bookman25

// App.jest.js
fit('renders without crashing', () => {
  expect(1).toBeTruthy()
});

test('.hasAssertions', () => {
  expect.hasAssertions()
})

This particular piece of code is passing without issues. Can you provide some more information on it? Sample code will be really helpful

@bookman25
Copy link
Contributor Author

So that does appear to work (I thought I tested that case and it was failing... but it's working for that case now).

The case that is still failing for us is when we put expect.hasAsssertions() into our setupTestFrameworkScriptFile. This enforces that every test has to have at least 1 assertion and that does not appear to respect the fit syntax.

@CGamesPlay
Copy link

Here's a failing test:

describe('test', function() {
  it.only('runs this test', function() {
    expect.hasAssertions();
    expect(1).toBe(1);
  })

  it('fails this test', function() {
    expect(2).toBe(2);
  });
});

Results:

$ node_modules/.bin/jest client/src/Common/fake.test.js
 FAIL  client/src/Common/fake.test.js
  ● test › fails this test

    expect.hasAssertions()

    Expected at least one assertion to be called but received none.

      at addAssertionErrors (node_modules/jest-jasmine2/build/setup-jest-globals.js:88:21)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at process._tickCallback (internal/process/next_tick.js:109:7)

  test
    ✓ runs this test (3ms)
    ✕ fails this test

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.187s
Ran all test suites matching "client/src/Common/fake.test.js".
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

$ node_modules/.bin/jest --version
v20.0.4

@localshred
Copy link

localshred commented Jul 24, 2017

I just encountered the same issue but from a different perspective. I have a bunch of stubbed/skipped tests that I have been filling out, and in one of the non-skipped tests I have an expect.hasAssertions() call. It causes all of the skipped tests to fail that they have no assertions (in my case they don't even have test functions yet).

Here is a boiled down example:

describe('my file', () => {
  xdescribe('section A', () => {
    xit('fill this out') // <--- test fails with "Expected at least one assertion to be called but received none."
    xit('fill this out too') // <--- test fails with "Expected at least one assertion to be called but received none."
    xit('fill this out as well') // <--- test fails with "Expected at least one assertion to be called but received none."
  })

  describe('section b', () => {
    it('testing this one', () => {
      expect.hasAssertions()  // <--- the above tests fail when this line is present, but are skipped when it is not present
      return myPromiseReturningFunc()
        .then(result => expect(result).not.toBeNull())
    })
  })
})

@rausanka
Copy link

rausanka commented Nov 6, 2017

Here is a simplified example of @bookman25's comment about using hasAssertions() outside of a test method resulting in skipped tests failing:

xdescribe('suite', () => {
  expect.hasAssertions()

  it('test', () => {
    expect(true).toBe(true)
  })
})

@juicetin
Copy link

juicetin commented Dec 22, 2017

Just following up on whether there's plans to introduce this behaviour, or if someone is working on one already? Having expect.hasAssertions() tends to be a relatively important feature of a large test suite but also makes use of test.only during development more cumbsersome :<

@bookman25
Copy link
Contributor Author

Looks like as a result of #4498 this ticket should probably be closed. hasAssertions is now only specific to an individual test and can no longer be used the way I originally described.

@cpojer cpojer closed this as completed Jan 3, 2018
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants