How to import jest?

2019-04-04 02:48发布

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?

2条回答
戒情不戒烟
2楼-- · 2019-04-04 03:01

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.

查看更多
爷、活的狠高调
3楼-- · 2019-04-04 03:19

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
},
查看更多
登录 后发表回答