Skip to content

Commit f0a16f9

Browse files
authored
feat: port first half of fetch tests to node test runner (#2569)
1 parent d00b822 commit f0a16f9

22 files changed

+832
-939
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"test": "node scripts/generate-pem && npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && npm run test:typescript && npm run test:node-test",
7979
"test:cookies": "borp --coverage -p \"test/cookie/*.js\"",
8080
"test:node-fetch": "mocha --exit test/node-fetch",
81-
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && borp --coverage -p \"test/webidl/*.js\"",
81+
"test:fetch": "npm run build:node && borp --expose-gc --coverage -p \"test/fetch/*.js\" && borp --coverage -p \"test/webidl/*.js\"",
8282
"test:jest": "jest",
8383
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
8484
"test:node-test": "borp --coverage -p \"test/node-test/*.js\"",
@@ -102,7 +102,7 @@
102102
"@types/node": "^18.0.3",
103103
"abort-controller": "^3.0.0",
104104
"atomic-sleep": "^1.0.0",
105-
"borp": "^0.4.2",
105+
"borp": "^0.5.0",
106106
"chai": "^4.3.4",
107107
"chai-as-promised": "^7.1.1",
108108
"chai-iterator": "^3.0.2",

test/fetch/407-statuscode-window-null.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
const { fetch } = require('../..')
44
const { createServer } = require('http')
55
const { once } = require('events')
6-
const { test } = require('tap')
6+
const { test } = require('node:test')
7+
const assert = require('node:assert')
78

89
test('Receiving a 407 status code w/ a window option present should reject', async (t) => {
910
const server = createServer((req, res) => {
1011
res.statusCode = 407
1112
res.end()
1213
}).listen(0)
1314

14-
t.teardown(server.close.bind(server))
15+
t.after(server.close.bind(server))
1516
await once(server, 'listening')
1617

1718
// if init.window exists, the spec tells us to set request.window to 'no-window',
1819
// which later causes the request to be rejected if the status code is 407
19-
await t.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
20+
await assert.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
2021
})

test/fetch/abort.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
5+
const { tspl } = require('@matteo.collina/tspl')
46
const { fetch } = require('../..')
57
const { createServer } = require('http')
68
const { once } = require('events')
@@ -17,7 +19,7 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
1719
res.end(JSON.stringify(body))
1820
})
1921

20-
t.teardown(server.close.bind(server))
22+
t.after(server.close.bind(server))
2123

2224
server.listen(0)
2325
await once(server, 'listening')
@@ -31,43 +33,42 @@ test('Allow the usage of custom implementation of AbortController', async (t) =>
3133
signal
3234
})
3335
} catch (e) {
34-
t.equal(e.code, DOMException.ABORT_ERR)
36+
assert.strictEqual(e.code, DOMException.ABORT_ERR)
3537
}
3638
})
3739

3840
test('allows aborting with custom errors', async (t) => {
3941
const server = createServer().listen(0)
4042

41-
t.teardown(server.close.bind(server))
43+
t.after(server.close.bind(server))
4244
await once(server, 'listening')
4345

44-
t.test('Using AbortSignal.timeout with cause', async (t) => {
45-
t.plan(2)
46-
46+
await t.test('Using AbortSignal.timeout with cause', async () => {
47+
const { strictEqual } = tspl(t, { plan: 2 })
4748
try {
4849
await fetch(`http://localhost:${server.address().port}`, {
4950
signal: AbortSignal.timeout(50)
5051
})
51-
t.fail('should throw')
52+
assert.fail('should throw')
5253
} catch (err) {
5354
if (err.name === 'TypeError') {
5455
const cause = err.cause
55-
t.equal(cause.name, 'HeadersTimeoutError')
56-
t.equal(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
56+
strictEqual(cause.name, 'HeadersTimeoutError')
57+
strictEqual(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
5758
} else if (err.name === 'TimeoutError') {
58-
t.equal(err.code, DOMException.TIMEOUT_ERR)
59-
t.equal(err.cause, undefined)
59+
strictEqual(err.code, DOMException.TIMEOUT_ERR)
60+
strictEqual(err.cause, undefined)
6061
} else {
61-
t.error(err)
62+
throw err
6263
}
6364
}
6465
})
6566

66-
t.test('Error defaults to an AbortError DOMException', async (t) => {
67+
t.test('Error defaults to an AbortError DOMException', async () => {
6768
const ac = new AbortController()
6869
ac.abort() // no reason
6970

70-
await t.rejects(
71+
await assert.rejects(
7172
fetch(`http://localhost:${server.address().port}`, {
7273
signal: ac.signal
7374
}),

test/fetch/abort2.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
45
const { fetch } = require('../..')
56
const { createServer } = require('http')
67
const { once } = require('events')
@@ -18,7 +19,7 @@ test('parallel fetch with the same AbortController works as expected', async (t)
1819
res.end(JSON.stringify(body))
1920
})
2021

21-
t.teardown(server.close.bind(server))
22+
t.after(server.close.bind(server))
2223

2324
const abortController = new AbortController()
2425

@@ -49,11 +50,9 @@ test('parallel fetch with the same AbortController works as expected', async (t)
4950
return a
5051
}, { resolved: [], rejected: [] })
5152

52-
t.equal(rejected.length, 9) // out of 10 requests, only 1 should succeed
53-
t.equal(resolved.length, 1)
53+
assert.strictEqual(rejected.length, 9) // out of 10 requests, only 1 should succeed
54+
assert.strictEqual(resolved.length, 1)
5455

55-
t.ok(rejected.every(rej => rej.reason?.code === DOMException.ABORT_ERR))
56-
t.same(resolved[0].value, body)
57-
58-
t.end()
56+
assert.ok(rejected.every(rej => rej.reason?.code === DOMException.ABORT_ERR))
57+
assert.deepStrictEqual(resolved[0].value, body)
5958
})

test/fetch/about-uri.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
45
const { fetch } = require('../..')
56

67
test('fetching about: uris', async (t) => {
7-
t.test('about:blank', async (t) => {
8-
await t.rejects(fetch('about:blank'))
8+
await t.test('about:blank', async () => {
9+
await assert.rejects(fetch('about:blank'))
910
})
1011

11-
t.test('All other about: urls should return an error', async (t) => {
12+
await t.test('All other about: urls should return an error', async () => {
1213
try {
1314
await fetch('about:config')
14-
t.fail('fetching about:config should fail')
15+
assert.fail('fetching about:config should fail')
1516
} catch (e) {
16-
t.ok(e, 'this error was expected')
17-
} finally {
18-
t.end()
17+
assert.ok(e, 'this error was expected')
1918
}
2019
})
2120
})

test/fetch/blob-uri.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
45
const { fetch } = require('../..')
56
const { Blob } = require('buffer')
67

@@ -16,85 +17,73 @@ test('fetching blob: uris', async (t) => {
1617
objectURL = URL.createObjectURL(blob)
1718
})
1819

19-
t.test('a normal fetch request works', async (t) => {
20+
await t.test('a normal fetch request works', async () => {
2021
const res = await fetch(objectURL)
2122

22-
t.equal(blobContents, await res.text())
23-
t.equal(blob.type, res.headers.get('Content-Type'))
24-
t.equal(`${blob.size}`, res.headers.get('Content-Length'))
25-
t.end()
23+
assert.strictEqual(blobContents, await res.text())
24+
assert.strictEqual(blob.type, res.headers.get('Content-Type'))
25+
assert.strictEqual(`${blob.size}`, res.headers.get('Content-Length'))
2626
})
2727

28-
t.test('non-GET method to blob: fails', async (t) => {
28+
await t.test('non-GET method to blob: fails', async () => {
2929
try {
3030
await fetch(objectURL, {
3131
method: 'POST'
3232
})
33-
t.fail('expected POST to blob: uri to fail')
33+
assert.fail('expected POST to blob: uri to fail')
3434
} catch (e) {
35-
t.ok(e, 'Got the expected error')
36-
} finally {
37-
t.end()
35+
assert.ok(e, 'Got the expected error')
3836
}
3937
})
4038

4139
// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L36-L41
42-
t.test('fetching revoked URL should fail', async (t) => {
40+
await t.test('fetching revoked URL should fail', async () => {
4341
URL.revokeObjectURL(objectURL)
4442

4543
try {
4644
await fetch(objectURL)
47-
t.fail('expected revoked blob: url to fail')
45+
assert.fail('expected revoked blob: url to fail')
4846
} catch (e) {
49-
t.ok(e, 'Got the expected error')
50-
} finally {
51-
t.end()
47+
assert.ok(e, 'Got the expected error')
5248
}
5349
})
5450

5551
// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L28-L34
56-
t.test('works with a fragment', async (t) => {
52+
await t.test('works with a fragment', async () => {
5753
const res = await fetch(objectURL + '#fragment')
5854

59-
t.equal(blobContents, await res.text())
60-
t.end()
55+
assert.strictEqual(blobContents, await res.text())
6156
})
6257

6358
// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L52-L56
64-
t.test('Appending a query string to blob: url should cause fetch to fail', async (t) => {
59+
await t.test('Appending a query string to blob: url should cause fetch to fail', async () => {
6560
try {
6661
await fetch(objectURL + '?querystring')
67-
t.fail('expected ?querystring blob: url to fail')
62+
assert.fail('expected ?querystring blob: url to fail')
6863
} catch (e) {
69-
t.ok(e, 'Got the expected error')
70-
} finally {
71-
t.end()
64+
assert.ok(e, 'Got the expected error')
7265
}
7366
})
7467

7568
// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L58-L62
76-
t.test('Appending a path should cause fetch to fail', async (t) => {
69+
await t.test('Appending a path should cause fetch to fail', async () => {
7770
try {
7871
await fetch(objectURL + '/path')
79-
t.fail('expected /path blob: url to fail')
72+
assert.fail('expected /path blob: url to fail')
8073
} catch (e) {
81-
t.ok(e, 'Got the expected error')
82-
} finally {
83-
t.end()
74+
assert.ok(e, 'Got the expected error')
8475
}
8576
})
8677

8778
// https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L64-L70
88-
t.test('these http methods should fail', async (t) => {
79+
await t.test('these http methods should fail', async () => {
8980
for (const method of ['HEAD', 'POST', 'DELETE', 'OPTIONS', 'PUT', 'CUSTOM']) {
9081
try {
9182
await fetch(objectURL, { method })
92-
t.fail(`${method} fetch should have failed`)
83+
assert.fail(`${method} fetch should have failed`)
9384
} catch (e) {
94-
t.ok(e, `${method} blob url - test succeeded`)
85+
assert.ok(e, `${method} blob url - test succeeded`)
9586
}
9687
}
97-
98-
t.end()
9988
})
10089
})

test/fetch/bundle.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
45

56
const { Response, Request, FormData, Headers } = require('../../undici-fetch')
67

7-
test('bundle sets constructor.name and .name properly', (t) => {
8-
t.equal(new Response().constructor.name, 'Response')
9-
t.equal(Response.name, 'Response')
8+
test('bundle sets constructor.name and .name properly', () => {
9+
assert.strictEqual(new Response().constructor.name, 'Response')
10+
assert.strictEqual(Response.name, 'Response')
1011

11-
t.equal(new Request('http://a').constructor.name, 'Request')
12-
t.equal(Request.name, 'Request')
12+
assert.strictEqual(new Request('http://a').constructor.name, 'Request')
13+
assert.strictEqual(Request.name, 'Request')
1314

14-
t.equal(new Headers().constructor.name, 'Headers')
15-
t.equal(Headers.name, 'Headers')
15+
assert.strictEqual(new Headers().constructor.name, 'Headers')
16+
assert.strictEqual(Headers.name, 'Headers')
1617

17-
t.equal(new FormData().constructor.name, 'FormData')
18-
t.equal(FormData.name, 'FormData')
19-
20-
t.end()
18+
assert.strictEqual(new FormData().constructor.name, 'FormData')
19+
assert.strictEqual(FormData.name, 'FormData')
2120
})
2221

23-
test('regression test for https://github.com/nodejs/node/issues/50263', (t) => {
22+
test('regression test for https://github.com/nodejs/node/issues/50263', () => {
2423
const request = new Request('https://a', {
2524
headers: {
2625
test: 'abc'
@@ -30,6 +29,5 @@ test('regression test for https://github.com/nodejs/node/issues/50263', (t) => {
3029

3130
const request1 = new Request(request, { body: 'does not matter' })
3231

33-
t.equal(request1.headers.get('test'), 'abc')
34-
t.end()
32+
assert.strictEqual(request1.headers.get('test'), 'abc')
3533
})
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
4+
const assert = require('node:assert')
45
const { fetch } = require('../..')
56
const { fetch: fetchIndex } = require('../../index-fetch')
67

78
test('FETCH: request errors and prints trimmed stack trace', async (t) => {
89
try {
910
await fetch('http://a.com')
1011
} catch (error) {
11-
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
12+
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
1213
}
1314
})
1415

1516
test('FETCH-index: request errors and prints trimmed stack trace', async (t) => {
1617
try {
1718
await fetchIndex('http://a.com')
1819
} catch (error) {
19-
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
20+
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
2021
}
2122
})

0 commit comments

Comments
 (0)