我已经源代码和检验分离如下:运行测试位于一个单独的目录与摩卡和TS节点?
`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
设置为使得测试文件可以导入源模块而不使用相对pa部份是这样的:
{
"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';
我试图用npm test
配置为运行摩卡和tsnode像这样运行测试的语句来导入hello
(基于this article) :
"scripts": {
"test": "mocha -r ts-node/register src/test/ts"
},
但是它看起来并不像TS-节点拿起我的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)