telescope icon indicating copy to clipboard operation
telescope copied to clipboard

Jest crashes with Nextjs v12.1.6

Open RC-Lee opened this issue 2 years ago • 0 comments

What happened: Since our update to Nextjs 12.1.6, the script pnpm jest has been crashing, giving an error log below.

Error: > Couldn't find a `pages` directory. Please create one under the project root
    at Object.findPagesDir (C:\OSD\telescope\node_modules\.pnpm\[email protected]_tmacpkxga3bcx5k2p7mjqnxqbe\node_modules\next\dist\lib\find-pages-dir.js:39:15)
    at C:\OSD\telescope\node_modules\.pnpm\[email protected]_tmacpkxga3bcx5k2p7mjqnxqbe\node_modules\next\dist\build\jest\jest.js:26:47
    at readConfigFileAndSetRootDir (C:\OSD\telescope\node_modules\.pnpm\[email protected]\node_modules\jest-config\build\readConfigFileAndSetRootDir.js:152:26)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)    at async readConfig (C:\OSD\telescope\node_modules\.pnpm\[email protected]\node_modules\jest-config\build\index.js:233:18)
    at async Promise.all (index 0)
    at async readConfigs (C:\OSD\telescope\node_modules\.pnpm\[email protected]\node_modules\jest-config\build\index.js:441:27)
    at async runCLI (C:\OSD\telescope\node_modules\.pnpm\@[email protected]\node_modules\@jest\core\build\cli\index.js:132:59)
    at async Object.run (C:\OSD\telescope\node_modules\.pnpm\[email protected]\node_modules\jest-cli\build\cli\index.js:155:37)
 ELIFECYCLE  Command failed with exit code 1.

What should have happened: Jest tests should run

How to reproduce it (as precise as possible): Run pnpm jest

Anything else we need to know?: The error happens on this line in Next.

What happened in Next is they added a new pagesDir in their Jest transformer.

In our monorepo, Jest considers the Telescope root as the root directory, instead of the web app root, and since it can't find pages in <root> or <root>/src, an error is thrown and the crash occurs.

Our choices may be

  1. Somehow tweak the front-end Jest config so it recognizes the web app root as root.

  2. Temporarily comment out this line in the Telescope root Jest config projects list. This will let all other unit tests to run in Jest. Currently we have a single unit test in the front-end.

  3. Completely ditch pnpm jest altogether and stick with Turborepo tests.. Somehow Turborepo can identify the correct <root> in its projects. pnpm test or pnpm test --filter=./src/web/app will run without crashing

  4. Downgrade back to Nextjs v12.1.4

RC-Lee avatar Sep 02 '22 02:09 RC-Lee