Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading a font using expo-font fails in jest unit tests: Module is missing from the asset registry #21434

Open
coffeebeats opened this issue Feb 28, 2023 · 10 comments

Comments

@coffeebeats
Copy link

Minimal reproducible example

https://github.com/coffeebeats/example-expo-asset-registry-bug

Summary

After upgrading from expo-asset@8.7.0 to expo-asset@8.8.0, when trying to load a font via expo-font in a jest unit test, the font asset module is found to be missing from the react-native asset registry. The error is provided below, with the path to the example repository replaced with <rootDir>:

NOTE: This issue reproduces in both expo@48 and expo@47, provided you downgrade the relevant packages (e.g. downgrade expo-font to 11.0.1).

Error: Module "1" is missing from the asset registry
        at Function.fromModule (<rootDir>/node_modules/expo-asset/src/Asset.ts:148:13)
        at fromModule (<rootDir>/node_modules/expo-font/src/FontLoader.ts:29:18)
        at <rootDir>/node_modules/expo-font/src/Font.ts:130:34
        at Generator.next (<anonymous>)
        at asyncGeneratorStep (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
        at _next (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)
        at <rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7
        at new Promise (<anonymous>)
        at <rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12
        at apply (<rootDir>/node_modules/expo-font/src/Font.ts:141:2)
        at apply (<rootDir>/node_modules/expo-font/src/Font.ts:106:40)
        at loadFontInNamespaceAsync (<rootDir>/node_modules/expo-font/src/Font.ts:99:43)
        at Array.map (<anonymous>)
        at map (<rootDir>/node_modules/expo-font/src/Font.ts:99:29)
        at Generator.next (<anonymous>)
        at asyncGeneratorStep (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
        at _next (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)
        at <rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7
        at new Promise (<anonymous>)
        at <rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12
        at apply (<rootDir>/node_modules/expo-font/src/Font.ts:104:2)
        at apply (<rootDir>/node_modules/expo-font/src/Font.ts:86:32)
        at <rootDir>/node_modules/expo-font/src/FontHooks.ts:29:14
        at commitHookEffectListMount (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12999:26)
        at commitPassiveMountOnFiber (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14422:11)
        at commitPassiveMountEffects_complete (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14382:9)
        at commitPassiveMountEffects_begin (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14369:7)
        at commitPassiveMountEffects (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14357:3)
        at flushPassiveEffectsImpl (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16248:3)
        at flushPassiveEffects (<rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16197:14)
        at <rootDir>/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16012:9
        at flushActQueue (<rootDir>/node_modules/react/cjs/react.development.js:2667:24)
        at act (<rootDir>/node_modules/react/cjs/react.development.js:2521:11)
        at actImplementation (<rootDir>/node_modules/@testing-library/react-native/src/act.ts:30:25)
        at renderWithAct (<rootDir>/node_modules/@testing-library/react-native/src/render.tsx:122:6)
        at renderWithAct (<rootDir>/node_modules/@testing-library/react-native/src/render.tsx:51:20)
        at Object.<anonymous> (<rootDir>/__tests__/App.test.tsx:8:11)
        at Generator.next (<anonymous>)
        at asyncGeneratorStep (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
        at _next (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:22:9)
        at <rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:27:7
        at new Promise (<anonymous>)
        at Object.<anonymous> (<rootDir>/node_modules/@babel/runtime/helpers/asyncToGenerator.js:19:12)
        at Promise.then.completed (<rootDir>/node_modules/jest-circus/build/utils.js:293:28)
        at new Promise (<anonymous>)
        at callAsyncCircusFn (<rootDir>/node_modules/jest-circus/build/utils.js:226:10)
        at _callCircusTest (<rootDir>/node_modules/jest-circus/build/run.js:248:40)
        at _runTest (<rootDir>/node_modules/jest-circus/build/run.js:184:3)
        at _runTestsForDescribeBlock (<rootDir>/node_modules/jest-circus/build/run.js:86:9)
        at _runTestsForDescribeBlock (<rootDir>/node_modules/jest-circus/build/run.js:81:9)
        at run (<rootDir>/node_modules/jest-circus/build/run.js:26:3)
        at runAndTransformResultsToJestFormat (<rootDir>/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:120:21)
        at jestAdapter (<rootDir>/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
        at runTestInternal (<rootDir>/node_modules/jest-runner/build/runTest.js:367:16)
        at runTest (<rootDir>/node_modules/jest-runner/build/runTest.js:444:34)

I think the commit that introduced this is #20258, but I haven't yet been able to trace down why this is occurring or how the font module actually gets into the registry successfully in expo-asset@8.7.0 (assuming that the issue isn't related to a missing mock, or something like that).

A few other points:

  • this does not occur when running the application in an emulator
  • this reproduces on both linux and mac environments

Environment

expo-env-info 1.0.5 environment info:
System:
OS: macOS 13.2.1
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.14.1 - ~/.volta/tools/image/node/18.14.1/bin/node
Yarn: 3.3.0 - ~/.volta/tools/image/yarn/3.3.0/bin/yarn
npm: 9.3.1 - ~/.volta/tools/image/node/18.14.1/bin/npm
Managers:
CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
Android SDK:
API Levels: 33
Build Tools: 30.0.3, 33.0.2
System Images: android-30 | AOSP ATD ARM 64 v8a, android-33 | Google APIs ARM 64 v8a
IDEs:
Android Studio: 2022.1 AI-221.6008.13.2211.9514443
Xcode: 14.2/14C18 - /usr/bin/xcodebuild
npmPackages:
expo: 48.0.4 => 48.0.4
react: 18.2.0 => 18.2.0
react-native: 0.71.3 => 0.71.3
Expo Workflow: managed

@coffeebeats coffeebeats added the needs validation Issue needs to be validated label Feb 28, 2023
@expo-bot expo-bot added the needs review Issue is ready to be reviewed by a maintainer label Feb 28, 2023
@Kudo
Copy link
Contributor

Kudo commented Feb 28, 2023

confirmed to reproduce the issue even using sdk 48 compatible expo-asset@8.9.0.

@Kudo Kudo added Issue accepted and removed needs validation Issue needs to be validated needs review Issue is ready to be reviewed by a maintainer labels Feb 28, 2023
@expo-bot
Copy link
Collaborator

Thank you for filing this issue!
This comment acknowledges we believe this may be a bug and there’s enough information to investigate it.
However, we can’t promise any sort of timeline for resolution. We prioritize issues based on severity, breadth of impact, and alignment with our roadmap. If you’d like to help move it more quickly, you can continue to investigate it more deeply and/or you can open a pull request that fixes the cause.

@Kudo Kudo added the Asset label Feb 28, 2023
@lucasrocali
Copy link

Same issue on expo@48.0.0, expo-asset@8.9.0

Adding the following to setupJests fixed

jest.mock('expo-font');
jest.mock('expo-asset');

@bucky0112
Copy link

Same issue on expo@48.0.0, expo-asset@8.9.0

Adding the following to setupJests fixed

jest.mock('expo-font');
jest.mock('expo-asset');

It works for me!

@codinsonn
Copy link
Contributor

Hmm, what if you're getting this error outside of Jest?

@KievDevel
Copy link

Same error outside of jest in the Expo Go after SDK 45 -> 48 upgrade.

@rikian
Copy link

rikian commented Apr 3, 2023

expo return array, u can mock like this

jest.mock('expo-font', () => []);

@madelineleclair
Copy link

Same issue on expo@48.0.0, expo-asset@8.9.0
Adding the following to setupJests fixed
jest.mock('expo-font');
jest.mock('expo-asset');

I was also experiencing this issue from @expo/vector-icons in jest tests after upgrading react-native to 0.72.4 from 0.70.8. This solution worked for me.

@skfaisal93
Copy link

setupJests

Same issue on expo@48.0.0, expo-asset@8.9.0

Adding the following to setupJests fixed

jest.mock('expo-font');
jest.mock('expo-asset');

What is "setupJests" ? I don't see any file with this name

@lucasrocali
Copy link

setupJests

Same issue on expo@48.0.0, expo-asset@8.9.0
Adding the following to setupJests fixed

jest.mock('expo-font');
jest.mock('expo-asset');

What is "setupJests" ? I don't see any file with this name

You can configure setupFiles on jest, something like

//package.json

    "jest": {
        ...
        "setupFiles": [
          "./setupJest.js"
        ],
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests