diff --git a/babel.config.js b/babel.config.js index c29a1d2e44..90abe51c4d 100644 --- a/babel.config.js +++ b/babel.config.js @@ -35,7 +35,13 @@ module.exports = function (api) { // cannot use `env` field because it will put them after // the `react-native-worklets/plugin` plugin ...(api.env('test') - ? ['@babel/plugin-transform-class-static-block'] + ? [ + '@babel/plugin-transform-class-static-block', + // Compile `import()` to require so jest (which runs without + // `--experimental-vm-modules`) can execute lazily-loaded modules + // like `@ipld/dag-cbor` via its moduleNameMapper. + '@babel/plugin-transform-dynamic-import', + ] : []), ...(api.env('production') ? ['transform-remove-console'] : []), diff --git a/src/lib/api/computeCid.ts b/src/lib/api/computeCid.ts index edd23e5901..c5bbb9830e 100644 --- a/src/lib/api/computeCid.ts +++ b/src/lib/api/computeCid.ts @@ -123,14 +123,9 @@ function isPlainObject(v: any): boolean { /** * Load `@ipld/dag-cbor` on demand. The dynamic `import()` lets web bundlers * emit it (and its ~190KB `cborg` dependency) as a separate chunk that only - * loads when posting a thread. Under jest (which runs without - * `--experimental-vm-modules`) dynamic import throws, so we fall back to a - * lazy `require`, which resolves through the test moduleNameMapper. + * loads when posting a thread. Under jest this compiles down to a require + * that resolves through the test moduleNameMapper. */ function importDagCbor(): Promise { - if (process.env.NODE_ENV === 'test') { - // eslint-disable-next-line @typescript-eslint/no-require-imports - return Promise.resolve(require('@ipld/dag-cbor')) - } return import('@ipld/dag-cbor') }