Skip to content

Commit bf4cfd9

Browse files
authored
Integration tests for hackernews example (#1804)
1 parent f014ca6 commit bf4cfd9

File tree

4 files changed

+1088
-145
lines changed

4 files changed

+1088
-145
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { DbDrop, MigrateDev } from '@prisma/migrate'
2+
import { PrismaClient } from '@prisma/client'
3+
import { createYoga } from 'graphql-yoga'
4+
import { schema } from '../src/schema'
5+
import { createContext } from '../src/context'
6+
7+
describe('hackernews example integration', () => {
8+
beforeAll(async () => {
9+
// migrate
10+
await MigrateDev.new().parse([])
11+
12+
// seed
13+
const client = new PrismaClient()
14+
await client.link.create({
15+
data: {
16+
url: 'https://www.prisma.io',
17+
description: 'Prisma replaces traditional ORMs',
18+
},
19+
})
20+
await client.$disconnect()
21+
})
22+
23+
afterAll(async () => {
24+
// drop
25+
await DbDrop.new().parse([
26+
'--preview-feature', // DbDrop is an experimental feature
27+
'--force',
28+
])
29+
})
30+
31+
const yoga = createYoga({ schema, context: createContext })
32+
33+
it('should get posts from feed', async () => {
34+
const response = await yoga.fetch(
35+
'http://yoga/graphql?query={feed{url,description}}',
36+
)
37+
38+
const body = await response.json()
39+
expect(body).toMatchInlineSnapshot(`
40+
{
41+
"data": {
42+
"feed": [
43+
{
44+
"description": "Prisma replaces traditional ORMs",
45+
"url": "https://www.prisma.io",
46+
},
47+
],
48+
},
49+
}
50+
`)
51+
})
52+
53+
it('should create a new post', async () => {
54+
const response = await yoga.fetch('http://yoga/graphql', {
55+
method: 'POST',
56+
headers: { 'content-type': 'application/json' },
57+
body: JSON.stringify({
58+
query: /* GraphQL */ `
59+
mutation createPost {
60+
postLink(
61+
url: "https://www.the-guild.dev/graphql/yoga-server"
62+
description: "Time to Relax with GraphQL Yoga"
63+
) {
64+
url
65+
description
66+
}
67+
}
68+
`,
69+
}),
70+
})
71+
72+
const body = await response.json()
73+
expect(body).toMatchInlineSnapshot(`
74+
{
75+
"data": {
76+
"postLink": {
77+
"description": "Time to Relax with GraphQL Yoga",
78+
"url": "https://www.the-guild.dev/graphql/yoga-server",
79+
},
80+
},
81+
}
82+
`)
83+
})
84+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
declare module '@prisma/migrate' {
2+
// https://github.com/prisma/prisma/blob/main/packages/internals/src/cli/types.ts
3+
class Command {
4+
public async parse(argv: string[]): Promise<string | Error>
5+
}
6+
7+
// https://github.com/prisma/prisma/blob/main/packages/migrate/src/commands/DbDrop.ts
8+
class DbDrop extends Command {
9+
public static new(): DbDrop
10+
}
11+
12+
// https://github.com/prisma/prisma/blob/main/packages/migrate/src/commands/MigrateDev.ts
13+
class MigrateDev extends Command {
14+
public static new(): DbDrop
15+
}
16+
}

examples/hackernews/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"license": "ISC",
1414
"devDependencies": {
1515
"@prisma/client": "3.15.2",
16+
"@prisma/migrate": "4.3.1",
17+
"@prisma/internals": "4.3.1",
1618
"@types/node": "16.11.60",
1719
"cross-env": "7.0.3",
1820
"ts-node": "10.9.1",

0 commit comments

Comments
 (0)