diff --git a/CHANGELOG.md b/CHANGELOG.md index d51ec64077..ae58babd05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ * None ### Fixed -* ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?) +* Fix Jest issues when testing against Realm ([#6003](https://github.com/realm/realm-js/issues/6003)) * None ### Compatibility diff --git a/packages/realm-react/custom-resolver.cjs b/packages/realm-react/custom-resolver.cjs deleted file mode 100644 index f4b9523980..0000000000 --- a/packages/realm-react/custom-resolver.cjs +++ /dev/null @@ -1,30 +0,0 @@ -//////////////////////////////////////////////////////////////////////////// -// -// Copyright 2023 Realm Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////// - -/* eslint-env node */ - -module.exports = function (request, options) { - if (request === "react-native") { - // Ensure that "react-native" always resolve relative to the rootDir - options.basedir = options.rootDir; - } else if (request === "realm") { - // Ensure the node binding is used when testing with Jest on node - options.conditions = ["require", "default", "node"]; - } - return options.defaultResolver(request, options); -}; diff --git a/packages/realm-react/jest.config.json b/packages/realm-react/jest.config.json index b58c030832..f43e5c446f 100644 --- a/packages/realm-react/jest.config.json +++ b/packages/realm-react/jest.config.json @@ -1,7 +1,6 @@ { "verbose": true, "preset": "react-native", - "resolver": "./custom-resolver.cjs", "roots": [ "/src" ], @@ -13,4 +12,4 @@ }, "testRegex": "(/__tests__/.*.(test|spec)).(jsx?|tsx?|js?|ts?)$", "testTimeout": 30000 -} \ No newline at end of file +} diff --git a/packages/realm/index.react-native.js b/packages/realm/index.react-native.js index 2a1b1b35d8..aba7d1bf3a 100644 --- a/packages/realm/index.react-native.js +++ b/packages/realm/index.react-native.js @@ -19,4 +19,19 @@ /* eslint-disable @typescript-eslint/no-var-requires -- We're exporting using CJS assignment */ /* eslint-env commonjs */ -module.exports = require("./dist/bundle.react-native").Realm; +// eslint-disable-next-line no-undef -- In React Native, process is not defined, but in Jest it is +const isJest = process?.env?.JEST_WORKER_ID !== undefined; + +let entryPoint; + +if (isJest) { + // Define a require function that will load the node bundle + // otherwise, metro will preemptively load the node bundle + const nodeRequire = require; + // Jest is running, use the node bundle + entryPoint = nodeRequire("./dist/bundle.node"); +} else { + entryPoint = require("./dist/bundle.react-native"); +} + +module.exports = entryPoint.Realm;