Edg*_*sos 1 javascript mocha.js commonjs typescript babeljs
我在 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]
}
}
Run Code Online (Sandbox Code Playgroud)
并被导入到另一个被测试调用的地方:
// 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(),
};
};
...
Run Code Online (Sandbox Code Playgroud)
运行测试时,我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
如果我评论这个电话,console.log(getRandomConfig)我可以看到它是undefined. 我不知道为什么会这样。更奇怪的是,在调用 的后续测试中getSingleRouteConfigMock,这同样console.log正确地输出了函数,这意味着它已经被导出了。
我摆弄过 Babel、Mocha 和 Typescript 配置,但没有成功。
这是 Babel 配置:
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
Run Code Online (Sandbox Code Playgroud)
摩卡配置:
mocha.opts
--require ts-node/register
--watch-extensions ts tsx
--require source-map-support/register
--recursive
--require @babel/register
--require @babel/polyfill
src/**/*.spec.**
Run Code Online (Sandbox Code Playgroud)
和打字稿配置:
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/*"
],
}
Run Code Online (Sandbox Code Playgroud)
以及相关章节 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",
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
968 次 |
| 最近记录: |