|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const { assert } = require('chai'); |
| 3 | +const { use } = require('chai'); |
4 | 4 |
|
5 | | -/** |
6 | | - * Asserts whether the return status was as expected |
7 | | - * |
8 | | - * @param {Object} res |
9 | | - * @param {integer} expected |
10 | | - */ |
11 | | -function status(res, expected) { |
| 5 | +module.exports = use(function (_chai, _utils) { |
| 6 | + const { assert } = _chai; |
12 | 7 |
|
13 | | - assert.deepEqual(res.status, expected, |
14 | | - `Expected status to be ${expected}, but was ${res.status}`); |
| 8 | + assert.status = (res, expected) => { |
| 9 | + const msg = `Expected status to be ${expected}, but was ${res.status}`; |
| 10 | + new _chai.Assertion(res.status, msg, assert.status, true).to.eql(expected); |
| 11 | + }; |
15 | 12 |
|
16 | | -} |
| 13 | + assert.contentType = (res, expectedRegexString) => { |
| 14 | + const actual = res.headers['content-type']; |
| 15 | + const msg = `Expected content-type to match ${expectedRegexString}, but was ${actual}`; |
| 16 | + new _chai.Assertion(actual, msg, assert.contentType, true).to.match(RegExp(expectedRegexString)); |
| 17 | + }; |
17 | 18 |
|
18 | | -/** |
19 | | - * Asserts whether content type was as expected |
20 | | - * |
21 | | - * @param {Object} res |
22 | | - * @param {string} expectedRegexString |
23 | | - */ |
24 | | -function contentType(res, expectedRegexString) { |
| 19 | + assert.fails = (promise, onRejected) => { |
25 | 20 |
|
26 | | - const actual = res.headers['content-type']; |
27 | | - assert.ok(RegExp(expectedRegexString).test(actual), |
28 | | - `Expected content-type to match ${expectedRegexString}, but was ${actual}`); |
| 21 | + let failed = false; |
29 | 22 |
|
30 | | -} |
31 | | - |
32 | | -function fails(promise, onRejected) { |
33 | | - |
34 | | - let failed = false; |
35 | | - |
36 | | - function trackFailure(e) { |
37 | | - failed = true; |
38 | | - return onRejected(e); |
39 | | - } |
40 | | - |
41 | | - function check() { |
42 | | - if (!failed) { |
43 | | - throw new Error('expected error was not thrown'); |
| 23 | + function trackFailure(e) { |
| 24 | + failed = true; |
| 25 | + return onRejected(e); |
44 | 26 | } |
45 | | - } |
46 | 27 |
|
47 | | - return promise.catch(trackFailure).then(check); |
| 28 | + function check() { |
| 29 | + if (!failed) { |
| 30 | + throw new Error('expected error was not thrown'); |
| 31 | + } |
| 32 | + } |
| 33 | + return promise.catch(trackFailure).then(check); |
48 | 34 |
|
49 | | -} |
| 35 | + }; |
50 | 36 |
|
51 | | -module.exports.assert = assert; |
52 | | -module.exports.assert.contentType = contentType; |
53 | | -module.exports.assert.status = status; |
54 | | -module.exports.assert.fails = fails; |
| 37 | +}).assert; |
0 commit comments