javascriptjestjses6-modules

How can I import Jest?


I would like to get rid of global variables in my Jest test code. Specifically describe, it, and expect:

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

So I tried to add:

import { describe, it } from 'jest';

and

import jest from 'jest';

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

And other variation but it's not working.

How can I get my Jest test code working without globals?


Solution

  • After I realized Jest is running in Node.js, it realized I could do this:

    let { describe, it } = global;
    

    It is not perfect, but one step closer... now I don't need to configure my linter with global variables any more.