title: Jest with Apollo-server and Prisma 2
body: <p>I would like to know how can I use JEST with Prisma 2 ?</p>
  <p>When I execute my simple test, I have a strange error :</p>
  <pre><code>npm run test
  
  &gt; prisma2@1.0.0 test /Users/jeremiechazelle/Sites/prisma2/server
  &gt; env-cmd -f ./env/.env.test jest --watchAll
    console.error
      2020-06-18T21:02:04.820Z prisma-client Client Version 2.0.1
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at n (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.&lt;anonymous&gt; (node_modules/.prisma/client/index.js:18:1)
  
    console.error
      2020-06-18T21:02:04.966Z prisma-client Engine Version 7d77a7cd570c9dedb4cdf31a1cbec615cf2e1eda
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at n (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.&lt;anonymous&gt; (node_modules/.prisma/client/index.js:19:1)
  
    console.error
      2020-06-18T21:02:04.973Z prisma-client {
        engineConfig: {
          cwd: '/Users/jeremiechazelle/Sites/prisma2/server/prisma',
          debug: false,
          datamodelPath: '/Users/jeremiechazelle/Sites/prisma2/server/node_modules/.prisma/client/schema.prisma',
          prismaPath: undefined,
          generator: {
            name: 'client',
            provider: 'prisma-client-js',
            output: '/Users/jeremiechazelle/Sites/prisma2/server/node_modules/@prisma/client',
            binaryTargets: [],
            config: {}
          },
          showColors: false,
          logLevel: undefined,
          logQueries: undefined,
          flags: [],
          clientVersion: '2.0.1'
        }
      }
  
        3 | import { PrismaClient } from &quot;@prisma/client&quot;
        4 | 
      &gt; 5 | const prisma = new PrismaClient()
          |                ^
        6 | 
        7 | const client = new ApolloBoost({
        8 |     uri: &quot;http://localhost:4000&quot;,
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at new NewPrismaClient (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:237:7)
        at Object.&lt;anonymous&gt; (tests/user.test.js:5:16)
  
   PASS  tests/user.test.js
    ✓ Should create a new user (473 ms)
  
    console.error
      2020-06-18T21:02:05.393Z prisma-client Prisma Client call:
  
        37 |     })
        38 | 
      &gt; 39 |     const exists = await prisma.user.findOne({
           |                                      ^
        40 |         where: {
        41 |             id: parseInt(response.data.createUser.user.id, 10),
        42 |         },
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.model [as User] (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:519:11)
        at Object.findOne (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:626:33)
        at _callee$ (tests/user.test.js:39:38)
        at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  
    console.error
      2020-06-18T21:02:05.398Z prisma-client prisma.user.findOne({
        where: {
          id: 72
        }
      })
  
        37 |     })
        38 | 
      &gt; 39 |     const exists = await prisma.user.findOne({
           |                                      ^
        40 |         where: {
        41 |             id: parseInt(response.data.createUser.user.id, 10),
        42 |         },
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.model [as User] (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:520:11)
        at Object.findOne (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:626:33)
        at _callee$ (tests/user.test.js:39:38)
        at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  
    console.error
      2020-06-18T21:02:05.401Z prisma-client Generated request:
  
        37 |     })
        38 | 
      &gt; 39 |     const exists = await prisma.user.findOne({
           |                                      ^
        40 |         where: {
        41 |             id: parseInt(response.data.createUser.user.id, 10),
        42 |         },
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.model [as User] (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:523:11)
        at Object.findOne (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:626:33)
        at _callee$ (tests/user.test.js:39:38)
        at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  
    console.error
      2020-06-18T21:02:05.403Z prisma-client query {
        findOneUser(where: {
          id: 72
        }) {
          email
          id
          name
          password
        }
      }
      
  
        37 |     })
        38 | 
      &gt; 39 |     const exists = await prisma.user.findOne({
           |                                      ^
        40 |         where: {
        41 |             id: parseInt(response.data.createUser.user.id, 10),
        42 |         },
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.model [as User] (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:524:11)
        at Object.findOne (node_modules/@prisma/client/src/runtime/getPrismaClient.ts:626:33)
        at _callee$ (tests/user.test.js:39:38)
        at tryCatch (node_modules/regenerator-runtime/runtime.js:45:40)
  
    console.error
      2020-06-18T21:02:05.411Z plusX Execution permissions of /Users/jeremiechazelle/Sites/prisma2/server/node_modules/.prisma/client/query-engine-darwin are fine
  
        at Function.apply [as log] (node_modules/@prisma/debug/dist/index.js:57:1)
        at debug (node_modules/@prisma/client/app/src/node_modules/.pnpm/debug@4.1.1/node_modules/debug/src/common.js:114:1)
        at debug (node_modules/@prisma/debug/dist/index.js:42:1)
        at Object.plusX (node_modules/@prisma/engine-core/dist/util.js:17:1)
        at NodeEngine.prismaPath [as getPrismaPath] (node_modules/@prisma/engine-core/dist/NodeEngine.js:334:1)
        at node_modules/@prisma/engine-core/dist/NodeEngine.js:398:1
  
  A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --runInBand --detectOpenHandles to find leaks.
  Test Suites: 1 passed, 1 total
  Tests:       1 passed, 1 total
  Snapshots:   0 total
  Time:        4.593 s
  Ran all test suites.
  
  Watch Usage
   › Press f to run only failed tests.
   › Press o to only run tests related to changed files.
   › Press p to filter by a filename regex pattern.
   › Press t to filter by a test name regex pattern.
   › Press q to quit watch mode.
   › Press Enter to trigger a test run.
  
  </code></pre>
  <p>I launch the test with this command :
  <code>npm run test</code></p>
  <p>Jest config from <strong>packages.json</strong> :</p>
  <pre><code>  &quot;scripts&quot;: {
      &quot;test&quot;: &quot;env-cmd -f ./env/.env.test jest --watchAll&quot;
    },
    &quot;jest&quot;: {
      &quot;globalSetup&quot;: &quot;./tests/jest/globalSetup.js&quot;,
      &quot;globalTeardown&quot;: &quot;./tests/jest/globalTeardown.js&quot;
    },
  </code></pre>
  <p><strong>server.js</strong></p>
  <pre><code>...
  const server = new ApolloServer({
      typeDefs,
      resolvers,
      context(request) {
          return {
              prisma,
              request,
          }
      },
  })
  
  export { server as default }
  </code></pre>
  <p><strong>./tests/jest/globalSetup.js</strong></p>
  <pre><code>require(&quot;@babel/register&quot;)
  
  const server = require(&quot;../../server&quot;).default
  
  module.exports = async () =&gt; {
      global.httpServer = await server.listen({ port: 4000 })
  }
  </code></pre>
  <p><strong>./tests/jest/globalTeardown.js</strong></p>
  <pre><code>module.exports = async () =&gt; {
      await global.httpServer.server.close()
  }
  </code></pre>
  <p><strong>user.test.js</strong></p>
  <pre><code>import &quot;cross-fetch/polyfill&quot;
  import ApolloBoost, { gql } from &quot;apollo-boost&quot;
  import { PrismaClient } from &quot;@prisma/client&quot;
  
  const prisma = new PrismaClient()
  
  const client = new ApolloBoost({
      uri: &quot;http://localhost:4000&quot;,
  })
  
  /* beforeEach(async () =&gt; {
      await prisma.user.deleteMany({})
  }) */
  
  test(&quot;Should create a new user&quot;, async () =&gt; {
      // console.log(prisma)
  
      const createUser = gql`
          mutation {
              createUser(
                  data: {
                      name: &quot;Test&quot;
                      email: &quot;test@example.com&quot;
                      password: &quot;Password123&quot;
                  }
              ) {
                  token
                  user {
                      id
                  }
              }
          }
      `
  
      const response = await client.mutate({
          mutation: createUser,
      })
  
      const exists = await prisma.user.findOne({
          where: {
              id: parseInt(response.data.createUser.user.id, 10),
          },
      })
  
      // expect(exists.email).toBe(&quot;test@example.com&quot;)
  })
  </code></pre>
  <p>The mutation works, but I can't execute prisma.user.findOne...</p>
  <p>source code : <a href="https://github.com/jechazelle/blue-sky" rel="nofollow noreferrer">https://github.com/jechazelle/blue-sky</a></p>