我在 TypeScript 项目中运行 Jest 时遇到问题。我有一个位于 rootDir/src/_services/index.ts 的 TypeScript 文件,Jest 抛出以下错误:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './auth.service'; ^^^^^^
SyntaxError: Unexpected token 'export'
index.ts 的内容包含语句
export * from './auth.service';
,看来 Jest 无法解析它。
export * from './auth.service'
我正在使用自定义 Jest 配置和 ts-jest。这是我的 Jest 配置文件中的片段
const createJestConfig = nextJest({
dir: './',
});
const customJestConfig = {
// ... other configurations ...
transformIgnorePatterns: [
'/node_modules/',
'/@bufbuild/protobuf/',
'/src/_services/index\\.ts$', // Ignore this path
],
// ... other configurations ...
};
module.exports = createJestConfig(customJestConfig);```
这就是您在此处包含第三个元素('/src/_services/index\.ts$')的原因吗?:
transformIgnorePatterns: [
'/node_modules/',
'/@bufbuild/protobuf/',
'/src/_services/index\\.ts$', // delete this!
],
因为这意味着您的文件 /src/_services/index.ts 不会被任何工具转换并保留其 ts 语法。