开玩笑,ES6模块"does not provide export named"



我有一个带有CRUD API(带sequelize(的express应用程序,我想用Jest测试它。我是单元测试的新手,所以我遵循Jest网站推荐的指南。

我遇到的问题是,我的应用程序是用ES6模块构建的,而Jest ES6模块是实验性的,它似乎没有";导入";包裹。

我有这个测试(取自指南(

import request from 'supertest';
import app from '../app';
describe('Test the root path', () => {
test('It should response the GET method', done => {
request(app)
.get('/')
.then(response => {
expect(response.statusCode).toBe(404);
done();
});
});
});

当我推出它时(使用NODE_OPTIONS=--experimental-vm-modules npx jest时,我不得不关注这个笑话维基页面(,它说

'sequelize' does not provide an export named 'DataTypes',当我正常启动应用程序时(就像使用npm start一样(,它运行良好,没有任何问题。

(完整的错误日志(:

(node:49576) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL  __tests__/app_test.js
● Test suite failed to run
SyntaxError: The requested module 'sequelize' does not provide an export named 'DataTypes'
at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:779:5)
at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
at runJest (node_modules/@jest/core/build/runJest.js:404:19)
at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

(和我的Jest配置(

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
export default async () => {
return {
verbose: true,
transform: {},
};
};

我做错什么了吗?我应该改为commonJS而不是ES6 吗

谢谢。

这是Jest:#9771中的一个已知问题。据说它在jest@28.0.0-alpha.0中是固定的。

解决这个问题的一个有趣的方法是从导入项目的package.json中删除main字段。

最新更新