Skip to content

Commit 6f65a33

Browse files
committed
feat: pactum sample with graphql
1 parent 46a53b9 commit 6f65a33

File tree

2 files changed

+188
-1
lines changed

2 files changed

+188
-1
lines changed

apps/graphql-sample/test/app.e2e-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const cats: Cat[] = [
2727

2828
const gql = '/graphql';
2929

30-
describe('AppController (e2e)', () => {
30+
describe('GraphQL AppResolver (e2e) {Supertest}', () => {
3131
let app: INestApplication;
3232

3333
beforeAll(async () => {
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { Test, TestingModule } from '@nestjs/testing';
3+
import * as pactum from 'pactum';
4+
import { AppModule } from '../src/app.module';
5+
import { Cat } from '../src/cat/models/cat-query.dto';
6+
7+
const cats: Cat[] = [
8+
{
9+
name: 'Ventus',
10+
age: 4,
11+
breed: 'Russian Blue',
12+
id: '1',
13+
},
14+
{
15+
name: 'Terra',
16+
age: 5,
17+
breed: 'Siberian',
18+
id: '2',
19+
},
20+
{
21+
name: 'Aqua',
22+
age: 3,
23+
breed: 'Maine Coon',
24+
id: '3',
25+
},
26+
];
27+
28+
describe('GraphQL ApPResolver (e2e) {Pactum}', () => {
29+
let app: INestApplication;
30+
let url: string;
31+
32+
beforeAll(async () => {
33+
const moduleFixture: TestingModule = await Test.createTestingModule({
34+
imports: [AppModule],
35+
}).compile();
36+
37+
app = moduleFixture.createNestApplication();
38+
await app.listen(0);
39+
url = await app.getUrl();
40+
pactum.request.setBaseUrl(url.replace('[::1]', 'localhost'));
41+
});
42+
43+
afterAll(async () => {
44+
await app.close();
45+
});
46+
47+
describe('/graphql', () => {
48+
describe('cats', () => {
49+
it('should get the cats array', () => {
50+
return pactum
51+
.spec()
52+
.post('/graphql')
53+
.withGraphQLQuery(
54+
`query cats {
55+
getCats {
56+
id
57+
name
58+
age
59+
breed
60+
}
61+
}`,
62+
)
63+
.expectStatus(200)
64+
.expectBody({
65+
data: {
66+
getCats: cats,
67+
},
68+
});
69+
});
70+
});
71+
describe('one cat', () => {
72+
it('should get a single cat', () => {
73+
return pactum
74+
.spec()
75+
.post('/graphql')
76+
.withGraphQLQuery(
77+
`query cat($cat: CatInput!){
78+
getCat(catId:$cat) {
79+
id
80+
name
81+
age
82+
breed
83+
}
84+
}`,
85+
)
86+
.withGraphQLVariables({
87+
cat: {
88+
id: '2',
89+
},
90+
})
91+
.expectStatus(200)
92+
.expectBody({
93+
data: {
94+
getCat: {
95+
name: 'Terra',
96+
age: 5,
97+
breed: 'Siberian',
98+
id: '2',
99+
},
100+
},
101+
});
102+
});
103+
it('should get an error for a bad id', () => {
104+
return (
105+
pactum
106+
.spec()
107+
.post('/graphql')
108+
.withGraphQLQuery(
109+
`query cast($cat: CatInput!){
110+
getCat(catId:$cat){
111+
id
112+
name
113+
age
114+
breed
115+
}
116+
}`,
117+
)
118+
.withGraphQLVariables({
119+
cat: {
120+
id: '500',
121+
},
122+
})
123+
.expectStatus(200)
124+
// verify that the response has an error array with at least one object that has the following message
125+
.expectJsonLike({
126+
errors: [
127+
{
128+
message: 'No cat with id 500 found',
129+
},
130+
],
131+
})
132+
);
133+
});
134+
});
135+
it('should create a cat then retrieve all cats', async () => {
136+
const newCat = {
137+
name: 'Vanitas',
138+
breed: 'Calico',
139+
age: 100,
140+
};
141+
await pactum
142+
.spec()
143+
.post('/graphql')
144+
.withGraphQLQuery(
145+
`mutation makeACat($newCat:CatInsert!) {
146+
insertCat(newCat:$newCat){
147+
breed
148+
age
149+
id
150+
name
151+
}
152+
}`,
153+
)
154+
.withGraphQLVariables({
155+
newCat,
156+
})
157+
.expectStatus(200)
158+
.expectJson({
159+
data: {
160+
insertCat: {
161+
...newCat,
162+
id: '4',
163+
},
164+
},
165+
});
166+
await pactum
167+
.spec()
168+
.post('/graphql')
169+
.withGraphQLQuery(
170+
`query getAll {
171+
getCats {
172+
name
173+
age
174+
breed
175+
id
176+
}
177+
}`,
178+
)
179+
.expectStatus(200)
180+
.expectJson({
181+
data: {
182+
getCats: cats.concat([{ ...newCat, id: '4' }]),
183+
},
184+
});
185+
});
186+
});
187+
});

0 commit comments

Comments
 (0)