blitz-js / blitz

⚡️ The Missing Fullstack Toolkit for Next.js
https://Blitzjs.com
MIT License
13.68k stars 798 forks source link

Tests are broken in a freshly generated project #3980

Closed andrewhamon closed 1 year ago

andrewhamon commented 1 year ago

What is the problem?

With a newly generated blitz app, tests are failing.

Paste all your error logs here:

yarn run jest

info  - Loaded env from /Users/andrewhamon/code/my-test-app/.env.test
info  - Loaded env from /Users/andrewhamon/code/my-test-app/.env
● Validation Warning:

  Unknown option "name" with value "client" was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

● Validation Warning:

  Unknown option "name" with value "server" was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

 PASS   SERVER  src/auth/mutations/forgotPassword.test.ts
 PASS   SERVER  src/auth/mutations/resetPassword.test.ts
 FAIL   CLIENT  test/index.test.tsx
  ● 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/andrewhamon/code/my-test-app/node_modules/nanoid/index.prod.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { urlAlphabet } from './url-alphabet/index.js'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      7 | }
      8 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1449:14)
      at Object.<anonymous> (node_modules/@blitzjs/auth/dist/index-server.cjs:11:16)
      at Object.<anonymous> (node_modules/@blitzjs/rpc/dist/chunks/rpc.cjs:8:14)
      at Object.<anonymous> (node_modules/@blitzjs/rpc/dist/index-browser.cjs:7:13)
      at Object.<anonymous> (node_modules/@blitzjs/rpc/dist/index-server.cjs:9:22)
      at Object.<anonymous> (src/users/hooks/useCurrentUser.ts:9:14)
      at Object.<anonymous> (test/index.test.tsx:7:25)

Test Suites: 1 failed, 2 passed, 3 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        3.353 s
Ran all test suites in 2 projects.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Paste all relevant code snippets here:

There is no relevent code snippet

What are detailed steps to reproduce this?

  1. npx blitz new my-test-app use default for all choices
  2. cd my-test-app
  3. yarn jest

Run blitz -v and paste the output here:

Blitz version: 2.0.0-beta.18 (local)
macOS Monterey | darwin-arm64 | Node: v18.12.1

 Package manager: npm

  System:
    OS: macOS 12.6.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 8.30 GB / 64.00 GB
    Shell: 5.8.1 - /run/current-system/sw/bin/zsh
  Binaries:
    Node: 18.12.1 - /var/folders/0g/758sdwpd3snckpsnb716xjwr0000gp/T/yarn--1668985517428-0.00988142138234882/node
    Yarn: 1.22.19 - /var/folders/0g/758sdwpd3snckpsnb716xjwr0000gp/T/yarn--1668985517428-0.00988142138234882/yarn
    npm: 8.19.2 - ~/.nix-profile/bin/npm
  npmPackages:
    @blitzjs/auth: 2.0.0-beta.18 => 2.0.0-beta.18
    @blitzjs/next: 2.0.0-beta.18 => 2.0.0-beta.18
    @blitzjs/rpc: 2.0.0-beta.18 => 2.0.0-beta.18
    @prisma/client: 4.6.0 => 4.6.0
    blitz: 2.0.0-beta.18 => 2.0.0-beta.18
    next: 12.2.5 => 12.2.5
    prisma: 4.6.0 => 4.6.0
    react: 18.2.0 => 18.2.0
    react-dom: 18.2.0 => 18.2.0
    typescript: ^4.8.4 => 4.9.3

✨  Done in 0.73s.

Please include below any other applicable logs and screenshots that show your problem:

No response

dillondotzip commented 1 year ago

@andrewhamon Ah yes, just realized this too. Good news is there is an issue open to start using vitest instead of jest for the generated templates https://github.com/blitz-js/blitz/issues/3835

This should fix these issues.

dillondotzip commented 1 year ago

A PR has been open https://github.com/blitz-js/blitz/pull/3985

Just waiting for the documentation to be updated before we merge and release

logancollingwood commented 1 year ago

Thanks for the update on this!

Just curious if there's any recommended approach to fix up a project that was created when these were broken besides cherry-picking in the appropriate fixes for my app. Thanks in advance!