Search code examples
node.jsexpressunit-testingjestjs

Jest, ES6 modules "does not provide export named"


I have an express app with a CRUD API (with sequelize) and I want to test it with Jest. I'm pretty new in unit-testing so I follow this guide, recommended by Jest's website.

The problem I have is that my app is built with ES6 modules and Jest ES6 modules is experimental and it seems that it doesn't "import" packages.

I have this test (took from the guide)

import request from 'supertest';
import app from '../app';

describe('Test the root path', () => {
    test('It should response the GET method', done => {
        request(app)
            .get('/')
            .then(response => {
                expect(response.statusCode).toBe(404);
                done();
            });
    });
});

And when I launched it (with NODE_OPTIONS=--experimental-vm-modules npx jest I had to follow this jest wiki page), It says that

'sequelize' does not provide an export named 'DataTypes' and when I launch my app normally (like with npm start) it works fine, without any problems.

(the complete error log):

(node:49576) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  __tests__/app_test.js
  ● Test suite failed to run

    SyntaxError: The requested module 'sequelize' does not provide an export named 'DataTypes'

      at Runtime.linkAndEvaluateModule (node_modules/jest-runtime/build/index.js:779:5)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

(and my Jest config)

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */

export default async () => {
    return {
        verbose: true,
        transform: {},
    };
};

Am I doing something wrong ? Should I change to commonJS instead of ES6

Thank you.


Solution

  • This is a known problem in Jest: #9771. It is said to be fixed in [email protected].

    An interesting hack to work around this problem is to remove the main field from the package.json of the imported project.