我在Typescript中的Mocha测试遇到问题,我担心它与Babel有关,但是我真的不确定发生了什么。
基本上,我有一个正在文件中导出的功能
// src/my-ts-lib/tests/components/factoryMocks/componentConfigMocks.ts
...
export function getRandomConfig(type?: string): ComponentConfig {
const randomComponentType = type || randomType();
return {
type: randomComponentType,
config: configLibrary[randomComponentType]
}
}
然后导入到另一个中,由测试调用:
// src/my-ts-lib/tests/components/RouteComponent/mocks/routeMocks.ts
...
import { getRandomConfig } from '../../factoryMocks/componentConfigMocks';
..
export const getSingleRouteConfigMock = (componentType?: string): RouteProps => {
const defaultComponentType = 'PageLayout';
return {
childComponent: {
type: componentType || defaultComponentType,
config: getRandomConfig(componentType || defaultComponentType)
},
pageName: randomString(),
path: randomString(),
};
};
...
运行测试时,出现以下错误:
RouteCompnent/mocks/routeMocks.ts:10
config: getRandomConfig(componentType || defaultComponentType),
^
TypeError: componentConfigMocks_1.getRandomConfig is not a function
at Object.exports.getSingleRouteConfigMock (/Users/.../routeMocks.ts:10:44)
如果我对呼叫和console.log(getRandomConfig)
进行评论,则可以看到它是undefined
。我不知道为什么会这样。甚至更奇怪的是,在随后的调用getSingleRouteConfigMock
的测试中,相同的console.log
正确地输出了该函数,意味着该函数已被导出。
我摆弄Babel,Mocha和Typescript配置,但没有成功。
这是Babel配置:
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
Mocha配置:
mocha.opts
--require ts-node/register
--watch-extensions ts tsx
--require source-map-support/register
--recursive
--require @babel/register
--require @babel/polyfill
src/**/*.spec.**
和Typescript配置:
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": false,
"module": "commonjs",
"target": "es6",
"jsx": "react"
},
"include": [
"./src/**/*"
],
"exclude": [
"./src/**/*.spec.ts",
"./src/my-ts-lib/components/**/*.spec.tsx",
"./src/my-ts-lib/test-helpers/*"
],
}
以及package.json
...
"dependencies": {
...
"@babel/polyfill": "7.2.x",
...
},
"devDependencies": {
"@babel/core": "7.2.x",
"@babel/preset-env": "7.2.x",
"@babel/preset-react": "7.0.x",
"@babel/register": "7.0.x",
"babel-loader": "8.x",
"mocha": "3.2.x",
...
}
答案 0 :(得分:1)
我发现我有一个循环依赖。这就是为什么它不起作用。