Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma with JEST - at Function.apply [as log] #2803

Closed
jechazelle opened this issue Jun 18, 2020 · 2 comments
Closed

Prisma with JEST - at Function.apply [as log] #2803

jechazelle opened this issue Jun 18, 2020 · 2 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug.

Comments

@jechazelle
Copy link

I try to use JEST with Prisma 2, but I have a strange error :

npm run test

> prisma2@1.0.0 test /Users/jeremiechazelle/Sites/prisma2/server
> 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.<anonymous> (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.<anonymous> (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 "@prisma/client"
      4 | 
    > 5 | const prisma = new PrismaClient()
        |                ^
      6 | 
      7 | const client = new ApolloBoost({
      8 |     uri: "http://localhost:4000",

      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.<anonymous> (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 | 
    > 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 | 
    > 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 | 
    > 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 | 
    > 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.

Launch test with this command :
npm run test

Jest config from packages.json :

  "scripts": {
    "test": "env-cmd -f ./env/.env.test jest --watchAll"
  },
  "jest": {
    "globalSetup": "./tests/jest/globalSetup.js",
    "globalTeardown": "./tests/jest/globalTeardown.js"
  },

server.js

...
const server = new ApolloServer({
    typeDefs,
    resolvers,
    context(request) {
        return {
            prisma,
            request,
        }
    },
})

export { server as default }

./tests/jest/globalSetup.js

require("@babel/register")

const server = require("../../server").default

module.exports = async () => {
    global.httpServer = await server.listen({ port: 4000 })
}

./tests/jest/globalTeardown.js

module.exports = async () => {
    await global.httpServer.server.close()
}

user.test.js

import "cross-fetch/polyfill"
import ApolloBoost, { gql } from "apollo-boost"
import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient()

const client = new ApolloBoost({
    uri: "http://localhost:4000",
})

/* beforeEach(async () => {
    await prisma.user.deleteMany({})
}) */

test("Should create a new user", async () => {
    // console.log(prisma())

    const createUser = gql`
        mutation {
            createUser(
                data: {
                    name: "Test"
                    email: "test@example.com"
                    password: "Password123"
                }
            ) {
                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("test@example.com")
})

source code : https://github.com/jechazelle/blue-sky

@pantharshit00
Copy link
Contributor

This is related to #2787 in which our debug logger was running in tests. This is now fixed in dev channel and will be part of 2.1.0 tomorrow.

To install it right now, run:

yarn add @prisma/cli@dev @prisma/client@dev

After doing that I was able to run your test without any tracing output:
image

@pantharshit00 pantharshit00 added 2.0.0 bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. labels Jun 22, 2020
@pantharshit00
Copy link
Contributor

pantharshit00 commented Jun 22, 2020

(Internal note: I closed this as a duplicate of #2787 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

2 participants