JEST
安装
yarn add --dev jest
配置
package.json -> scripts -> “test”: “jest”
使用
export
建立测试目录
foo.spec.js
import
Jest不支持ESM的问题
直接使用会报错:(因为Jest默认是node下的不支持ESM)
SyntaxError: Cannot use import statement outside a module
配置Babel(ESM转为CMJ)
// babel.config.js
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};
配置Nodejs
webpack/vite/rollup打包工具处理支持Jest使用ESM