gpt4 book ai didi

typescript - 如何在 Jest 测试文件中使用从 typescript 文件导入模块

转载 作者:搜寻专家 更新时间:2023-10-30 22:00:29 26 4
gpt4 key购买 nike

我使用 jest 来测试我的 typescript 代码。

import ClassA from '../classA';
jest.mock('../classA');

当我从我的 classA.ts 文件导入一个时,jest 给我一个错误:

export default ClassA;
^^^^^^
SyntaxError: Unexpected token export

这是我在 package.json 中的 jest 配置:

"devDependencies": {
"@types/jest": "^19.2.3",
"jest": "^20.0.4",
"ts-jest": "^20.0.4",
"ts-node": "^3.0.2",
"typescript": "^2.3.2"
},
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}

最佳答案

{
"compilerOptions": {
"types": ["vue-typescript-import-dts"],
}
}
import Child = require('./child.vue')
import * as Child from './child.vue'
npm install vue-typescript-import-dts --save-dev

关于typescript - 如何在 Jest 测试文件中使用从 typescript 文件导入模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44172231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com