admin管理员组

文章数量:1391999

I'm trying to get unit tests with mocha to work. I'm using typescript which gets piled down to plain javascript with tsc. 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:


    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

    • Installed ts-mocha, mocha, @types/mocha
    • Ran ts-mocha src/test/test.test.ts
  2. Using mocha & piled ts files

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

Both ways produce the error above.

index.ts looks like this:


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

Compiled (index.js):


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

And frontend/v1.ts:


    export default class FrontendEndpoint {
        [...]
    }

Compiled (v1.js):


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

My tsconfig looks like this:

{
    "pilerOptions": {
        "target": "es2015",
        "module": "monjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "strictPropertyInitialization": false,
        "experimentalDecorators": true,
        "typeRoots": [
            "src/types"
        ],
        "emitDecoratorMetadata": true,
        "sourceRoot": "src",
        "outDir": "build"
    },
    "pileOnSave": 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.

I'm having the same issues when trying to add unit tests to my frontend React app with Webpack, with Mocha and with Jest. Do I miss something pletely?

I'm trying to get unit tests with mocha to work. I'm using typescript which gets piled down to plain javascript with tsc. 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:


    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

    • Installed ts-mocha, mocha, @types/mocha
    • Ran ts-mocha src/test/test.test.ts
  2. Using mocha & piled ts files

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

Both ways produce the error above.

index.ts looks like this:


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

Compiled (index.js):


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

And frontend/v1.ts:


    export default class FrontendEndpoint {
        [...]
    }

Compiled (v1.js):


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

My tsconfig looks like this:

{
    "pilerOptions": {
        "target": "es2015",
        "module": "monjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "noImplicitReturns": true,
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "strictPropertyInitialization": false,
        "experimentalDecorators": true,
        "typeRoots": [
            "src/types"
        ],
        "emitDecoratorMetadata": true,
        "sourceRoot": "src",
        "outDir": "build"
    },
    "pileOnSave": 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.

I'm having the same issues when trying to add unit tests to my frontend React app with Webpack, with Mocha and with Jest. Do I miss something pletely?

Share Improve this question edited Jan 11, 2019 at 13:24 Fabian S. asked Jan 11, 2019 at 13:03 Fabian S.Fabian S. 611 silver badge6 bronze badges 5
  • Someone else mentioned problems with default imports and Jest just the other day... Hmmm. – T.J. Crowder Commented Jan 11, 2019 at 13:05
  • "And the frontend/v1.ts: ... exports.default = FrontendEndpoint;" Apologies if the answer is obvious, I'm not a big TypeScript guy, but why not export default FrontendEndpoint;? You're using ESM syntax for the import, but CommonJS syntax for the export...? – T.J. Crowder Commented Jan 11, 2019 at 13:07
  • I may have made this a bit unclear. This is javascript piled from typescript. It's how the typescript piler handles default exports. – Fabian S. Commented Jan 11, 2019 at 13:15
  • You said *"And the frontend/v1.ts: followed by code. So naturally I assumed that was code in frontend/v1.ts. With the edit, I have no idea what that code is. Would you please show both the TypeScript for the export and the JavaScript, clearly labelled (as you did with index.ts). – T.J. Crowder Commented Jan 11, 2019 at 13:18
  • Sure, sorry. I added the source code. – Fabian S. Commented Jan 11, 2019 at 13:25
Add a ment  | 

1 Answer 1

Reset to default 6

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./pahen/madge and resolving them, running tests is working just fine.

本文标签: javascript1default is not a constructor when testing TypeScript with MochaStack Overflow