TypeScript 无法在 Jest 的上下文中生成对象
TypeScript failing to generate objects in the context of Jest
我正在尝试通过 Jest 测试我的 React 应用程序。当我 运行 以下命令对 运行 我的测试时, jest
我收到以下错误:
Debug Failure. False expression: Output generation failed
1 | import * as TestData from 'TestModule';
2 |
> 3 | export class TestDataEmitter {
4 | constructor() { }
5 | public emit() {
6 | return TestData.data;
at Object.transpileModule (node_modules/typescript/lib/typescript.js:93386:18)
at Object.<anonymous> (component.ts:3:67)
这是我的目录结构:
| component.ts
| package.json
| tree.txt
| tsconfig.json
| typings.d.ts
| yarn-error.log
| yarn.lock
|
\---test
test.ts
相关文件:
typings.d.ts
declare interface TestInterface {
id: number;
data: string;
}
declare module 'TestModule' {
const testData: TestInterface;
export = testData;
}
component.ts
import * as TestData from 'TestModule';
export class TestDataEmitter {
constructor() { }
public emit() {
return TestData.data;
}
}
test/test.ts
import * as TestData from 'TestModule';
export class TestDataEmitter {
constructor() { }
public emit() {
return TestData.data;
}
}
package.json
{
"devDependencies": {
"@types/jest": "^22.2.3",
"jest": "^22.4.3",
"ts-jest": "^22.4.2",
"typescript": "^2.8.1"
},
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\.tsx?$": "ts-jest"
},
"testRegex": "tests?(?:\|/)(?:(?!helpers?).*(?:\|/))*.*[Tt]ests?.*\.tsx?",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"moduleNameMapper": {
"TestModule": "<rootDir>/typings.d.ts"
}
}
}
如果我删除 moduleNameMapper
对象,我将得到 "Cannot find module 'TestModule' from 'component.ts'"。
这是一个包含上述文件的 git 存储库:https://github.com/morganthrapp/jest-mcve
您只有 TestModule
的类型,但没有该模块的实现。 moduleNameMapper 应该用于添加模块的存根。
emit 函数无法找到 TestModule 的实现。创建一个空文件 testModule
并将映射器配置更改为
"TestModule": "<rootDir>/testModule.ts".
现在测试将解析模块,由于文件为空,TestModule.data
将是 undefined
。
如果我误解了这个问题,我深表歉意。
我正在尝试通过 Jest 测试我的 React 应用程序。当我 运行 以下命令对 运行 我的测试时, jest
我收到以下错误:
Debug Failure. False expression: Output generation failed
1 | import * as TestData from 'TestModule';
2 |
> 3 | export class TestDataEmitter {
4 | constructor() { }
5 | public emit() {
6 | return TestData.data;
at Object.transpileModule (node_modules/typescript/lib/typescript.js:93386:18)
at Object.<anonymous> (component.ts:3:67)
这是我的目录结构:
| component.ts
| package.json
| tree.txt
| tsconfig.json
| typings.d.ts
| yarn-error.log
| yarn.lock
|
\---test
test.ts
相关文件:
typings.d.ts
declare interface TestInterface {
id: number;
data: string;
}
declare module 'TestModule' {
const testData: TestInterface;
export = testData;
}
component.ts
import * as TestData from 'TestModule';
export class TestDataEmitter {
constructor() { }
public emit() {
return TestData.data;
}
}
test/test.ts
import * as TestData from 'TestModule';
export class TestDataEmitter {
constructor() { }
public emit() {
return TestData.data;
}
}
package.json
{
"devDependencies": {
"@types/jest": "^22.2.3",
"jest": "^22.4.3",
"ts-jest": "^22.4.2",
"typescript": "^2.8.1"
},
"scripts": {
"test": "jest"
},
"jest": {
"transform": {
"^.+\.tsx?$": "ts-jest"
},
"testRegex": "tests?(?:\|/)(?:(?!helpers?).*(?:\|/))*.*[Tt]ests?.*\.tsx?",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"moduleNameMapper": {
"TestModule": "<rootDir>/typings.d.ts"
}
}
}
如果我删除 moduleNameMapper
对象,我将得到 "Cannot find module 'TestModule' from 'component.ts'"。
这是一个包含上述文件的 git 存储库:https://github.com/morganthrapp/jest-mcve
您只有 TestModule
的类型,但没有该模块的实现。 moduleNameMapper 应该用于添加模块的存根。
emit 函数无法找到 TestModule 的实现。创建一个空文件 testModule
并将映射器配置更改为
"TestModule": "<rootDir>/testModule.ts".
现在测试将解析模块,由于文件为空,TestModule.data
将是 undefined
。
如果我误解了这个问题,我深表歉意。