Jest: Issue when using expect.not

Created on 21 Mar 2018  ·  11Comments  ·  Source: facebook/jest

I am experiencing a strange behavior using expect.not, which may be a bug.
not seems not to defined on the expect object anymore.

I pasted a simple example from https://facebook.github.io/jest/docs/en/expect.html#expectnotstringcontainingstring
into a repl.it demo: https://repl.it/@holgergp/testing-not

The output of the test:

Jest v22.1.2 node v7.4.0 linux/amd64

 FAIL  ./not-test.js
  not.stringContaining
    ✕ matches if the actual string does not contain the expected substring (7ms)

  ● not.stringContaining › matches if the actual string does not contain the expected substring

    TypeError: Cannot read property 'stringContaining' of undefined

      3 | 
      4 |   it('matches if the actual string does not contain the expected substring', () => {
    > 5 |     expect('How are you?').toEqual(expect.not.stringContaining(expected));
      6 |   });
      7 | });

      at Object.it (not-test.js:5:46)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.913s
Ran all test suites.
exit status 1
Documentation Help Wanted Website good first issue

Most helpful comment

@SimenB I'm working on a PR to fix this now

All 11 comments

Hey @holgergp this seems to be an issue with the docs containing new features that haven't been published yet.

From what I can see .not asymmetric matchers were added in #5517 but haven't been published yet.

@SimenB I'm working on a PR to fix this now

@SimenB PR #5853 :smile:

Thank you!

Imma call this fixed

Any suggestions on what to use instead? =)

I made this solution but am not particularly fond of it:

    try {
      expect(app.html()).toEqual(expect.stringContaining('你好'));
    } catch (error) {
      expect(true).toEqual(true);
    }
    expect.assertions(2);

@gurbraj what do you mean? expect.not landed in Jest 23.0.0 so it's available now

How the expression should look like for the Jest before 23.0.0?

In case you are on a version < 23.0.0 here's what I ended up doing.

Example similar to the docs for not.stringContaining.

describe('not.toEqual using `expect.stringContaining`', () => {
  const expected = 'Hello world!';

  it('matches if the actual string does not contain the expected substring', () => {
    expect('How are you?').not.toEqual(expect.stringContaining(expected));
  });
});

If anyone sees any issue with this, please let me know.

Was this page helpful?
0 / 5 - 0 ratings