syy
syy

Reputation: 807

Require export default module in test files

I am trying to require a module in my test files, but can't figure out how to do it when I export it as a default module. I have the following code:

server.ts

import { MyClass } from './myClass';

/* Other code here */

const server = app.listen(port, () => {
    console.log('Started');
});

export default server;

This builds perfectly fine using webpack. I export the server because I want to be able to test it in my spec files. I tried doing this in my test file (using mocha):

testFile.ts

describe('Express Server', () => {
    let server: any;

    beforeEach(() => {
        delete require.cache[require.resolve('./server.ts')];
        server = require('./server.ts');
    });

    afterEach((done: any) => {
        server.close(done);
    });

    it('sample test', (done: any) => {
        done();
    });
});

Of course, the above is not testing anything. But it doesn't matter since it throws an error in the before each: SyntaxError: Unexpected token {

How can I require my server module? I need to be able to reset the require before each test.

Upvotes: 1

Views: 391

Answers (2)

syy
syy

Reputation: 807

So it turns out I had to change the module defined in tsconfig.json. I changed it to "commonjs" from "es2015" and the error posted in the question went away. In addition, I also had to change how I retrieved the server as posted below (added .default after the require).

tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist",
        "target": "es5",
        "module": "commonjs", // Had to change the value for this
        "sourceMap": true
    },
    "include": [
        "./src/**/*"
    ],
    "exclude": [
        "./dist",
        "./node_modules"
    ]
}

testFile.ts

describe('Express Server', () => {
    let server: any;

    beforeEach(() => {
        delete require.cache[require.resolve('./server.ts')];

        // Had to change this line to have ".default" at the end
        server = require('./server.ts').default;
    });

    afterEach((done: any) => {
        server.close(done);
    });

    it('sample test', (done: any) => {
        done();
    });
});

I'm not fully educated on the differences between "es2015" and "commonjs", so if someone knows, please share. Using the above changes, I was able to run the following command:

nyc ts-mocha -p tsconfig.json --reporter mocha-multi-reporters --reporter-options configFile=test/config/mocha-config.json test/**/*.test.ts --exit

and it pulled in the server and all my tests pass without any errors. Thank you Freez for mentioning the test environment might not be reading the typescript correctly.

Upvotes: 0

Freez
Freez

Reputation: 7588

Your test environment seems to not support TypeScript files.

Try to run the mocha command using ts-node

mocha -r ts-node/register src/**/test.ts

source: https://journal.artfuldev.com/write-tests-for-typescript-projects-with-mocha-and-chai-in-typescript-86e053bdb2b6

Upvotes: 1

Related Questions