0

Repo

I am trying to setup Jest in my Turborepo monorepo specifically in my UI package. I'm trying to setup Jest so I have a very basic test to see how it works:

Button.spec.tsx

import {describe, it, expect} from "@jest/globals";
import {render} from "@testing-library/react-native";
import Button from "./Button.tsx";
import React from "react";

describe("Button", () => {
  it("renders the button with text", () => {
    expect(true).toBe(true);
    render(<Button>Click me</Button>);
  });
});

However, the test fails with a long error. Here is the important part:

    <monorepoRoot>/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/react-native/index.js:28
    
    import typeof * as ReactNativePublicAPI from './index.js.flow';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import {describe, it, expect} from "@jest/globals";
    > 2 | import {render} from "@testing-library/react-native";
        | ^
      3 | import Button from "./Button.tsx";
      4 | import React from "react";
      5 |

I have tried a lot to fix this error and feel like I have hit wall this. Here is everything:

  • I have tried creating my config from scratch with the jest cli
  • Using the react-native preset
  • Using the jsWithBabelPreset transform
  • Using ts-jest with transforms
  • Using ts-jest config helper, see here
  • Using module file extensions
  • Using babel instead and in addition to ts-jest
  • Using babel's module:metro-react-native-babel-preset
  • Using babel-plugin-strip-flow-types
  • Using babel/plugin-transform-modules-commonjs
  • Using experimental-vm-modules
  • Transforming all of node_modules which only led to new errors

Here is my current config files:

babel.config.js

/** @type {import('@babel/core').ConfigFunction} */
export default {
  presets: ["module:metro-react-native-babel-preset"],
  plugins: [
    "@babel/plugin-transform-flow-strip-types",
    "@babel/plugin-transform-modules-commonjs",
  ],
};

jest.config.ts

import type {Config} from "jest";
import {createJsWithBabelPreset} from "ts-jest";

const jsWithBabelPreset = createJsWithBabelPreset({
  tsconfig: "tsconfig.json",
  babelConfig: true,
});

const jestConfig: Config = {
  preset: "react-native",
  transform: jsWithBabelPreset.transform,
  transformIgnorePatterns: [
    "/node_modules/(?!(@react-native|react-native)/).*/",
  ],
  moduleFileExtensions: ["ts", "tsx", "js", "flow"],
};

export default jestConfig;

tsconfig.json

{
  "extends": "../typescript-config/react-library.json",
  "compilerOptions": {
    "jsx": "react",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ESNext",
    "esModuleInterop": true,
    "allowImportingTsExtensions": true,
    "allowJs": true
  }
}

tsconfig.json (base)

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "@react-native/typescript-config",
  "compilerOptions": {
    "jsx": "react-jsx",
    "allowImportingTsExtensions": true,
    "noEmit": true
  }
}

Test command

node --experimental-vm-modules node_modules/jest/bin/jest.js

I suspect the issue is with React native shipping flow code into the bundle but I do not know how to fix it. Here are my Jest related dependencies:

    "@testing-library/react-native": "^12.9.0",
    "babel-jest": "^30.0.1",
    "jest": "^30.0.0",
    "metro-react-native-babel-preset": "^0.77.0",
    "ts-jest": "^29.4.0",
    "@babel/core": "catalog:",
    "@babel/plugin-transform-flow-strip-types": "^7.27.1",
    "@babel/plugin-transform-private-methods": "^7.27.1",
    "@babel/plugin-transform-private-property-in-object": "^7.27.1",
    "@babel/plugin-transform-react-jsx": "^7.27.1",
    "@babel/preset-env": "^7.27.2",
    "@babel/preset-react": "^7.27.1",
    "@jest/globals": "^30.0.0",

As far as I am aware these are the latest versions:

System specs:

  • Node v23.4.0
  • macOS 15.5
  • pnpm 10.12.1
  • turbo 2.5.4

What should I do to fix this.

P.S. I have already had to patch a react native package shipping flow code in a .js extension.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.