我已經源代碼和檢驗分離如下:運行測試位於一個單獨的目錄與摩卡和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)