Skip to content

Commit 2778372

Browse files
committed
refactor to add needed assert methods to TAP, reverting last commit's approach
1 parent 71dd46c commit 2778372

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/user-agent/test.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ import StackUtils from 'stack-utils';
2020

2121
type SkipFunction = (...args: any[]) => any;
2222

23+
// Helper function to add required `TestUserAgent` assert methods
24+
// that are missing in a `TAP` instance. This is as an intended side effect
25+
// to ensure compatibility with `TestUserAgent` class below
26+
function addNodeAssertMethods(tapInstance: Test): void {
27+
(tapInstance as any).strictEqual = tapInstance.equal.bind(tapInstance);
28+
(tapInstance as any).notStrictEqual = tapInstance.not.bind(tapInstance);
29+
(tapInstance as any).doesNotMatch = tapInstance.notMatch.bind(tapInstance);
30+
(tapInstance as any).deepStrictEqual = tapInstance.same.bind(tapInstance);
31+
}
32+
2333
/**
2434
* Test user-agent class.
2535
*/
@@ -46,31 +56,23 @@ export class TestUserAgent extends MockUserAgent {
4656
* Delegate assertion to test framework currently in use.
4757
*/
4858
assert(name: string, args: any[], msg: string, skip: SkipFunction): void {
49-
const test: any = this._assert ?? {
50-
equal: assert.strictEqual,
51-
not: assert.notStrictEqual,
52-
match: assert.match,
53-
notMatch: assert.doesNotMatch,
54-
ok: assert.ok,
55-
notOk: (value: any, message: string) => assert.ok(!value, message),
56-
same: assert.deepStrictEqual
57-
};
59+
const test: any = this._assert ?? assert;
5860
test[name](...args, msg, {stack: this._stack.captureString(10, skip).replaceAll('file://', '')});
5961
}
6062

6163
/**
6264
* Check response content for exact match.
6365
*/
6466
bodyIs(body: string): this {
65-
this.assert('equal', [this.body.toString(), body], 'body is equal', this.bodyIs);
67+
this.assert('strictEqual', [this.body.toString(), body], 'body is equal', this.bodyIs);
6668
return this;
6769
}
6870

6971
/**
7072
* Opposite of `bodyIs`.
7173
*/
7274
bodyIsnt(body: string): this {
73-
this.assert('not', [this.body.toString(), body], 'body is not equal', this.bodyIsnt);
75+
this.assert('notStrictEqual', [this.body.toString(), body], 'body is not equal', this.bodyIsnt);
7476
return this;
7577
}
7678

@@ -86,7 +88,7 @@ export class TestUserAgent extends MockUserAgent {
8688
* Opposite of `bodyLike`.
8789
*/
8890
bodyUnlike(regex: RegExp): this {
89-
this.assert('notMatch', [this.body.toString(), regex], 'body is not similar', this.bodyUnlike);
91+
this.assert('doesNotMatch', [this.body.toString(), regex], 'body is not similar', this.bodyUnlike);
9092
return this;
9193
}
9294

@@ -105,7 +107,7 @@ export class TestUserAgent extends MockUserAgent {
105107
async closedOk(code: number): Promise<void> {
106108
await this._waitFinished();
107109
const finished = this._finished == null ? null : this._finished[0];
108-
this.assert('equal', [finished, code], `WebSocket closed with status ${code}`, this.closedOk);
110+
this.assert('strictEqual', [finished, code], `WebSocket closed with status ${code}`, this.closedOk);
109111
}
110112

111113
/**
@@ -159,23 +161,23 @@ export class TestUserAgent extends MockUserAgent {
159161
* Opposite of `headerExists`.
160162
*/
161163
headerExistsNot(name: string): this {
162-
this.assert('notOk', [this.res.get(name) !== null], `no "${name}" header`, this.headerExistsNot);
164+
this.assert('ok', [this.res.get(name) === null], `no "${name}" header`, this.headerExistsNot);
163165
return this;
164166
}
165167

166168
/**
167169
* Check response header for exact match.
168170
*/
169171
headerIs(name: string, value: string): this {
170-
this.assert('equal', [this.res.get(name), value], `${name}: ${value}`, this.headerIs);
172+
this.assert('strictEqual', [this.res.get(name), value], `${name}: ${value}`, this.headerIs);
171173
return this;
172174
}
173175

174176
/**
175177
* Opposite of `headerIs`.
176178
*/
177179
headerIsnt(name: string, value: string): this {
178-
this.assert('not', [this.res.get(name), value], `not ${name}: ${value}`, this.headerIsnt);
180+
this.assert('notStrictEqual', [this.res.get(name), value], `not ${name}: ${value}`, this.headerIsnt);
179181
return this;
180182
}
181183

@@ -202,7 +204,7 @@ export class TestUserAgent extends MockUserAgent {
202204
*/
203205
jsonIs(value: JSONValue, pointer = ''): this {
204206
const expected = jsonPointer(JSON.parse(this.body.toString()), pointer);
205-
this.assert('same', [expected, value], `exact match for JSON Pointer "${pointer}" (JSON)`, this.jsonIs);
207+
this.assert('deepStrictEqual', [expected, value], `exact match for JSON Pointer "${pointer}" (JSON)`, this.jsonIs);
206208
return this;
207209
}
208210

@@ -277,7 +279,7 @@ export class TestUserAgent extends MockUserAgent {
277279
* Check response status for exact match.
278280
*/
279281
statusIs(status: number): this {
280-
this.assert('equal', [this.res.statusCode, status], `response status is ${status}`, this.statusIs);
282+
this.assert('strictEqual', [this.res.statusCode, status], `response status is ${status}`, this.statusIs);
281283
return this;
282284
}
283285

@@ -299,7 +301,7 @@ export class TestUserAgent extends MockUserAgent {
299301
*/
300302
textUnlike(selector: string, regex: RegExp): this {
301303
this.assert(
302-
'notMatch',
304+
'doesNotMatch',
303305
[this._html.at(selector)?.text() ?? '', regex],
304306
`no similar match for selector "${selector}"`,
305307
this.textLike
@@ -311,7 +313,7 @@ export class TestUserAgent extends MockUserAgent {
311313
* Check response `Content-Type` header for exact match.
312314
*/
313315
typeIs(value: string): this {
314-
this.assert('equal', [this.res.type, value], `Content-Type: ${value}`, this.typeIs);
316+
this.assert('strictEqual', [this.res.type, value], `Content-Type: ${value}`, this.typeIs);
315317
return this;
316318
}
317319

@@ -361,7 +363,7 @@ export class TestUserAgent extends MockUserAgent {
361363
*/
362364
yamlIs(value: JSONValue, pointer = ''): this {
363365
const expected = jsonPointer(yaml.load(this.body.toString()) as JSONValue, pointer);
364-
this.assert('same', [expected, value], `exact match for JSON Pointer "${pointer}" (YAML)`, this.yamlIs);
366+
this.assert('deepStrictEqual', [expected, value], `exact match for JSON Pointer "${pointer}" (YAML)`, this.yamlIs);
365367
return this;
366368
}
367369

@@ -371,11 +373,13 @@ export class TestUserAgent extends MockUserAgent {
371373
}
372374

373375
_prepareTap(tap: Test): void {
376+
addNodeAssertMethods(tap);
374377
this._assert = tap;
375378
const subtests = [tap];
376379
const assert = this._assert;
377380

378381
assert.beforeEach(async t => {
382+
addNodeAssertMethods(t);
379383
subtests.push(t);
380384
this._assert = t;
381385
});

test/test-user-agent.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ t.test('TestUserAgent', async t => {
2828

2929
app.get('/target', ctx => ctx.render({text: 'Hi'}));
3030

31-
for (const useTap of [true, false]) {
31+
for (const useTap of [false, true]) {
3232
const ua = useTap ? await app.newTestUserAgent({tap: t}) : await app.newTestUserAgent();
3333

3434
await t.test('Status Assertion test', async () => {
@@ -96,7 +96,7 @@ t.test('TestUserAgent', async t => {
9696

9797
t.same(results, [
9898
['ok', [true], 'GET request for /'],
99-
['equal', [200, 200], 'response status is 200']
99+
['strictEqual', [200, 200], 'response status is 200']
100100
]);
101101
});
102102

@@ -115,12 +115,12 @@ t.test('TestUserAgent', async t => {
115115

116116
t.same(results, [
117117
['ok', [true], 'GET request for /'],
118-
['equal', ['text/plain; charset=utf-8', 'text/plain'], 'Content-Type: text/plain'],
118+
['strictEqual', ['text/plain; charset=utf-8', 'text/plain'], 'Content-Type: text/plain'],
119119
['match', ['text/plain; charset=utf-8', /plain/], 'Content-Type is similar'],
120120
['ok', [true], 'header "Content-Type" exists'],
121-
['notOk', [false], 'no "Content-Disposition" header'],
122-
['equal', ['text/plain; charset=utf-8', 'text/plain'], 'Content-Type: text/plain'],
123-
['not', ['text/plain; charset=utf-8', 'text/plain'], 'not Content-Type: text/plain'],
121+
['ok', [true], 'no "Content-Disposition" header'],
122+
['strictEqual', ['text/plain; charset=utf-8', 'text/plain'], 'Content-Type: text/plain'],
123+
['notStrictEqual', ['text/plain; charset=utf-8', 'text/plain'], 'not Content-Type: text/plain'],
124124
['match', ['text/plain; charset=utf-8', /plain/], 'Content-Type is similar']
125125
]);
126126
});
@@ -137,10 +137,10 @@ t.test('TestUserAgent', async t => {
137137

138138
t.same(results, [
139139
['ok', [true], 'GET request for /'],
140-
['equal', ['Hello Mojo!', 'test'], 'body is equal'],
141-
['not', ['Hello Mojo!', 'test'], 'body is not equal'],
140+
['strictEqual', ['Hello Mojo!', 'test'], 'body is equal'],
141+
['notStrictEqual', ['Hello Mojo!', 'test'], 'body is not equal'],
142142
['match', ['Hello Mojo!', /that/], 'body is similar'],
143-
['notMatch', ['Hello Mojo!', /whatever/], 'body is not similar']
143+
['doesNotMatch', ['Hello Mojo!', /whatever/], 'body is not similar']
144144
]);
145145
});
146146

@@ -166,8 +166,8 @@ t.test('TestUserAgent', async t => {
166166
['ok', [false], 'no element for selector "div"'],
167167
['match', ['Two', /test/], 'similar match for selector "div"'],
168168
['match', ['', /123/], 'similar match for selector "nothing"'],
169-
['notMatch', ['Two', /test/], 'no similar match for selector "div"'],
170-
['notMatch', ['', /123/], 'no similar match for selector "nothing"']
169+
['doesNotMatch', ['Two', /test/], 'no similar match for selector "div"'],
170+
['doesNotMatch', ['', /123/], 'no similar match for selector "nothing"']
171171
]);
172172

173173
results = [];
@@ -196,15 +196,15 @@ t.test('TestUserAgent', async t => {
196196
['ok', [true], 'has value for JSON Pointer "/a" (JSON)'],
197197
['ok', [false], 'has value for JSON Pointer "/d" (JSON)'],
198198
[
199-
'same',
199+
'deepStrictEqual',
200200
[
201201
['b', 'c'],
202202
['b', 'c']
203203
],
204204
'exact match for JSON Pointer "/a" (JSON)'
205205
],
206-
['same', [undefined, 'e'], 'exact match for JSON Pointer "/d" (JSON)'],
207-
['same', [{a: ['b', 'c']}, {a: ['b', 'c']}], 'exact match for JSON Pointer "" (JSON)']
206+
['deepStrictEqual', [undefined, 'e'], 'exact match for JSON Pointer "/d" (JSON)'],
207+
['deepStrictEqual', [{a: ['b', 'c']}, {a: ['b', 'c']}], 'exact match for JSON Pointer "" (JSON)']
208208
]);
209209
});
210210

@@ -224,15 +224,15 @@ t.test('TestUserAgent', async t => {
224224
['ok', [true], 'has value for JSON Pointer "/a" (YAML)'],
225225
['ok', [false], 'has value for JSON Pointer "/d" (YAML)'],
226226
[
227-
'same',
227+
'deepStrictEqual',
228228
[
229229
['b', 'c'],
230230
['b', 'c']
231231
],
232232
'exact match for JSON Pointer "/a" (YAML)'
233233
],
234-
['same', [undefined, 'e'], 'exact match for JSON Pointer "/d" (YAML)'],
235-
['same', [{a: ['b', 'c']}, {a: ['b', 'c']}], 'exact match for JSON Pointer "" (YAML)']
234+
['deepStrictEqual', [undefined, 'e'], 'exact match for JSON Pointer "/d" (YAML)'],
235+
['deepStrictEqual', [{a: ['b', 'c']}, {a: ['b', 'c']}], 'exact match for JSON Pointer "" (YAML)']
236236
]);
237237
});
238238

@@ -261,7 +261,7 @@ t.test('TestUserAgent', async t => {
261261
['ok', [true], 'send message'],
262262
['ok', [true], 'message received'],
263263
['ok', [true], 'closed WebSocket'],
264-
['equal', [1000, 1000], 'WebSocket closed with status 1000']
264+
['strictEqual', [1000, 1000], 'WebSocket closed with status 1000']
265265
]);
266266
});
267267

0 commit comments

Comments
 (0)