Skip to content

Commit 9292164

Browse files
authored
Fix Jest config for MSW + axios compatibility (#77)
## Description Jest tests were failing with the error below on freshly created Expo app using the CLI. ``` ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { LensList } from "./lens-list.mjs"; ^^^^^^ SyntaxError: Cannot use import statement outside a module > 1 | import { setupServer } from 'msw/node'; | ^ 2 | 3 | /** 4 | * MSW server for mocking network requests at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14) at Object.<anonymous> (node_modules/msw/src/core/experimental/define-network.ts:2:47) at Object.<anonymous> (node_modules/msw/src/node/setup-server.ts:9:8) at Object.require (src/test/server.ts:1:1) at Object.require (jest.setup.js:4:1) ``` Jest fixes were made to the Expo app built from the boilerplate Belt template and ported to the CLI as well so that tests for new apps don't break. ## Related links - https://mswjs.io/docs/integrations/react-native/#prerequisites - mswjs/msw#2698 - mswjs/msw#170
1 parent c39e3c4 commit 9292164

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

templates/boilerplate/jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ module.exports = {
33
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
44
coveragePathIgnorePatterns: ['/node_modules', 'src/test'],
55
transformIgnorePatterns: [
6-
'node_modules/(?!((jest-)?react-native|@react-native-community|@react-native|react-native|@react-navigation)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|until-async)',
6+
'node_modules/(?!((jest-)?react-native|@react-native-community|@react-native|react-native|@react-navigation)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|until-async|@open-draft|rettime)',
77
],
8+
transform: {
9+
'^.+\\.mjs$': 'babel-jest',
10+
},
811
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
912
moduleNameMapper: {
1013
'.+\\.(png|jpg|ttf|woff|woff2)$': '<rootDir>/src/test/fileMock.js',

templates/boilerplate/jest.setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';
44
import server from 'src/test/server';
55
import queryClient from 'src/util/api/queryClient';
66

7+
global.ReadableStream = require('stream/web').ReadableStream;
8+
79
beforeEach(() => {
810
jest.clearAllMocks();
911
});

0 commit comments

Comments
 (0)