How to import jest?

2019-04-04 03:11发布

问题:

I would like to get rid of globals in my jest test code. Specifically describe, it and expect

describe('Welcome (Snapshot)', () => {
  it('Welcome renders hello world', () => {
     ...
  });
});

So I tried add

import {describe,it} from 'jest';

and

import jest from 'jest';

jest.describe( ... 
  jest.it( ... 

and other variations..

But no luck.

How should I get it working?

回答1:

The simplest solution for this is adding jest: true to your env config in eslint, like so:

"env": {
  "browser": true,
  "node": true,
  "jasmine": true,
  "jest": true,
  "es6": true
},


回答2:

After I realized jest is running in node, it realized I could do this:

let { describe, it } = global;

Not perfect, but one step closer.. now I don't need to configure my linter with globals anymore.