blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Only User model data can be retrieved while testing #369

Closed LearningProcesss closed 2 years ago

LearningProcesss commented 2 years ago

What is the problem?

Only User model data can be retrieved while testing.

Paste all your error logs here:

yarn test --verbose -t "Prisma concrete query test"
yarn run v1.22.15
jest --verbose -t 'Prisma concrete query test'
 FAIL   SERVER  app/githubs/queries/github.queries.test.ts
  Prisma concrete query test
    ✕ should get the real count for each model (163 ms)

  ● Prisma concrete query test › should get the real count for each model

    expect(received).resolves.toBeGreaterThan(expected)

    Expected: > 0
    Received:   0

       5 |
       6 |         await expect(db.user.count()).resolves.toBeGreaterThan(0)
    >  7 |         await expect(db.githubRepo.count()).resolves.toBeGreaterThan(0)
         |                                                      ^
       8 |
       9 |     });
      10 | });

      at Object.toBeGreaterThan (node_modules/expect/build/index.js:198:20)
      at Object.<anonymous> (app/githubs/queries/github.queries.test.ts:7:54)

  console.log
    prisma:info Starting a sqlite pool with 9 connections.

      at Object.log (node_modules/@prisma/client/runtime/index.js:38162:11)

Test Suites: 1 failed, 4 skipped, 1 of 5 total
Tests:       1 failed, 8 skipped, 9 total

Paste all relevant code snippets here:

//blitz-tester/app/githubs/queries/github.queries.test.ts

import db from "db";

describe('Prisma concrete query test', () => {
    it('should get the real count for each model', async () => {

        await expect(db.user.count()).resolves.toBeGreaterThan(0)
        await expect(db.githubRepo.count()).resolves.toBeGreaterThan(0)

    });
});

What are detailed steps to reproduce this?

1.

Run blitz -v and paste the output here:

Linux 5.10 | linux-x64 | Node: v16.13.0

blitz: 0.44.3 (global)
blitz: 0.43.0 (local)

  Package manager: yarn 
  System:
    OS: Linux 5.10 Alpine Linux
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 1.05 GB / 3.81 GB
    Shell: 1.33.1 - /bin/ash
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 1.22.15 - /usr/local/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm
    Watchman: Not Found
  npmPackages:
    @prisma/client: 3.5.0 => 3.5.0 
    blitz: 0.43.0 => 0.43.0 
    prisma: 3.5.0 => 3.5.0 
    react: 18.0.0-alpha-5ca4b0433-20211020 => 18.0.0-alpha-5ca4b0433-20211020 
    react-dom: 18.0.0-alpha-5ca4b0433-20211020 => 18.0.0-alpha-5ca4b0433-20211020 
    typescript: ~4.4 => 4.4.4 

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

Database is filled by relevant model data, i can query with native driver:

sqlite> SELECT COUNT(*) FROM githubrepo; 290

beerose commented 2 years ago

Is it possible that it uses a test database instead of the dev one while running the tests?

LearningProcesss commented 2 years ago

Is it possible that it uses a test database instead of the dev one while running the tests?

This, along with a mock \ injection were my first thoughts. Sadly, i cant find an explicit reference. default test/util.tsx file for example contains a full BlitzRouter mock but nothing about db.

beerose commented 2 years ago

Is there any db.$reset() in the tests? That would clear the db.

LearningProcesss commented 2 years ago

yes, blitz comes with forgotPassword.test and resetPassword.test files by default and use it like:

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

I tried removing that also. does not solve. Because does not clear the real db, seems to me that this $reset is used to clear a temporary database (as you suggested) or an injected one. Keep in mind also that i'm executing only my test case yarn test --verbose -t "Prisma concrete query test" so i expect that db.$reset() is never called before my test.

LearningProcesss commented 2 years ago

"Test database" was the right path to follow. Digging into Prisma documentations i come across Environment doc. Turns out that .env.test.local expose this env key: DATABASE_URL="file:./db_test.sqlite" It was so clear and under my watch that i completely missed it!

beerose commented 2 years ago

Oh cool! Glad you found it