我想为Node / Express / Typescript应用程序编写测试。在我的 src 目录旁边,添加了一个带有该演示内容的 app.spec.ts 文件的 tests 目录
import { App } from '../src/app';
describe('App', () => {
let instance: App;
beforeEach(() => {
instance = new App();
});
it('creates an instance of App', async () => {
expect(instance).toBeInstanceOf(App);
});
});
不幸的是,杰斯特(Jest)无法处理打字稿。
详细信息:
C:... \ tests \ app.spec.ts:1 ({“宾语。 “:function(module,exports,require,__ dirname,__ filename,global,jest){import {App} from'../ src / app';
SyntaxError:意外令牌{ 在ScriptTransformer._transformAndBuildScript(node_modules/@jest/transform/build/ScriptTransformer.js:471:17) 在ScriptTransformer.transform(node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
这是我的package.json(相关字段)
{
"scripts": {
"start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"build": "tsc -p .",
"test": "jest",
"testWithCoverage": "jest --coverage"
},
"devDependencies": {
"@types/express": "^4.17.0",
"@types/jest": "^24.0.17",
"@types/node": "^12.6.9",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.2",
"jest": "^24.8.0",
"nodemon": "^1.19.1",
"ts-jest": "^24.0.2",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
}
}
我还用该内容创建了一个 jest.config.js 文件
module.exports = {
roots: [
'./tests',
],
moduleFileExtensions: [
'ts',
'tsx',
'js',
'jsx',
],
};
有人知道这里缺少什么吗?
答案 0 :(得分:2)
将您的jest.config.js
文件更改为:
module.exports = {
"roots": [
"<rootDir>/src",
"<rootDir>/tests"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}