callstack / react-native-testing-library

🦉 Simple and complete React Native testing utilities that encourage good testing practices.
https://callstack.github.io/react-native-testing-library/
MIT License
3.01k stars 264 forks source link

Vitest support #1586

Closed camilossantos2809 closed 2 months ago

camilossantos2809 commented 2 months ago

Ask your Question

Is this library supposed to work with vitest? I'm thinking to migrate from jest but exactly in .tsx files where my tests are slow, I'm getting some errors:

SyntaxError: Cannot use import statement outside a module
 ❯ Object.<anonymous> node_modules/@testing-library/react-native/src/helpers/format-default.ts:1:1
// vite.config.ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [tsconfigPaths(), react()],
  test: {
    globals: true,
    setupFiles: "./src/utils/test-helpers/setup-vitest.ts",
    server: {
      deps: {
        inline: ["react-native"],
      },
    },
  },
});
// setup-vitest.ts
import "@testing-library/react-native/extend-expect";

Without setup file it gives a different error:

SyntaxError: Cannot use import statement outside a module
 ❯ Object.<anonymous> node_modules/@testing-library/react-native/src/helpers/host-component-names.tsx:3:1
mdjastrzebski commented 2 months ago

Currently we do not support for Vitest, but if there would be users willing to use it and submit PRs for making it work with RNTL we would be happy to start supporting it.

RNTL is written in a way that we try to avoid tight coupling to Jest. I know that other users are able to user RNTL with Mocha.

Regarding your error, it looks like Vitest is trying to run the RNTL code directly as CommonJS and not passing it through Babel, and hence does so because of import statements.

pierrezimmermannbam commented 2 months ago

The issue is that the RN ecosystem as a whole doesn't support vitest for now and since testing in RN environment requires a lot of mock, most RN librairies have a jest preset and not a vitest preset. For instance RN has a jest preset but not a vitest preset. Maybe it could work if you alias vitest globals to match the ones of jest but I haven't tried it. It would probably not be very hard for RNTL to support vitest but it would only make sense if it was supported more widely through the RN ecosystem

mdjastrzebski commented 2 months ago

A project to watch then, and potentially collaborate in the future is VXRN who work on bringing Vite into RN world. It's in alpha now but seems to be run by smart folks.

mdjastrzebski commented 2 months ago

Closing as not fixable for now.