在Mocha测试中使用带有ts-node的assert

时间:2019-01-26 02:08:20

标签: node.js typescript mocha

我在mocha中创建了test.ts测试

import * as assert from "assert";

describe('it', () => {
  it('should ', done => {
    assert.strictEqual(true, false);
    done();
  });
});

我的package.json的{​​{1}}:

devDependencies

"devDependencies": { "@types/mocha": "^5.2.5", "@types/node": "^10.12.18", "mocha": "^5.2.0", "ts-node": "^8.0.1", "typescript": "^3.2.4" }

tsconfig.json

运行{ "compilerOptions": { "target": "es2015", "module": "commonjs", "sourceMap": true, "declaration": true, "outDir": "./dist", "strict": true, "types": ["mocha"] } } 时,出现此错误:

ts-node

我的命令行调用是这样:

error TS2307: Cannot find module 'assert'.

常规./node_modules/mocha/bin/mocha -r ts-node/register test/test.ts不会引发错误。

1 个答案:

答案 0 :(得分:0)

通过GitHub上的this issue comment解决了该问题:

types中的

tsconfig.json必须包含node

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "types": ["mocha", "node"]
  }
}