【发布时间】:2021-06-02 12:40:37
【问题描述】:
jest 无法导入我的导入,这导致npm run test 命令在我的bar.ts 文件的第一行出现SyntaxError: Unexpected token 'export' 失败。对于此示例,foo.js 是本地文件,而不是节点模块。我可以将foo.js 更改为foo.ts 并更改bar.ts 中的导入,但这不是解决方案。
我的 src 文件夹中的文件:
-
foo.js:
export const magicNumber = 42;
-
bar.ts:
import { magicNumber } from './foo.js';
export const secondMagicNumber = magicNumber / 2;
-
bar.test.ts:
import { secondMagicNumber } from './bar';
import assert from 'assert';
it('should work', function() {
assert.strictEqual(secondMagicNumber, 21);
});
根文件夹中的文件:
-
jest.config.js:
export default {
preset: 'ts-jest',
testEnvironment: 'node',
};
-
package.json:
{
"name": "testing-with-jest",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^26.0.20",
"jest": "^26.6.3",
"ts-jest": "^26.5.2",
"typescript": "^4.2.2"
},
"type": "module"
}
-
tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"moduleResolution": "node"
},
"include": ["./src/**/*.ts"]
}
【问题讨论】:
标签: javascript node.js typescript jestjs ts-jest