Does Jest support ES6 import/export?

后端 未结 7 835
情话喂你
情话喂你 2020-11-29 01:21

If I use import/export from ES6 then all my Jest tests fail with error:

Unexpected reserved word

I convert my object un

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 01:30

    UPDATE 2020 - native support of ECMAScript modules (ESM)


    According to this issue, there is native support of ESM from jest@25.4.0. So you won't have to use babel anymore. At the time of writing this answer (05/2020), to activate that you need to do three simple things:

    • Make sure you don't transform away import statements by setting transform: {} in config file
    • Run node@^12.16.0 || >=13.2.0 with --experimental-vm-modules flag
    • Run your test with jest-environment-node or jest-environment-jsdom-sixteen.

    So your Jest configuration file should contain at least this:

    export default {
        testEnvironment: 'jest-environment-node',
        transform: {}
        ...
    };
    

    And to set --experimental-vm-modules flag, you will have to run Jest as follows:

    node --experimental-vm-modules node_modules/jest/bin/jest.js
    

    (I hope this will change in the future)

提交回复
热议问题