From 11452627687e2acf581d1b9b79c07cc777e446e6 Mon Sep 17 00:00:00 2001 From: Yusuf YILDIRIM Date: Sat, 10 Sep 2022 13:40:04 +0300 Subject: [PATCH 1/2] Fix Jest 28+ `expect.extend is not a function` error --- src/reanimated2/jestUtils.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/reanimated2/jestUtils.ts b/src/reanimated2/jestUtils.ts index d827c7fc957..3a7e3fd932f 100644 --- a/src/reanimated2/jestUtils.ts +++ b/src/reanimated2/jestUtils.ts @@ -188,11 +188,13 @@ export const advanceAnimationByFrame = (count) => { }; export const setUpTests = (userConfig = {}) => { - let expect; - try { - expect = require('expect'); - } catch (_) { - // for Jest in version 28+ + let expect = require('expect'); + + // 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 { expect: expectModule } = require('@jest/globals'); expect = expectModule; } From d6c60ff5d9ea15a38b56eaceedcf125871866d24 Mon Sep 17 00:00:00 2001 From: Krzysztof Piaskowy Date: Wed, 2 Nov 2022 13:40:45 +0100 Subject: [PATCH 2/2] If undefined try to get default (jest 28) --- src/reanimated2/jestUtils.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/reanimated2/jestUtils.ts b/src/reanimated2/jestUtils.ts index 3a7e3fd932f..1e3c5882b4f 100644 --- a/src/reanimated2/jestUtils.ts +++ b/src/reanimated2/jestUtils.ts @@ -188,15 +188,21 @@ export const advanceAnimationByFrame = (count) => { }; export const setUpTests = (userConfig = {}) => { - let expect = require('expect'); - - // 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 { 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');