Skip to content

Commit a58b021

Browse files
author
DavertMik
committed
removed dep on chai in JSONResponse
1 parent a86706f commit a58b021

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

lib/helper/JSONResponse.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const Helper = require('@codeceptjs/helper')
2-
3-
const chai = require('chai')
4-
5-
const expect = chai.expect
6-
chai.use(require('chai-deep-match'))
2+
const assert = require('assert')
73
const joi = require('joi')
84

95
/**
@@ -107,7 +103,7 @@ class JSONResponse extends Helper {
107103
*/
108104
seeResponseCodeIs(code) {
109105
this._checkResponseReady()
110-
expect(this.response.status).to.eql(code, 'Response code is not the same as expected')
106+
assert.strictEqual(this.response.status, code, 'Response code is not the same as expected')
111107
}
112108

113109
/**
@@ -121,34 +117,31 @@ class JSONResponse extends Helper {
121117
*/
122118
dontSeeResponseCodeIs(code) {
123119
this._checkResponseReady()
124-
expect(this.response.status).not.to.eql(code)
120+
assert.notStrictEqual(this.response.status, code)
125121
}
126122

127123
/**
128124
* Checks that the response code is 4xx
129125
*/
130126
seeResponseCodeIsClientError() {
131127
this._checkResponseReady()
132-
expect(this.response.status).to.be.gte(400)
133-
expect(this.response.status).to.be.lt(500)
128+
assert(this.response.status >= 400 && this.response.status < 500)
134129
}
135130

136131
/**
137132
* Checks that the response code is 3xx
138133
*/
139134
seeResponseCodeIsRedirection() {
140135
this._checkResponseReady()
141-
expect(this.response.status).to.be.gte(300)
142-
expect(this.response.status).to.be.lt(400)
136+
assert(this.response.status >= 300 && this.response.status < 400)
143137
}
144138

145139
/**
146140
* Checks that the response code is 5xx
147141
*/
148142
seeResponseCodeIsServerError() {
149143
this._checkResponseReady()
150-
expect(this.response.status).to.be.gte(500)
151-
expect(this.response.status).to.be.lt(600)
144+
assert(this.response.status >= 500 && this.response.status < 600)
152145
}
153146

154147
/**
@@ -161,8 +154,7 @@ class JSONResponse extends Helper {
161154
*/
162155
seeResponseCodeIsSuccessful() {
163156
this._checkResponseReady()
164-
expect(this.response.status).to.be.gte(200)
165-
expect(this.response.status).to.be.lt(300)
157+
assert(this.response.status >= 200 && this.response.status < 300)
166158
}
167159

168160
/**
@@ -185,17 +177,19 @@ class JSONResponse extends Helper {
185177
seeResponseContainsJson(json = {}) {
186178
this._checkResponseReady()
187179
if (Array.isArray(this.response.data)) {
188-
let fails = 0
180+
let found = false
189181
for (const el of this.response.data) {
190182
try {
191-
expect(el).to.deep.match(json)
183+
this._assertContains(el, json)
184+
found = true
185+
break
192186
} catch (err) {
193-
fails++
187+
continue
194188
}
195189
}
196-
expect(fails < this.response.data.length, `No elements in array matched ${JSON.stringify(json)}`).to.be.true
190+
assert(found, `No elements in array matched ${JSON.stringify(json)}`)
197191
} else {
198-
expect(this.response.data).to.deep.match(json)
192+
this._assertContains(this.response.data, json)
199193
}
200194
}
201195

@@ -219,9 +213,22 @@ class JSONResponse extends Helper {
219213
dontSeeResponseContainsJson(json = {}) {
220214
this._checkResponseReady()
221215
if (Array.isArray(this.response.data)) {
222-
this.response.data.forEach((data) => expect(data).not.to.deep.match(json))
216+
for (const data of this.response.data) {
217+
try {
218+
this._assertContains(data, json)
219+
assert.fail(`Found matching element: ${JSON.stringify(data)}`)
220+
} catch (err) {
221+
// expected to fail
222+
continue
223+
}
224+
}
223225
} else {
224-
expect(this.response.data).not.to.deep.match(json)
226+
try {
227+
this._assertContains(this.response.data, json)
228+
assert.fail('Response contains the JSON')
229+
} catch (err) {
230+
// expected to fail
231+
}
225232
}
226233
}
227234

@@ -247,28 +254,35 @@ class JSONResponse extends Helper {
247254
seeResponseContainsKeys(keys = []) {
248255
this._checkResponseReady()
249256
if (Array.isArray(this.response.data)) {
250-
this.response.data.forEach((data) => expect(data).to.include.keys(keys))
257+
for (const data of this.response.data) {
258+
for (const key of keys) {
259+
assert(key in data, `Key "${key}" is not found in ${JSON.stringify(data)}`)
260+
}
261+
}
251262
} else {
252-
expect(this.response.data).to.include.keys(keys)
263+
for (const key of keys) {
264+
assert(key in this.response.data, `Key "${key}" is not found in ${JSON.stringify(this.response.data)}`)
265+
}
253266
}
254267
}
255268

256269
/**
257-
* Executes a callback function passing in `response` object and chai assertions with `expect`
270+
* Executes a callback function passing in `response` object and assert
258271
* Use it to perform custom checks of response data
259272
*
260273
* ```js
261-
* I.seeResponseValidByCallback(({ data, status, expect }) => {
262-
* expect(status).to.eql(200);
263-
* expect(data).keys.to.include(['user', 'company']);
274+
* I.seeResponseValidByCallback(({ data, status }) => {
275+
* assert.strictEqual(status, 200);
276+
* assert('user' in data);
277+
* assert('company' in data);
264278
* });
265279
* ```
266280
*
267281
* @param {function} fn
268282
*/
269283
seeResponseValidByCallback(fn) {
270284
this._checkResponseReady()
271-
fn({ ...this.response, expect })
285+
fn({ ...this.response, assert })
272286
const body = fn.toString()
273287
fn.toString = () => `${body.split('\n')[1]}...`
274288
}
@@ -285,7 +299,7 @@ class JSONResponse extends Helper {
285299
*/
286300
seeResponseEquals(resp) {
287301
this._checkResponseReady()
288-
expect(this.response.data).to.deep.equal(resp)
302+
assert.deepStrictEqual(this.response.data, resp)
289303
}
290304

291305
/**
@@ -332,6 +346,17 @@ class JSONResponse extends Helper {
332346
_checkResponseReady() {
333347
if (!this.response) throw new Error('Response is not available')
334348
}
349+
350+
_assertContains(actual, expected) {
351+
for (const key in expected) {
352+
assert(key in actual, `Key "${key}" not found in ${JSON.stringify(actual)}`)
353+
if (typeof expected[key] === 'object' && expected[key] !== null) {
354+
this._assertContains(actual[key], expected[key])
355+
} else {
356+
assert.deepStrictEqual(actual[key], expected[key], `Values for key "${key}" don't match`)
357+
}
358+
}
359+
}
335360
}
336361

337362
module.exports = JSONResponse

0 commit comments

Comments
 (0)