Skip to content

Commit

Permalink
Make the react-native entry point Jest compatible
Browse files Browse the repository at this point in the history
* Remove the need to create a custom resolver in jest code testing realm
* This ensures there are no breaking changes in current users tests
  • Loading branch information
takameyer committed Jul 28, 2023
1 parent ec2d005 commit 7134e8f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 33 deletions.
30 changes: 0 additions & 30 deletions packages/realm-react/custom-resolver.cjs

This file was deleted.

3 changes: 1 addition & 2 deletions packages/realm-react/jest.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"verbose": true,
"preset": "react-native",
"resolver": "./custom-resolver.cjs",
"roots": [
"<rootDir>/src"
],
Expand All @@ -13,4 +12,4 @@
},
"testRegex": "(/__tests__/.*.(test|spec)).(jsx?|tsx?|js?|ts?)$",
"testTimeout": 30000
}
}
17 changes: 16 additions & 1 deletion packages/realm/index.react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// Detect if jest is running
const isJest = process?.env?.JEST_WORKER_ID !== undefined;

Check failure on line 23 in packages/realm/index.react-native.js

View workflow job for this annotation

GitHub Actions / Lint

'process' is not defined

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;

0 comments on commit 7134e8f

Please sign in to comment.