0

I am using Jest to run some tests and have come across an issue with one of the modules I am using. The issue is that I am trying to import a module that is not compiled down when built. What I mean by not compiled down is that the imports are left at the top of the file I am trying to import into another. Here is what I mean:

jest.mock('@my-org/my-package/dist/browser');
import * as NTracking from '@my-org/my-package/dist/browser';
import generateStubs from '../test-jest/fixtures/client-stubs';

import testedController from './controller';

describe('client controller b', () => {
    const stubs = generateStubs();
    let mockWindow;

    beforeEach(() => {
        mockWindow = stubs.mockWindow;
        NTracking.mockImplementation(() => stubs.nTracking);
    });

    afterEach(() => {
        jest.clearAllMocks();
    });

    it('should construct', () => {
        new testedController(mockWindow);
    });
});

The issue I have is on line 2 where I am trying to import NTracking from @my-org/my-package/dist/browser. The file I am trying to import looks like this:

browser.js

import somePackage from 'some-packge/some-function';
import someOtherPackage from 'some-other-packge/some-function';

When running the test I get the following error:

SyntaxError: Unexpected identifier

This is due to the word import obviously not being a native javascript definition.

My question is this - how could I configure Jest to compile the problematic module (my-package) before running the tests?

red house 87
  • 1,837
  • 9
  • 50
  • 99

1 Answers1

0

Are you sure the issue is with the import statement? I had this kind of issue and the error message was:

SyntaxError: Cannot use import statement outside a module

It appears that support for ECMAScript Modules is still experimental, you would probably have better success by transforming your modules with babel before the tests, see the discussion here: Does Jest support ES6 import/export?

raaaahman
  • 80
  • 11