【问题标题】:mocha test not recognizing *.d.ts declarationsmocha 测试无法识别 *.d.ts 声明
【发布时间】:2020-02-22 14:16:26
【问题描述】:

我在配置 mocha 以支持我的打字稿代码时遇到问题。我在项目根目录的src 文件夹中的global.d.ts 中定义了一些类型。

当我运行mocha -r ts-node/register test/**/*.ts 时,我收到以下错误:

TSError: ⨯ Unable to compile TypeScript:
src/mediator/index.ts:5:28 - error TS2304: Cannot find name 'DomainEvent'.

5     [key: string]: (event: DomainEvent) => Promise<void>
                             ~~~~~~~~~~~
src/mediator/index.ts:11:57 - error TS2304: Cannot find name 'DomainEvent'.

11     registerHandler(eventType: string, handler: (event: DomainEvent) => Promise<void>) {
                                                           ~~~~~~~~~~~
src/mediator/index.ts:18:37 - error TS2304: Cannot find name 'IntegrationEvent'.

18     async publish(integrationEvent: IntegrationEvent) {

当我运行ts-node ./src/app.ts 时,应用程序运行良好。有谁知道我做错了什么?

我的 tsconfig(我怀疑问题与此有关):

{
    "compilerOptions": {
        "module": "commonjs",
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "target": "es6",
        "noImplicitAny": false,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "@mediator": ["src/mediator"],
            "*": [
                "node_modules/*"
            ]
        },
        "typeRoots": [
            "./global"
        ]
    },
    "include": [
        "**/*.ts"
    ]
}

【问题讨论】:

    标签: node.js typescript mocha.js


    【解决方案1】:

    tsconfig.json 中包含您的global.d.ts

    {
      "compilerOptions": {
      },
      "include": [
        "./global.d.ts",
        "./src/**/*.d.ts"
      ]
    }
    

    【讨论】:

    • 这似乎不能解决我的问题。我仍然收到Property 'status' does not exist on type 'SyntaxError'.,我在 global.d.ts 中扩展了它,它在我运行节点时工作。
    • 最后我使用了ts-node: { files: true },但我还必须修复可能会修复您的示例的 global.d.ts。
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 2016-05-20
    • 2018-01-23
    • 2020-04-30
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多