正如标题所述,我在使用jest测试时遇到了问题,它不允许从
lowdb
包中导出。它似乎仅对这个单一的包抛出此错误 - 我的其余代码也使用ES6导出,并且我的package.json
文件有键type: module
。
我尝试过的方法
- 添加类型模块 - Jest:SyntaxError:Unexpected token 'export'
- 添加
transformIgnorePatterns: ["<rootDir>/node_modules/(?!lowdb)"]
- Jest设置"SyntaxError:意外的标记导出" - 添加一个使用
babel-jest
的js
转换器 - Jest遇到了意外的标记- SyntaxError:Unexpected token 'export' - 更新我的
tsconfig
以输出commonJS
模块 - 将我的
tsconfig
更新为allowJs:true
并更新我的jesttransforms
以使用ts-jest
解析JS
我不确定我错在哪里,如果我很迟钝,我提前道歉。
这是我的TS配置。{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"esModuleInterop": true,
"allowJs": true,
"target": "ES6",
"moduleResolution": "node",
"resolveJsonModule": true,
"module": "ES6",
"baseUrl": "src",
"declaration": true,
"allowSyntheticDefaultImports": true,
"paths": {
"@services/*": ["services/*"],
"@constants/*": ["constants/*"],
"@typeDefs/*": ["typeDefs/*"],
"@config/*": ["config/*"],
"@utils/*": ["utils/*"],
"@assets/*": ["assets/*"]
}
},
"include": ["src/**/*.ts"],
"exclude": ["rollup.config.ts", "jest.config.ts"]
}
这是我的jest.config.ts
文件:
import type { Config } from "@jest/types";
import { pathsToModuleNameMapper } from "ts-jest";
import { compilerOptions } from "./tsconfig.json";
// Sync object
const config: Config.InitialOptions = {
verbose: true,
roots: ["<rootDir>"],
preset: "ts-jest",
testEnvironment: "node",
transform: {
"^.+\\.ts$": "ts-jest",
"^.+\\.js$": "babel-jest",
},
testRegex: ["^.+\\.test\\.ts$"],
moduleDirectories: ["src", "node_modules"],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
};
export default config;
最后,我按照以下方式调用jest:
"test:ts": "jest --config=jest.config.ts",
提前感谢您。
编辑:一些附加信息
我很确定它不应该影响这个问题,但我想提供更多上下文可能有所帮助。我正在使用两个不同的配置运行jest——一个用于JS和另一个用于TS,而与之相关的存储库具有一些仅在nodeJS中运行的JS构建脚本。
JS配置如下(没有问题):
// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
verbose: true,
testEnvironment: "jest-environment-node",
transformIgnorePatterns: ["/node_modules/"],
transform: {},
testRegex: ["^.+\\.test\\.js$"],
};
export default config;
下面是调用的方式:
"test:js": "yarn node --experimental-vm-modules $(yarn bin jest) --config=jest.config.js",