Skip to content

Commit 43d2f00

Browse files
authored
Merge pull request #220 from AthennaIO/develop
test(http): validate nested properties in responde
2 parents 72eb802 + 110b00c commit 43d2f00

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/http",
3-
"version": "5.34.0",
3+
"version": "5.35.0",
44
"description": "The Athenna Http server. Built on top of fastify.",
55
"license": "MIT",
66
"author": "João Lenon <[email protected]>",

src/testing/plugins/request/TestResponse.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import { Assert } from '@japa/assert'
11-
import { Macroable } from '@athenna/common'
11+
import { Json, Macroable } from '@athenna/common'
1212
import type { Response } from 'light-my-request'
1313

1414
export class TestResponse extends Macroable {
@@ -104,7 +104,13 @@ export class TestResponse extends Macroable {
104104
* ```
105105
*/
106106
public assertBodyContainsKey(key: string) {
107-
this.assert.property(this.response.json(), key)
107+
const body = this.response.json()
108+
const value = Json.get(body, key)
109+
110+
this.assert.assert(
111+
value !== undefined,
112+
`The body does not contain the key ${key}`
113+
)
108114
}
109115

110116
/**
@@ -124,7 +130,10 @@ export class TestResponse extends Macroable {
124130
* ```
125131
*/
126132
public assertBodyNotContainsKey(key: string) {
127-
this.assert.notProperty(this.response.json(), key)
133+
const body = this.response.json()
134+
const value = Json.get(body, key)
135+
136+
this.assert.assert(value === undefined, `The body contains the key ${key}`)
128137
}
129138

130139
/**
@@ -138,7 +147,22 @@ export class TestResponse extends Macroable {
138147
* ```
139148
*/
140149
public assertBodyContainsAllKeys(keys: string[]) {
141-
this.assert.properties(this.response.json(), keys)
150+
const body = this.response.json()
151+
const seenKeys = new Set()
152+
153+
for (const key of keys) {
154+
const value = Json.get(body, key)
155+
156+
if (value !== undefined) {
157+
seenKeys.add(key)
158+
}
159+
}
160+
161+
if (seenKeys.size !== keys.length) {
162+
return this.assert.fail(
163+
`The body does not contain all keys: ${keys.join(', ')}`
164+
)
165+
}
142166
}
143167

144168
/**
@@ -158,7 +182,21 @@ export class TestResponse extends Macroable {
158182
* ```
159183
*/
160184
public assertBodyNotContainsAllKeys(keys: string[]) {
161-
this.assert.notAllProperties(this.response.json(), keys)
185+
const body = this.response.json()
186+
const seenKeys = new Set()
187+
188+
for (const key of keys) {
189+
const value = Json.get(body, key)
190+
191+
if (value !== undefined) {
192+
seenKeys.add(key)
193+
}
194+
}
195+
196+
this.assert.assert(
197+
seenKeys.size,
198+
`The body contains keys: ${keys.join(', ')}`
199+
)
162200
}
163201

164202
/**

0 commit comments

Comments
 (0)