Testing flow using prisma and graphql

Viewed 1576

I'm using prisma-graphql for backend server, and jest & supertest for testing graphql queries. For testing environment, I'd like to establish an in-memory database to create and retrieve data during test-phase. My questions are,

  1. What is a good way to configure a test DB for prisma client? Do I need to configure a memory DB at before all phase? like below?

    //setup.js for test
    
    beforeAll(async() => {
      db = new sqlite3.Database(':memory:', (err) => {
        if (err) {
          return console.error(err.message);
        }
        console.log('Connected to the in-memory SQlite database.');
      });
    })
    
  2. How & when can I run the commands for prisma? For instance, I need to run the below commands for in memory db, to create tables. https://github.com/prisma/prisma/issues/732 seems to suggest that in memory migration doesn't make sense - in other words, it does not support such thing?

    npx prisma migrate --experimental save/up
    npx prisma generate
    
1 Answers

I would suggest going for integration tests against an actual DB as they will be a good way to test your code against the Queries/Mutations that you perform.

I have created a repo here that performs integration tests against an actual DB and this one is for GraphQL. These contain the entire Jest setup from start to finish.

Related