Javascript 使用ES6导入语句节点运行测试

Javascript 使用ES6导入语句节点运行测试,javascript,node.js,jestjs,es6-modules,Javascript,Node.js,Jestjs,Es6 Modules,我在ubuntu18.04.5上使用nodev15.0.1和jest26.6.0 我已经设置了一个简单的测试用例,在文件的顶部,我尝试使用ES6导入语句: import Color from './color.js' test("Initialized properly after construction", () => { expect(1 + 1).toBe(2); }); 此外,下面是color.js的代码: class Color { co

我在ubuntu
18.04.5
上使用node
v15.0.1
和jest
26.6.0

我已经设置了一个简单的测试用例,在文件的顶部,我尝试使用ES6导入语句:

import Color from './color.js'

test("Initialized properly after construction", () => {
    expect(1 + 1).toBe(2);
});
此外,下面是color.js的代码:

class Color {
    constructor(r, g, b, a) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
}

export {
    Color
};
当我运行jest时,我得到以下错误输出:


 FAIL  src/modules/color.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /home/daniel/Documents/raycaster/src/modules/color.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Color from './color.js'
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at new Script (node:vm:100:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.288 s
Ran all test suites.
npm ERR! code 1


尽管有这些配置,jest似乎无法在ES6兼容模式下运行。要启用导入语句,我需要进行哪些配置?

因为您要将类作为默认导出导入,所以需要输入一个默认值。要了解更多信息,请查看此


由于要将类作为默认导出导入,因此需要输入默认值。要了解更多信息,请查看此

作为国家

安装jest时自动安装babel jest,如果项目中存在babel配置,则会自动转换文件。要避免此行为,可以显式重置转换配置选项:

转换:{}

这正是此配置所做的,它禁用Babel并阻止
import
被传输

解决方案是删除
transform:{}
,并仅出于目的使用
transform

所提到的专用于节点中的本机ES模块支持。它建议
transform:{}
需要通过以下方式启用它们:

node --experimental-vm-modules node_modules/.bin/jest
这不建议常规使用,因为节点和Jest中的ESM支持都是实验性的,可能会导致问题和缺少功能,因为Jest已经严重依赖CommonJS模块。

as states

安装jest时自动安装babel jest,如果项目中存在babel配置,则会自动转换文件。要避免此行为,可以显式重置转换配置选项:

转换:{}

这正是此配置所做的,它禁用Babel并阻止
import
被传输

解决方案是删除
transform:{}
,并仅出于目的使用
transform

所提到的专用于节点中的本机ES模块支持。它建议
transform:{}
需要通过以下方式启用它们:

node --experimental-vm-modules node_modules/.bin/jest
这不建议常规使用,因为节点和Jest中的ESM支持都是实验性的,可能会导致问题和缺少功能,因为Jest已经严重依赖CommonJS模块。

我发现

这突出了我需要的作品:

在我的
package.json
文件中,我需要指定以下内容:

"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
我发现

这突出了我需要的作品:

在我的
package.json
文件中,我需要指定以下内容:

"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"

请分享颜色的内容。js@chyke007现在已经添加了color.js的内容,请共享color的内容。js@chyke007现在我已经添加了color.js的内容,谢谢你这么说,一旦我在package.json文件中更改了用于触发jest的命令,我就遇到了这个问题,所以它非常有用!感谢您这么说,一旦我在package.json文件中更改了用于触发jest的命令,我就遇到了这个问题,所以它非常有用!