"Maximum call stack size exceeded" when running cyclic ES6 imports in Jest

Viewed 687

While writing some test suites for some ES6 modules, I discovered that Jest v26.6 runs into errors when trying to do some cyclic imports.

The problem can be reduced to the following:

ClassA.mjs

import {B} from './ClassB.mjs';

class A {
    constructor()
    {
        //...
    }
}

ClassB.mjs

import {A} from './ClassA.mjs';

class B extends A {
    constructor()
    {
        super();

        // ...
    }
}

Cyclic.test.js

import {B} from "./ClassB.mjs";

describe('Cyclic', () => {
    test('Cyclic', () => {
        // ...
    });
});

Jest exits with a "Maximum call stack size exceeded" error message, at different places of its source, depending on the imports of the test.

Are there any solutions to this, any known workarounds?

Thanks in advance.

1 Answers

I was also seeing this with Jest version 26 using NODE_OPTIONS='--experimental-vm-modules'. When I updated to Jest version 27 (27.5.1), the errors went away.

With Jest version 26, my errors looked like:

RangeError: Maximum call stack size exceeded
Exception in PromiseRejectCallback:
internal/vm/module.js:321
        const module = await linker(identifier, this);
Related