Jest,ES6模块“不提供名为”的导出”

ff29svar  于 6个月前  发布在  Jest
关注(0)|答案(2)|浏览(110)

我有一个带有CRUD API(带有续集)的Express应用程序,我想用Jest测试它。我对单元测试很陌生,所以我遵循了Jest网站推荐的guide
我遇到的问题是,我的应用程序是用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时,我必须遵循这个jest wiki page),它说
'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)

  • (和我的笑话配置)*
// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */

export default async () => {
    return {
        verbose: true,
        transform: {},
    };
};


我做错了什么吗?我应该换成commonJS而不是ES6吗
谢谢

rks48beu

rks48beu1#

这是Jest:#9771中的一个已知问题。据说在[[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)中已修复。
解决这个问题的一个有趣的方法是从导入项目的package.json中删除main字段。

ffdz8vbo

ffdz8vbo2#

这个问题在新版本的jest中得到解决了吗?我在jest version: 29.7.0中遇到了同样的问题。

相关问题