我在与使用时TS-节点用于测试使用摩卡将env变量TS_NODE_PROJECT
麻烦。
项目结构如下所示:
src/
main_test.ts
tsconfig.json
package.json
在我的测试,我想用一个异步功能,这需要"lib": ["es2018"]
的编译选项。
// src/main_test.ts
describe('', () => {
it('test', () => {
(async function() {})()
});
});
// src/tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"lib": ["es2018"]
},
"exclude": [
"../node_modules"
]
}
要运行测试,我用这个命令,但它导致一个错误:
TS_NODE_PROJECT='src' && mocha --require ts-node/register src/*_test.ts
# TSError: ⨯ Unable to compile TypeScript:
# error TS2468: Cannot find global value 'Promise'.
# src/main_test.ts(3,10): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
这意味着,src/tsconfig.json
不被使用。根据Overriding `tsconfig.json` for ts-node in mocha
和TS-节点文档中,命令应该传递正确tsconfig.json
路径TS-节点。
移动src/tsconfig.json
到项目目录,并运行相同的命令使试验取得成功。我怎样才能通过tsconfig.json
路径TS-节点,以便测试编译正确?
哦。多么尴尬...
TS_NODE_PROJECT='src/tsconfig.json' mocha --require ts-node/register src/*_test.ts