Jest tests started failing with TypeError: A dynamic import callback was invoked without --experimental-vm-modules after recent React Native upgrade
Asked Answered
H

0

7

I've been trying to resolve this test error for a while now with not success. Recently I upgraded React Native from 0.72.3 to 0.74.3 and got the Android application to build and run, however, I can't get all the tests to pass. I am currently getting the following error:

TypeError: A dynamic import callback was invoked without --experimental-vm-modules

Inside my icons.ts file which dynamically lazy loads icons the following way:

const Icons = {
    sun: lazy(() => import('@src/assets/icons/sun.svg')),
    moon: lazy(() => import('@src/assets/icons/moon.svg')),
    cloud: lazy(() => import('@src/assets/icons/cloud.svg')),
}

The tests were passing fine before, and now they no longer pass. I believe adding --experimental-vm-modules solves that specific error, but causes other errors and from what I've read, using that flag is not advised. Is there anything else I can do to resolve this issue?

Homicide answered 29/7, 2024 at 10:9 Comment(1)
I'm having a similar problem and I've not found a solution yet. I have a NestJS project that transpiles TypeScript to CommonJS, but I have a dependency that's an ES module. If I change it from CommonJS to anything else, NestJS breaks. So the only way to import an ES module is to use a dynamic import. Unfortunately, Jest doesn't like that and I'm getting the same error as you. I haven't solved it yet, but I'm starting to believe that I will have to mock out the dynamic import, so that it doesn't happen. The whole ESM thing is such a mess. The lack of interoperability with CommonJS is shocking.Bondage

© 2022 - 2025 — McMap. All rights reserved.