diff --git a/src/reanimated2/jestUtils.ts b/src/reanimated2/jestUtils.ts index d827c7fc957..1e3c5882b4f 100644 --- a/src/reanimated2/jestUtils.ts +++ b/src/reanimated2/jestUtils.ts @@ -188,13 +188,21 @@ export const advanceAnimationByFrame = (count) => { }; export const setUpTests = (userConfig = {}) => { - let expect; - try { - expect = require('expect'); - } catch (_) { - // for Jest in version 28+ - const { expect: expectModule } = require('@jest/globals'); + let expect = global.expect; + if (expect === undefined) { + const expectModule = require('expect'); expect = expectModule; + // Starting from Jest 28, "expect" package uses named exports instead of default export. + // So, requiring "expect" package doesn't give direct access to "expect" function anymore. + // It gives access to the module object instead. + // We use this info to detect if the project uses Jest 28 or higher. + if (typeof expect === 'object') { + const jestGlobals = require('@jest/globals'); + expect = jestGlobals.expect; + } + if (expect === undefined || expect.extend === undefined) { + expect = expectModule.default; + } } require('setimmediate');