aelbore/esbuild-jest

Lazy/Dynamic import causes jest worker to fail

AlonMiz opened this issue · 0 comments

  ● Test suite failed to run

    Call retries were exceeded

      at ChildProcessWorker.initialize (../../node_modules/jest-worker/build/workers/ChildProcessWorker.js:193:21)

making this part sync require fixes the issue
image

related to this thread: jestjs/jest#8769
doesn't happen in ts-jest.

were able to get more informed error by simplifying the case

Error
: 
    at invariant (/node_modules/jest-runtime/build/index.js:2004:11)
    at importModuleDynamically (/node_modules/jest-runtime/build/index.js:1362:11)
    at importModuleDynamicallyWrapper (node:internal/vm/module:448:21)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)

reproduce:
test.js

import {getModuleLazy} from './lazy'
describe('some test', () => {
  test("amazing test", async () => {
     await getModuleLazy()
  })
})

lazy.js

export const getModuleLazy = () => {
  return import('lodash').then(mod => {
    console.log(mod);
  });
};

UPDATE:
updated to jest 27, got this error, which is more informative:

Error: You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules
    at invariant (/node_modules/jest-runtime/build/index.js:2137:11)
    at importModuleDynamically (/node_modules/jest-runtime/build/index.js:1486:11)
    at importModuleDynamicallyWrapper (node:internal/vm/module:448:21)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)

were able to follow some of the instructions in the doc. after adding --experimental-vm-modules to the node jest command manually, the test passed.