Skip to content

Instantly share code, notes, and snippets.

@unicornware
Last active January 15, 2022 06:02
Show Gist options
  • Save unicornware/e7bae3039e39987b7f6a2059c1c7ca61 to your computer and use it in GitHub Desktop.
Save unicornware/e7bae3039e39987b7f6a2059c1c7ca61 to your computer and use it in GitHub Desktop.
Chai Custom Matchers (TypeScript)
/**
* @file Custom Matchers - each
* @module tests/config/matchers/each
*/
/**
* Wraps each item in an array in a {@link chai.Assertion}.
*
* [1]: https://www.chaijs.com/api
* [2]: https://www.chaijs.com/api/plugins
*
* @param {Chai.ChaiStatic} chai - [`chai`][1] module
* @param {Chai.ChaiUtils} utils - [Plugin utilities][2]
* @return {void} Nothing when complete
*/
const each = (chai: Chai.ChaiStatic, utils: Chai.ChaiUtils): void => {
return utils.addMethod(
chai.Assertion.prototype,
each.name,
function (
this: Chai.Assertion,
fn: (item: Chai.Assertion, index: number) => any
): Chai.Assertion {
const array: any[] = utils.flag(this, 'object')
for (const [index, target] of array.entries()) {
fn(new chai.Assertion(target), index)
}
return this
}
)
}
export default each
// typings/chai/global.d.ts
declare module 'chai' {
import type { ExceptionCode } from '@flex-development/exceptions/enums'
import type { HttpStatus } from '@nestjs/common'
global {
export namespace Chai {
interface Assertion {
each(fn: (item: Chai.Assertion, index: number) => any): Chai.Assertion
jsonResponse(
status?: HttpStatus | ExceptionCode,
body?: 'array' | 'object'
): Chai.Assertion
}
}
}
}
import { ExceptionCode } from '@flex-development/exceptions/enums'
import { HttpStatus } from '@nestjs/common'
import type { Response } from 'superagent'
/**
* @file Custom Matchers - jsonResponse
* @module tests/config/jsonResponse
*/
/**
* Checks if a {@link Response} contains JSON content.
*
* The {@link Response#body} type and {@link Response#status} can be verified as
* well. Response bodies are expected to be an array or object.
*
* [1]: https://www.chaijs.com/api
* [2]: https://www.chaijs.com/api/plugins
*
* @see https://github.com/visionmedia/superagent
* @see https://github.com/chaijs/chai-http#assertions
*
* @param {Chai.ChaiStatic} chai - [`chai`][1] module
* @param {Chai.ChaiUtils} utils - [Plugin utilities][2]
* @return {void} Nothing when complete
*/
const jsonResponse = (chai: Chai.ChaiStatic, utils: Chai.ChaiUtils): void => {
return utils.addMethod(
chai.Assertion.prototype,
jsonResponse.name,
function (
this: Chai.Assertion,
estatus: HttpStatus | ExceptionCode = HttpStatus.OK,
ebody?: 'array' | 'object'
): Chai.Assertion {
const res: Response = utils.flag(this, 'object')
const message_body = `expected response body to be an ${ebody}`
new chai.Assertion(res).to.have.status(estatus)
new chai.Assertion(res).to.be.json
ebody && new chai.Assertion(res.body).to.be.an(ebody, message_body)
return this
}
)
}
export default jsonResponse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment