我正在尝试为仅导出 ES 模块的 3rd 方包编写一个简单的 Jest 测试。它是 http
的包装器服务器。
这是我设置的测试仓库(只需运行 yarn && yarn jest
即可重现):https://github.com/jamesopti/hocuspocus-testing
无论我试验什么配置,在尝试运行它时仍然会出现此错误:
Must use import to load ES Module: /Users/j/hocuspocus-testing/node_modules/@hocuspocus/server/dist/hocuspocus-server.esm.js
> 1 | import { Server, Hocuspocus } from '@hocuspocus/server'
| ^
2 | import * as request from 'supertest'
3 |
4 | describe('Server (e2e)', () => {
我已经尝试过的东西:transformIgnorePatterns
transformIgnorePatterns: ['node_modules/(?!@hocuspocus/)']
babel-jest
使用 BabelYou appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
有什么想法我在这里想念的吗?我认为这很简单
[编辑 1] - 新增
tsconfig.json
和一个工作 src/index.ts
文件到 the example repo .
最佳答案
因此,对于仍然遇到此问题的任何人,文档的这一部分中解释了 ESM 配置:
https://kulshekhar.github.io/ts-jest/docs/guides/esm-support
{
// [...]
"jest": {
"extensionsToTreatAsEsm": [".ts"],
"globals": {
"ts-jest": {
"useESM": true
}
}
}
}
关于node.js - 在 node_modules 错误中带有 ES 模块的 Jest Typescript - 必须使用导入来加载 ES 模块 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68520619/