【问题标题】:Using Mocha with TypeScript paths将 Mocha 与 TypeScript 路径一起使用
【发布时间】:2017-08-31 17:07:54
【问题描述】:

在 TypeScript 中使用路径 + baseUrl 配置时,我在运行 mocha 时遇到问题

我的 tsconfig.js 是这样设置的:

"baseUrl": "./src/", /* Base directory to resolve non-absolute module names. */
"paths": {
  "interfaces/*": [
    "interfaces/*"
  ],
  "models/*": [
    "models/*"
  ],
  "schemas/*": [
    "schemas/*"
  ],
  "classes/*": [
    "classes/*"
  ],
  "libs/*": [
    "libs/*"
  ],
  "config/*": [
    "config/*"
  ]

我将 mocha 作为“mocha build/test”运行

编译后的 TS 代码无法找到我的引用,因为它编译为

var user_1 = require("interfaces/user");

如果我事先添加“../”,它将毫无问题地编译

有什么想法我在这里做错了吗?

【问题讨论】:

    标签: typescript testing path compilation mocha.js


    【解决方案1】:

    您可以使用tsconfig-paths,按照tsconfig-paths上的说明操作:

    mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"
    

    【讨论】:

    • 这是给我的
    • 这修复了我的错误,即Cannot find module ... at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/app/node_modules/@cspotcode/source-map-support/source-map-support.js:679:30)
    【解决方案2】:

    您应该使用 mocha-TypeScript 集成包,例如 ts-mocha

    【讨论】:

      【解决方案3】:

      您应该使用普通的测试集成包(最流行的)。例如 moch 打字稿。 这样,您只需要在 package.json 中的 test 中进行设置

      script: 'mocha --ui mocha-typescript test.ts'
      

      https://www.npmjs.com/package/mocha-typescript

      我会给你一个更好的例子——test.ts

      import { suite, test, slow, timeout } from "mocha-typescript";
      @suite class Hello {
          @test world() {
              assert.equal(1, 2, "Expected one to equal two.");
          }
      }
      

      【讨论】:

      • mocha-typescript 现已弃用,请尝试 Eliran Pe'er 的回答中提到的包
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 2011-08-07
      • 2015-11-03
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多