Шутка с Apollo-сервером и Prisma 2 - PullRequest
0 голосов
/ 20 июня 2020

Я хотел бы знать, как я могу использовать JEST с Prisma 2?

Когда я выполняю свой простой тест, у меня появляется странная ошибка:

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.

Я запускаю тест с эта команда: npm run test

Конфигурация Jest из пакетов. json:

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

сервер. 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")
})

Мутация работает, но я не могу выполнить prisma.user.findOne ...

исходный код: https://github.com/jechazelle/blue-sky

1 Ответ

0 голосов
/ 22 июня 2020

Я добавил интеграционный тест в это репо с Prisma. Вы можете найти настройки в папке prisma, а тесты - в папке tests.

...