简体   繁体   English

..._1.default 不是使用 Mocha 测试 TypeScript 时的构造函数

[英]..._1.default is not a constructor when testing TypeScript with Mocha

I'm trying to get unit tests with mocha to work.我正在尝试使用 mocha 进行单元测试。 I'm using typescript which gets compiled down to plain javascript with tsc.我正在使用打字稿,它被编译成带有 tsc 的普通 javascript。 I'm always getting the error:我总是收到错误:

    src\index.ts:22
        [new FrontendEndpoint(), ...],
         ^
    TypeError: v1_1.default is not a constructor

I approached two ways (and ran into the same problem twice):我采用了两种方法(并两次遇到相同的问题):

First I created a dummy test test.test.ts , importing some of my modules for testing purposes:首先,我创建了一个虚拟测试test.test.ts ,导入我的一些模块用于测试目的:


    import { APIServer } from './../api/index';
    import { describe } from 'mocha';
    import FrontendEndpoint from '../api/endpoints/frontend/v1';
    import { SocketConnector } from '../api/sockets/socketio';

    describe('TestTest', () => {
        it('should run', (done) => {
            const server = new APIServer(4000, [new FrontendEndpoint()], new SocketConnector([]));
            done();
        });
    });

  1. Using ts-mocha使用 ts-mocha

    • Installed ts-mocha, mocha, @types/mocha安装 ts-mocha、mocha、@types/mocha
    • Ran ts-mocha src/test/test.test.tsts-mocha src/test/test.test.ts
  2. Using mocha & compiled ts files使用 mocha 和编译的 ts 文件

    • Installed mocha, @types/mocha安装 mocha, @types/mocha
    • Ran mocha build/test/test.test.jsmocha build/test/test.test.js

Both ways produce the error above.两种方式都会产生上述错误。

index.ts looks like this: index.ts看起来像这样:


    import FrontendEndpoint from './api/endpoints/frontend/v1';
    [...]
    new FrontendEndpoint()

Compiled (index.js):编译(index.js):


    [...]
    const v1_1 = require("./api/endpoints/frontend/v1");
    [...]
    new v1_1.default()

And frontend/v1.ts :frontend/v1.ts


    export default class FrontendEndpoint {
        [...]
    }

Compiled (v1.js):编译(v1.js):


    class FrontendEndpoint {
        [...]
    }
    exports.default = FrontendEndpoint;

My tsconfig looks like this:我的 tsconfig 看起来像这样:

{
    "compilerOptions": {
        "target": "es2015",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "strictPropertyInitialization": false,
        "experimentalDecorators": true,
        "typeRoots": [
            "src/types"
        ],
        "emitDecoratorMetadata": true,
        "sourceRoot": "src",
        "outDir": "build"
    },
    "compileOnSave": true,
    "exclude": [
        "node_modules",
        "coverage",
        "build",
        "logs"
    ],
}

It seems to only have issues with default exports.似乎只有默认导出有问题。 Why don't they work as expected?为什么它们不能按预期工作? When running the app using node build/index.js everything is working fine, the default exports/imports work as expected.使用node build/index.js运行应用程序时,一切正常,默认导出/导入按预期工作。

I'm having the same issues when trying to add unit tests to my frontend React app with Webpack, with Mocha and with Jest.在尝试使用 Webpack、Mocha 和 Jest 向前端 React 应用程序添加单元测试时,我遇到了同样的问题。 Do I miss something completely?我是否完全错过了什么?

I found the solution myself.我自己找到了解决方案。

While debugging my tests I discovered that some of the exports are not being called.在调试我的测试时,我发现一些导出没有被调用。 This is due to cyclic dependencies of the files, that prevented them from being exported correctly.这是由于文件的循环依赖关系导致它们无法正确导出。

After finding these cycles using https://github.com/pahen/madge and resolving them, running tests is working just fine.使用https://github.com/pahen/madge找到这些循环并解决它们后,运行测试工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM