用 Apollo-server 和 Prisma 2 开玩笑

Posted

技术标签:

【中文标题】用 Apollo-server 和 Prisma 2 开玩笑【英文标题】:Jest with Apollo-server and Prisma 2 【发布时间】:2020-10-10 16:11:48 【问题描述】:

我想知道如何将 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

来自 packages.json 的 Jest 配置:

  "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")
)

突变有效,但我无法执行 prisma.user.findOne...

源代码:https://github.com/jechazelle/blue-sky

【问题讨论】:

【参考方案1】:

我在this repo 中使用 Prisma 添加了集成测试。您可以在prisma 文件夹中找到设置,在tests 文件夹中找到测试。

【讨论】:

感谢@Ryan 的回答!我尝试测试我的数据库(例如,如果数据库中存在插入)不仅响应

以上是关于用 Apollo-server 和 Prisma 2 开玩笑的主要内容,如果未能解决你的问题,请参考以下文章

Prisma v2:生成一个 .graphql 文件来编辑?

我不能在 apollo-server 中创建一个 rest API 吗?

在 apollo-server 中将网关上的联合类型转换为服务 GraphQL 中的标量类型

不能在一个文件中运行多个测试

将 primsa 模式导入 graphql 模式

记录 apollo-server GraphQL 查询和每个请求的变量