【发布时间】:2017-09-28 22:49:03
【问题描述】:
我将源代码和测试分开如下:
`src/main/ts/hello.ts` //SOURCE FILES HERE
`src/test/ts/hello.spec.ts` //SPEC FILES HERE
src/test/ts/hello.spec.ts 中的 import 语句如下所示:
import hello from 'hello';
hello.ts 源代码如下所示:
export function hello() {
return 'Hello World!';
}
export default hello;
我的tsconfig.json 设置为测试文件可以在不使用相对路径的情况下导入源模块,如下所示:
{
"include": [
"src/main/ts/**/*.ts"
],
"exclude": [
"node_modules"
],
"compilerOptions": {
"experimentalDecorators": true,
"noImplicitAny": true,
"moduleResolution": "node",
"target": "es6",
"baseUrl": ".",
"paths": {
"*": [
"*", "src/main/ts/*"
]
}
}
}
这样hello.spec.ts 文件可以使用语句import hello from 'hello'; 导入hello
我正在尝试使用 npm test 运行测试,配置为像这样运行 mocha 和 tsnode(基于 this article):
"scripts": {
"test": "mocha -r ts-node/register src/test/ts"
},
但是,当我收到此错误时,似乎 ts-node 并没有在我的 tsconfig.json 配置上运行:
mocha -r ts-node/register src/test/ts
Error: Cannot find module 'hello'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:286:25)
【问题讨论】:
标签: node.js typescript mocha.js typescript2.0 ts-node