[英]Do Any JavaScript Test Frameworks Support ES Modules
Do any test frameworks support tests that use the new ES Modules syntax?是否有任何测试框架支持使用新的 ES 模块语法的测试? I have a JS application which heavily uses .mjs
files with ES Modules.我有一个 JS 应用程序,它大量使用带有 ES 模块的.mjs
文件。 I tried Jest and Jasmine, both of which throw errors when I try to write run tests for my app.我尝试了 Jest 和 Jasmine,当我尝试为我的应用程序编写运行测试时,它们都会抛出错误。 I need to test this file:我需要测试这个文件:
math.mjs数学.mjs
export function add(a, b) {
return a + b;
}
As of February 2021, this is what I saw:截至 2021 年 2 月,这是我所看到的:
.mjs
extension though. Jasmine :内置支持,但需要.mjs
扩展名。You can use the esm
package with Jasmine.您可以将esm
包与 Jasmine 一起使用。 Not sure about Jest though 🤔虽然不确定 Jest 🤔
math.spec.js数学规范.js
import { add } from './math.mjs';
describe('Add', () => {
it('should add 3 and 2', () => {
expect(add(3,2)).toBe(5);
});
});
$ yarn global add jasmine esm
$ jasmine --require=esm
Randomized with seed 44366
Started
.
1 spec, 0 failures
Finished in 0.004 seconds
Other test runners with support for ES Modules are:其他支持 ES 模块的测试运行器是:
The project Japa works with ESM and Typescript, and works perfectly in my set-up (Node.js 16.x with "type": "module"
and .js
files). Japa项目与 ESM 和 Typescript 一起使用,并且在我的设置中完美运行(Node.js 16.x 带有"type": "module"
和.js
文件)。 The only issue I got was easy to solve, so you should give it a try我遇到的唯一问题很容易解决,所以你应该试一试
I tried Vitest and it worked like a charm.我尝试了 Vitest ,它就像一个魅力。 My goal was to build a publishable library with typescript.我的目标是用 typescript 构建一个可发布的库。 The library was part of a monorepo built with Nx w with "type": "module"
in package.json
and the following in my tsconfig.json
:该库是使用Nx w 构建的 monorepo 的一部分,其中 package.json 中的"type": "module"
以及我的package.json
中的以下tsconfig.json
:
"compilerOptions": {
"module": "ESNext",
"target": "ES6",
"moduleResolution": "node"
}
It worked with 0 configuration, and I tried testing functions that used both CJS and ESM type imports from 3rd party packages.它适用于 0 配置,我尝试测试使用来自 3rd 方包的 CJS 和 ESM 类型导入的功能。
On a side note, I really like the clean syntax.顺便说一句,我真的很喜欢简洁的语法。 Plus, vitest also supports testing components built with React/Vue.此外,vitest 还支持使用 React/Vue 构建的测试组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.