callstack / reassure

Performance testing companion for React and React Native
https://callstack.github.io/reassure/
MIT License
1.21k stars 25 forks source link

[FEATURE] Support for Create React App #507

Closed jiayihu closed 2 weeks ago

jiayihu commented 3 weeks ago

Is your feature request related to a problem? Please describe. Is there any way to support Create React App + TypeScript? I know that CRA is basically abandoned nowadays but in many production apps it's still used.

When I run npx assure I get the Jest error because it's not transforming the test file with Babel. I'm not sure where to fix it:

 FAIL  src/studio/components/extensions/selection-mode/selection.perf.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/jiayihu/Desktop/Repo/CollaboardWeb/src/studio/components/extensions/selection-mode/selection.perf.test.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { range } from "ramda";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

From what I can see, Jest is correctly configured to transform any TS file https://github.com/facebook/create-react-app/blob/0a827f69ab0d2ee3871ba9b71350031d8a81b7ae/packages/react-scripts/scripts/utils/createJestConfig.js#L42-L50

Describe the solution you'd like npx assure should be able to transform perf-test.ts files with Jest as usual

jiayihu commented 3 weeks ago

Okay after a bit of source code reading I found that TEST_RUNNER_PATH=/path-to-your-project/node_modules/react-scripts/scripts/test.js npx reassure works.

The idea is simply that react-scripts test works in making Jest transforming the files, so instead of using the Jest bin, we use the CRA test.js bin.

Would you like me to add a note for it on the README?

mdjastrzebski commented 2 weeks ago

CRA is no longer recommended by the React team, therefore I would leave this as is, with the existing workaround you've found.