blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Using planetscale / mysql breaks jest tests #157

Closed zipzapzanigan closed 2 years ago

zipzapzanigan commented 2 years ago

What is the problem?

when you run db.$reset when using planetscale and prisma (mysql) it seems that the default jest tests don't work.

db.$reset drops the tables of the database, and it does a "prisma migrate reset --force --skip-generate" but planetscale doesn't use migrate, it requires that you use prisma db push. This would require that we send a mysql command to drop all tables, and then do a db push.

Paste all your error logs here:

 FAIL   SERVER  app/auth/mutations/forgotPassword.test.ts (15.26 s)
  forgotPassword mutation
    ✕ does not throw error if user doesn't exist (7048 ms)
    ✕ works correctly (7255 ms)

  ● forgotPassword mutation › does not throw error if user doesn't exist

    expect(received).resolves.not.toThrow()

    Received promise rejected instead of resolved
    Rejected to value: [Error:·
    Invalid `prisma.user.findFirst()` invocation:··
      The table `(not available)` does not exist in the current database.]

      17 | describe("forgotPassword mutation", () => {
      18 |   it("does not throw error if user doesn't exist", async () => {
    > 19 |     await expect(forgotPassword({ email: "no-user@email.com" }, {} as Ctx)).resolves.not.toThrow()
         |           ^
      20 |   })
      21 |
      22 |   it("works correctly", async () => {

      at expect (node_modules/expect/build/index.js:134:15)
      at Object.<anonymous> (app/auth/mutations/forgotPassword.test.ts:19:11)

  ● forgotPassword mutation › works correctly

    Invalid `db.user.create()` invocation in
/app/auth/mutations/forgotPassword.test.ts:24:32

      21 
      22 it("works correctly", async () => {
      23   // Create test user
    → 24   const user = await db.user.create(
      The table `(not available)` does not exist in the current database.

      22 |   it("works correctly", async () => {
      23 |     // Create test user
    > 24 |     const user = await db.user.create({
         |                  ^
      25 |       data: {
      26 |         email: "user@example.com",
      27 |         tokens: {

      at cb (node_modules/@prisma/client/runtime/index.js:38703:17)
      at PrismaClient._request (node_modules/@prisma/client/runtime/index.js:40859:18)
      at Object.<anonymous> (app/auth/mutations/forgotPassword.test.ts:24:18)

Paste all relevant code snippets here:

beforeEach(async () => {
  await db.$reset()
})

What are detailed steps to reproduce this?

  1. set up planetscale DB
  2. create a new default blitz project
  3. configure with planetscale
  4. run jest tests (fails on user test)

Run blitz -v and paste the output here:

macOS Monterey | darwin-arm64 | Node: v16.13.2

blitz: 0.45.3 (global)
blitz: 0.45.3 (local)

  Package manager: npm 
  System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1
    Memory: 127.48 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /opt/homebrew/bin/yarn
    npm: 8.1.4 - /opt/homebrew/bin/npm
    Watchman: Not Found
  npmPackages:
    @prisma/client: 3.9.2 => 3.9.2 
    blitz: 0.45.3 => 0.45.3 
    prisma: 3.9.2 => 3.9.2 
    react: 18.0.0-beta-149b420f6-20211119 => 18.0.0-beta-149b420f6-20211119 
    react-dom: 18.0.0-alpha-5ca4b0433-20211020 => 18.0.0-alpha-5ca4b0433-20211020 
    typescript: ~4.5 => 4.5.5 

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

No response

beerose commented 2 years ago

Hey @zipzap888!

The issue was also briefly discussed here: https://github.com/blitz-js/blitz/issues/3053. Once we have a Planetscale recipe, it should do all the required changes to make the app work with Planetscale, like removing the migrations folder, removing or replacing $reset calls etc.

Right now the solution is to perform all the changes manually.