【发布时间】:2022-10-21 17:02:18
【问题描述】:
我正在尝试使用 Typescript 和 Mocha 编写一些测试。
按照它的文档,我最终得到了以下设置:
包.json
{
//...
"scripts": {
"test": "mocha",
},
//...
}
.mocharc.json
{
"extension": ["test.ts"],
"spec": "tests/*.test.ts",
"require": "ts-node/register",
"recursive": true
}
tsconfig.json
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
"isolatedModules": true,
},
"files": [
"src/main/main.ts",
],
}
运行 npm test 会引发以下错误:TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [...]/tests/task.test.ts。
这是我唯一的测试,直到我导入单独的 .ts 文件 (../src/core/task) 之前它都有效,以确保完整性:
任务.test.ts
import { assert } from 'chai';
import { Task } from '../src/core/task';
describe('Task', () => {
it('Task Run', () => {
const task = new Task({
title: "My Title",
command: "echo hello",
path: "."
});
task.run();
})
});
我根据其他一些答案以及ts-mocha 尝试了我的配置的几种排列,但没有成功。
【问题讨论】:
-
在摆弄了一会儿之后,我注意到问题是
core/task正在导入的仅限 ESM 的库。为了使它工作,我似乎需要将项目移植到 ESM。不确定是否有更简单的选择
标签: node.js typescript mocha.js