Closed
Description
What happened?
I just upgraded my dependencies and now I get the following error during my setup when running jest
:
● Test suite failed to run
ReferenceError: C:\dev\projects\climbers-atlas\ClimbersAtlas\test\setup.ts: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: async_storage_mock_1
Allowed objects: AbortController, AbortSignal, AggregateError, Array, ArrayBuffer, Atomics, BigInt, BigInt64Array, BigUint64Array, Boolean, Buffer, DataView, Date, Error, EvalError, Event, EventTarget, FinalizationRegistry, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, MessageChannel, MessageEvent, MessagePort, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, SharedArrayBuffer, String, Symbol, SyntaxError, TextDecoder, TextEncoder, TypeError, URIError, URL, URLSearchParams, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakRef, WeakSet, WebAssembly, __dirname, __filename, arguments, atob, btoa, clearImmediate, clearInterval, clearTimeout, console, decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, escape, eval, expect, exports, global, globalThis, isFinite, isNaN, jest, module, parseFloat, parseInt, performance, process, queueMicrotask, require, setImmediate, setInterval, setTimeout, undefined, unescape.
Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` (case insensitive) are permitted.
28 | Object.defineProperty(exports, "__esModule", { value: true });
29 | jest.mock("@react-native-async-storage/async-storage", () => {
> 30 | return async_storage_mock_1.default;
| ^^^^^^^^^^^^^^^^^^^^
31 | });
32 | jest.mock("i18n-js", () => ({
33 | currentLocale: () => "en",
at File.buildCodeFrameError (node_modules/@babel/core/lib/transformation/file/file.js:249:12)
at NodePath.buildCodeFrameError (node_modules/@babel/traverse/lib/path/index.js:145:21)
at newFn (node_modules/@babel/traverse/lib/visitors.js:181:21)
at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:100:31)
at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:105:16)
at TraversalContext.visitMultiple (node_modules/@babel/traverse/lib/context.js:74:17)
at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:131:19)
at traverseNode (node_modules/@babel/traverse/lib/traverse-node.js:24:17)
at NodePath.visit (node_modules/@babel/traverse/lib/path/context.js:107:52)
at TraversalContext.visitQueue (node_modules/@babel/traverse/lib/context.js:105:16)
at TraversalContext.visitSingle (node_modules/@babel/traverse/lib/context.js:79:19)
at TraversalContext.visit (node_modules/@babel/traverse/lib/context.js:133:19)
at traverseNode (node_modules/@babel/traverse/lib/traverse-node.js:24:17)
at traverse (node_modules/@babel/traverse/lib/index.js:62:34)
at transformFile (node_modules/@babel/core/lib/transformation/index.js:108:29)
at transformFile.next (<anonymous>)
at run (node_modules/@babel/core/lib/transformation/index.js:35:12)
at run.next (<anonymous>)
at transform (node_modules/@babel/core/lib/transform.js:29:41)
at transform.next (<anonymous>)
at evaluateSync (node_modules/gensync/index.js:251:28)
at sync (node_modules/gensync/index.js:89:14)
at stopHiding - secret - don't use this - v1 (node_modules/@babel/core/lib/errors/rewrite-stack-trace.js:50:12)
at transformSync (node_modules/@babel/core/lib/transform.js:56:76)
at TsJestTransformer.process (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:193:32)
at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:615:31)
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:757:40)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:814:19)
Moving the mock into the __mocks__
folder or changing it to jest.doMock(...)
fixes the error but then times out on the tests consuming AsyncStorage
.
Version
1.17.10
What platforms are you seeing this issue on?
- Android
- iOS
- macOS
- Windows
- web
System Information
System:
OS: Windows 10 10.0.22000
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 14.56 GB / 31.81 GB
Binaries:
Node: 16.15.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 8.19.2 - C:\dev\projects\climbers-atlas\ClimbersAtlas\node_modules\.bin\npm.CMD
Watchman: Not Found
SDKs:
Android SDK:
API Levels: 32
Build Tools: 32.0.0, 32.1.0, 33.0.0
System Images: android-31 | Google Play Intel x86 Atom_64
Android NDK: Not Found
Windows SDK:
AllowAllTrustedApps: Disabled
Versions: 10.0.17763.0
IDEs:
Android Studio: AI-212.5712.43.2112.8512546
Visual Studio: 17.2.32516.85 (Visual Studio Community 2022)
Languages:
Java: 17.0.1 - /c/Program Files/Java/jdk-17.0.1/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.2.0 => 18.2.0
react-native: 0.70.2 => 0.70.2
react-native-windows: Not Found
npmGlobalPackages:
*react-native*: Not Found
Steps to Reproduce
I uploaded a repo that reproduces the issue here to make things quicker: https://github.com/Silthus/jest-async-storage-error
Just clone it and run yarn install
then yarn test
to reproduce the error.
The repo was created the following way:
npx ignite-cli new JestDemo \ --bundle=test.jest \ --git \ --install-deps \ --packager=yarn \ --target-path=C:\temp\jest-demo\JestDemo \ --remove-demo
- Remove
android
andios
folders. - Pin all dependencies to a fixed version.
- Run
yarn upgrade-interactive --latest -E
and upgrade all dependencies to the latest version. - Add missing
jest-environment-jsdom
:yarn add --dev jest-environment-jsdom
- Fix "private method error" by adding
@babel/plugin-proposal-private-methods
tobabel.config.js
const plugins = [
[
"@babel/plugin-proposal-decorators",
{
legacy: true,
},
],
[
"@babel/plugin-proposal-private-methods",
{
loose: true
}
],
["@babel/plugin-proposal-optional-catch-binding"],
"react-native-reanimated/plugin", // NOTE: this must be last in the plugins
]
const vanillaConfig = {
presets: ["module:metro-react-native-babel-preset"],
env: {
production: {},
},
plugins,
}
const expoConfig = {
presets: ["babel-preset-expo"],
env: {
production: {},
},
plugins,
}
let isExpo = false
try {
const Constants = require("expo-constants")
// True if the app is running in an `expo build` app or if it's running in Expo Go.
isExpo =
Constants.executionEnvironment === "standalone" ||
Constants.executionEnvironment === "storeClient"
} catch {
}
const babelConfig = isExpo ? expoConfig : vanillaConfig
module.exports = babelConfig
- Run
yarn test
and see the tests fail