在 mocha 测试中使用带有 ts-node 的断言

using assert with ts-node in mocha test

我在 test.ts

中创建了一个 mocha 测试
import * as assert from "assert";

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

我的package.jsondevDependencies:

"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

常规 tsc 不会抛出任何错误。

通过 this issue comment 在 GitHub 上解决了它:

typestsconfig.json 需要包含 node:

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