I would like to know how can I use JEST with Prisma 2?
When I execute my simple test, 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.
I launch the 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")
})
The mutation works, but I can't execute prisma.user.findOne...
source code: https://github.com/jechazelle/blue-sky
I have added integration test in this repo with Prisma. You can find the setup in the prisma
folder and the tests in the tests
folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.