0

Historically I've just used the transformIgnorePatterns option in Jest to convert esmodule libraries from esmodules to commonjs:

module.exports = {
    transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!my-library)'
    ]
}

However this adds a lot of extra compile time, especially since I have a bunch of libraries in these ignore patterns now.

I'm trying to avoid needing to transform esmodules to commonjs. I'm using Jest & SWC to try and do this.

Here is my Jest config:

import fs from 'fs';

const swcConfig = JSON.parse(fs.readFileSync('./.swcrc', 'utf8'));

export default {
    testEnvironment: 'jsdom',
    transform: {
        '^.+\.jsx?$': [
            '@swc/jest',
            swcConfig
        ]
    }
};

And my package.json is setup the following way:

{
    "type": "module",
    "scripts": {
        "test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
    }
}

This seems to be enough to add support for local esmodule code. However, library code still fails. The following example is a super simple test I've created for this. I'm just trying to make an import from a library whose code is in esmodules work:

import { MyComponent } from 'my-library';

describe('My Test', () => {
    it('can import successfully', () => {
        expect(MyComponent).not.toBeNull();
    });
});

The import from my-library will fail with a Jest encountered an unexpected token error, referring to the export line at the top of the index file in the library.

Is there some way to make this work without transformIgnorePatterns?

halfer
  • 19,824
  • 17
  • 99
  • 186
craigmiller160
  • 5,751
  • 9
  • 41
  • 75
  • 1
    Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Jul 12 '23 at 10:57

0 Answers0