From 938e7b47655822c25b5fa9c8073be6bfbb8f6c29 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Mon, 12 Feb 2024 12:01:52 +0100 Subject: [PATCH] chore: migrate a batch of tests to node test runner (#2739) --- test/mock-agent.js | 775 +++++++++++---------- test/mock-client.js | 140 ++-- test/mock-errors.js | 41 +- test/mock-interceptor-unused-assertions.js | 104 +-- test/mock-interceptor.js | 126 ++-- test/mock-pool.js | 136 ++-- test/mock-scope.js | 35 +- test/mock-utils.js | 63 +- 8 files changed, 703 insertions(+), 717 deletions(-) diff --git a/test/mock-agent.js b/test/mock-agent.js index ed58464b527..80b04e658e8 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after, describe } = require('node:test') const { createServer } = require('node:http') const { promisify } = require('node:util') const { request, setGlobalDispatcher, MockAgent, Agent } = require('..') @@ -14,93 +15,87 @@ const Dispatcher = require('../lib/dispatcher') const { MockNotMatchedError } = require('../lib/mock/mock-errors') const { fetch } = require('..') -test('MockAgent - constructor', t => { - t.plan(5) - - t.test('sets up mock agent', t => { - t.plan(1) +describe('MockAgent - constructor', () => { + test('sets up mock agent', t => { + t = tspl(t, { plan: 1 }) t.doesNotThrow(() => new MockAgent()) }) - t.test('should implement the Dispatcher API', t => { - t.plan(1) + test('should implement the Dispatcher API', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() t.ok(mockAgent instanceof Dispatcher) }) - t.test('sets up mock agent with single connection', t => { - t.plan(1) + test('sets up mock agent with single connection', t => { + t = tspl(t, { plan: 1 }) t.doesNotThrow(() => new MockAgent({ connections: 1 })) }) - t.test('should error passed agent is not valid', t => { - t.plan(2) + test('should error passed agent is not valid', t => { + t = tspl(t, { plan: 2 }) t.throws(() => new MockAgent({ agent: {} }), new InvalidArgumentError('Argument opts.agent must implement Agent')) t.throws(() => new MockAgent({ agent: { dispatch: '' } }), new InvalidArgumentError('Argument opts.agent must implement Agent')) }) - t.test('should be able to specify the agent to mock', t => { - t.plan(1) + test('should be able to specify the agent to mock', t => { + t = tspl(t, { plan: 1 }) const agent = new Agent() - t.teardown(agent.close.bind(agent)) + after(() => agent.close()) const mockAgent = new MockAgent({ agent }) - t.equal(mockAgent[kAgent], agent) + t.strictEqual(mockAgent[kAgent], agent) }) }) -test('MockAgent - get', t => { - t.plan(3) - - t.test('should return MockClient', (t) => { - t.plan(1) +describe('MockAgent - get', t => { + test('should return MockClient', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) }) - t.test('should return MockPool', (t) => { - t.plan(1) + test('should return MockPool', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) t.ok(mockPool instanceof MockPool) }) - t.test('should return the same instance if already created', (t) => { - t.plan(1) + test('should return the same instance if already created', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool1 = mockAgent.get(baseUrl) const mockPool2 = mockAgent.get(baseUrl) - t.equal(mockPool1, mockPool2) + t.strictEqual(mockPool1, mockPool2) }) }) -test('MockAgent - dispatch', t => { - t.plan(3) - - t.test('should call the dispatch method of the MockPool', (t) => { - t.plan(1) +describe('MockAgent - dispatch', () => { + test('should call the dispatch method of the MockPool', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -121,13 +116,13 @@ test('MockAgent - dispatch', t => { })) }) - t.test('should call the dispatch method of the MockClient', (t) => { - t.plan(1) + test('should call the dispatch method of the MockClient', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) @@ -148,13 +143,13 @@ test('MockAgent - dispatch', t => { })) }) - t.test('should throw if handler is not valid on redirect', (t) => { - t.plan(7) + test('should throw if handler is not valid on redirect', (t) => { + t = tspl(t, { plan: 7 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) t.throws(() => mockAgent.dispatch({ origin: baseUrl, @@ -233,7 +228,7 @@ test('MockAgent - dispatch', t => { }) test('MockAgent - .close should clean up registered pools', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const baseUrl = 'http://localhost:9999' @@ -243,15 +238,15 @@ test('MockAgent - .close should clean up registered pools', async (t) => { const mockPool = mockAgent.get(baseUrl) t.ok(mockPool instanceof MockPool) - t.equal(mockPool[kConnected], 1) - t.equal(mockAgent[kClients].size, 1) + t.strictEqual(mockPool[kConnected], 1) + t.strictEqual(mockAgent[kClients].size, 1) await mockAgent.close() - t.equal(mockPool[kConnected], 0) - t.equal(mockAgent[kClients].size, 0) + t.strictEqual(mockPool[kConnected], 0) + t.strictEqual(mockAgent[kClients].size, 0) }) test('MockAgent - .close should clean up registered clients', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const baseUrl = 'http://localhost:9999' @@ -261,15 +256,15 @@ test('MockAgent - .close should clean up registered clients', async (t) => { const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) - t.equal(mockClient[kConnected], 1) - t.equal(mockAgent[kClients].size, 1) + t.strictEqual(mockClient[kConnected], 1) + t.strictEqual(mockAgent[kClients].size, 1) await mockAgent.close() - t.equal(mockClient[kConnected], 0) - t.equal(mockAgent[kClients].size, 0) + t.strictEqual(mockClient[kConnected], 0) + t.strictEqual(mockAgent[kClients].size, 0) }) test('MockAgent - [kClients] should match encapsulated agent', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -277,14 +272,14 @@ test('MockAgent - [kClients] should match encapsulated agent', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const agent = new Agent() - t.teardown(agent.close.bind(agent)) + after(() => agent.close()) const mockAgent = new MockAgent({ agent }) @@ -295,11 +290,11 @@ test('MockAgent - [kClients] should match encapsulated agent', async (t) => { }).reply(200, 'hello') // The MockAgent should encapsulate the input agent clients - t.equal(mockAgent[kClients].size, agent[kClients].size) + t.strictEqual(mockAgent[kClients].size, agent[kClients].size) }) test('MockAgent - basic intercept with MockAgent.request', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -307,14 +302,14 @@ test('MockAgent - basic intercept with MockAgent.request', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -332,18 +327,18 @@ test('MockAgent - basic intercept with MockAgent.request', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - basic intercept with request', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -351,7 +346,7 @@ test('MockAgent - basic intercept with request', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -359,7 +354,7 @@ test('MockAgent - basic intercept with request', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -375,18 +370,18 @@ test('MockAgent - basic intercept with request', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - should support local agents', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -394,7 +389,7 @@ test('MockAgent - should support local agents', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -402,7 +397,7 @@ test('MockAgent - should support local agents', async (t) => { const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -421,18 +416,18 @@ test('MockAgent - should support local agents', async (t) => { body: 'form1=data1&form2=data2', dispatcher: mockAgent }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - should support specifying custom agents to mock', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -440,14 +435,14 @@ test('MockAgent - should support specifying custom agents to mock', async (t) => t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const agent = new Agent() - t.teardown(agent.close.bind(agent)) + after(() => agent.close()) const mockAgent = new MockAgent({ agent }) setGlobalDispatcher(mockAgent) @@ -468,18 +463,18 @@ test('MockAgent - should support specifying custom agents to mock', async (t) => method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - basic Client intercept with request', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -487,7 +482,7 @@ test('MockAgent - basic Client intercept with request', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -495,7 +490,7 @@ test('MockAgent - basic Client intercept with request', async (t) => { const mockAgent = new MockAgent({ connections: 1 }) setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) mockClient.intercept({ @@ -513,18 +508,18 @@ test('MockAgent - basic Client intercept with request', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - basic intercept with multiple pools', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -532,7 +527,7 @@ test('MockAgent - basic intercept with multiple pools', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -540,7 +535,7 @@ test('MockAgent - basic intercept with multiple pools', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool1 = mockAgent.get(baseUrl) const mockPool2 = mockAgent.get('http://localhost:9999') @@ -565,18 +560,18 @@ test('MockAgent - basic intercept with multiple pools', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar-1' }) }) test('MockAgent - should handle multiple responses for an interceptor', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -584,7 +579,7 @@ test('MockAgent - should handle multiple responses for an interceptor', async (t t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -592,7 +587,7 @@ test('MockAgent - should handle multiple responses for an interceptor', async (t const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -615,11 +610,11 @@ test('MockAgent - should handle multiple responses for an interceptor', async (t const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) } @@ -628,26 +623,26 @@ test('MockAgent - should handle multiple responses for an interceptor', async (t const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { hello: 'there' }) } }) test('MockAgent - should call original Pool dispatch if request not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -655,28 +650,28 @@ test('MockAgent - should call original Pool dispatch if request not found', asyn const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - should call original Client dispatch if request not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -684,20 +679,20 @@ test('MockAgent - should call original Client dispatch if request not found', as const mockAgent = new MockAgent({ connections: 1 }) setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - should handle string responses', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -705,7 +700,7 @@ test('MockAgent - should handle string responses', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -713,7 +708,7 @@ test('MockAgent - should handle string responses', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -724,22 +719,20 @@ test('MockAgent - should handle string responses', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - should handle basic concurrency for requests', { jobs: 5 }, async (t) => { - t.plan(5) - const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) await Promise.all([...Array(5).keys()].map(idx => - t.test(`concurrent job (${idx})`, async (innerTest) => { - innerTest.plan(2) + test(`concurrent job (${idx})`, async (t) => { + t = tspl(t, { plan: 2 }) const baseUrl = 'http://localhost:9999' @@ -752,10 +745,10 @@ test('MockAgent - should handle basic concurrency for requests', { jobs: 5 }, as const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - innerTest.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const jsonResponse = JSON.parse(await getResponse(body)) - innerTest.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: `bar ${idx}` }) }) @@ -763,7 +756,7 @@ test('MockAgent - should handle basic concurrency for requests', { jobs: 5 }, as }) test('MockAgent - handle delays to simulate work', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -771,7 +764,7 @@ test('MockAgent - handle delays to simulate work', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -779,7 +772,7 @@ test('MockAgent - handle delays to simulate work', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -792,16 +785,16 @@ test('MockAgent - handle delays to simulate work', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') const elapsedInMs = process.hrtime(start)[1] / 1e6 t.ok(elapsedInMs >= 50, `Elapsed time is not greater than 50ms: ${elapsedInMs}`) }) test('MockAgent - should persist requests', async (t) => { - t.plan(8) + t = tspl(t, { plan: 8 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -809,7 +802,7 @@ test('MockAgent - should persist requests', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -817,7 +810,7 @@ test('MockAgent - should persist requests', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -836,12 +829,12 @@ test('MockAgent - should persist requests', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) } @@ -851,19 +844,19 @@ test('MockAgent - should persist requests', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) } }) test('MockAgent - handle persists with delayed requests', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -871,7 +864,7 @@ test('MockAgent - handle persists with delayed requests', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -879,7 +872,7 @@ test('MockAgent - handle persists with delayed requests', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -891,25 +884,25 @@ test('MockAgent - handle persists with delayed requests', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') } { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'POST' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') } }) test('MockAgent - calling close on a mock pool should not affect other mock pools', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -917,7 +910,7 @@ test('MockAgent - calling close on a mock pool should not affect other mock pool t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -925,7 +918,7 @@ test('MockAgent - calling close on a mock pool should not affect other mock pool const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPoolToClose = mockAgent.get('http://localhost:9999') mockPoolToClose.intercept({ @@ -949,25 +942,25 @@ test('MockAgent - calling close on a mock pool should not affect other mock pool const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, body } = await request(`${baseUrl}/bar`, { method: 'POST' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'bar') + t.strictEqual(response, 'bar') } }) test('MockAgent - close removes all registered mock clients', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -975,7 +968,7 @@ test('MockAgent - close removes all registered mock clients', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -991,7 +984,7 @@ test('MockAgent - close removes all registered mock clients', async (t) => { }).reply(200, 'foo') await mockAgent.close() - t.equal(mockAgent[kClients].size, 0) + t.strictEqual(mockAgent[kClients].size, 0) try { await request(`${baseUrl}/foo`, { method: 'GET' }) @@ -1001,7 +994,7 @@ test('MockAgent - close removes all registered mock clients', async (t) => { }) test('MockAgent - close removes all registered mock pools', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1009,7 +1002,7 @@ test('MockAgent - close removes all registered mock pools', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1025,7 +1018,7 @@ test('MockAgent - close removes all registered mock pools', async (t) => { }).reply(200, 'foo') await mockAgent.close() - t.equal(mockAgent[kClients].size, 0) + t.strictEqual(mockAgent[kClients].size, 0) try { await request(`${baseUrl}/foo`, { method: 'GET' }) @@ -1035,7 +1028,7 @@ test('MockAgent - close removes all registered mock pools', async (t) => { }) test('MockAgent - should handle replyWithError', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1043,7 +1036,7 @@ test('MockAgent - should handle replyWithError', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1051,7 +1044,7 @@ test('MockAgent - should handle replyWithError', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1063,15 +1056,15 @@ test('MockAgent - should handle replyWithError', async (t) => { }) test('MockAgent - should support setting a reply to respond a set amount of times', async (t) => { - t.plan(9) + t = tspl(t, { plan: 9 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1079,7 +1072,7 @@ test('MockAgent - should support setting a reply to respond a set amount of time const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1089,32 +1082,32 @@ test('MockAgent - should support setting a reply to respond a set amount of time { const { statusCode, body } = await request(`${baseUrl}/foo`) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, body } = await request(`${baseUrl}/foo`) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, headers, body } = await request(`${baseUrl}/foo`) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') } }) test('MockAgent - persist overrides times', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1122,7 +1115,7 @@ test('MockAgent - persist overrides times', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1130,7 +1123,7 @@ test('MockAgent - persist overrides times', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1142,42 +1135,42 @@ test('MockAgent - persist overrides times', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } }) test('MockAgent - matcher should not find mock dispatch if path is of unsupported type', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1185,7 +1178,7 @@ test('MockAgent - matcher should not find mock dispatch if path is of unsupporte const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1196,14 +1189,14 @@ test('MockAgent - matcher should not find mock dispatch if path is of unsupporte const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - should match path with regex', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1211,7 +1204,7 @@ test('MockAgent - should match path with regex', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1219,7 +1212,7 @@ test('MockAgent - should match path with regex', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1231,25 +1224,25 @@ test('MockAgent - should match path with regex', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } { const { statusCode, body } = await request(`${baseUrl}/hello/foobar`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } }) test('MockAgent - should match path with function', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1257,7 +1250,7 @@ test('MockAgent - should match path with function', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1265,7 +1258,7 @@ test('MockAgent - should match path with function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1276,14 +1269,14 @@ test('MockAgent - should match path with function', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match method with regex', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1291,7 +1284,7 @@ test('MockAgent - should match method with regex', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1299,7 +1292,7 @@ test('MockAgent - should match method with regex', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1310,14 +1303,14 @@ test('MockAgent - should match method with regex', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match method with function', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1325,7 +1318,7 @@ test('MockAgent - should match method with function', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1333,7 +1326,7 @@ test('MockAgent - should match method with function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1344,14 +1337,14 @@ test('MockAgent - should match method with function', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match body with regex', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1359,7 +1352,7 @@ test('MockAgent - should match body with regex', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1367,7 +1360,7 @@ test('MockAgent - should match body with regex', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1380,14 +1373,14 @@ test('MockAgent - should match body with regex', async (t) => { method: 'GET', body: 'hello=there' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match body with function', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1395,7 +1388,7 @@ test('MockAgent - should match body with function', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1403,7 +1396,7 @@ test('MockAgent - should match body with function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1416,21 +1409,21 @@ test('MockAgent - should match body with function', async (t) => { method: 'GET', body: 'hello=there' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match headers with string', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { res.end('should not be called') t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1438,7 +1431,7 @@ test('MockAgent - should match headers with string', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1489,21 +1482,21 @@ test('MockAgent - should match headers with string', async (t) => { Host: 'example.com' } }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match headers with regex', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { res.end('should not be called') t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1511,7 +1504,7 @@ test('MockAgent - should match headers with regex', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1562,21 +1555,21 @@ test('MockAgent - should match headers with regex', async (t) => { Host: 'example.com' } }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match headers with function', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { res.end('should not be called') t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1584,7 +1577,7 @@ test('MockAgent - should match headers with function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1635,14 +1628,14 @@ test('MockAgent - should match headers with function', async (t) => { Host: 'example.com' } }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match url with regex', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1650,7 +1643,7 @@ test('MockAgent - should match url with regex', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1658,7 +1651,7 @@ test('MockAgent - should match url with regex', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(new RegExp(baseUrl)) mockPool.intercept({ @@ -1669,14 +1662,14 @@ test('MockAgent - should match url with regex', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - should match url with function', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1684,7 +1677,7 @@ test('MockAgent - should match url with function', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1692,7 +1685,7 @@ test('MockAgent - should match url with function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get((value) => baseUrl === value) mockPool.intercept({ @@ -1703,14 +1696,14 @@ test('MockAgent - should match url with function', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - handle default reply headers', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1718,7 +1711,7 @@ test('MockAgent - handle default reply headers', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1726,7 +1719,7 @@ test('MockAgent - handle default reply headers', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1737,18 +1730,18 @@ test('MockAgent - handle default reply headers', async (t) => { const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.same(headers, { + t.strictEqual(statusCode, 200) + t.deepStrictEqual(headers, { foo: 'bar', hello: 'there' }) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - handle default reply trailers', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1756,7 +1749,7 @@ test('MockAgent - handle default reply trailers', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1764,7 +1757,7 @@ test('MockAgent - handle default reply trailers', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1775,18 +1768,18 @@ test('MockAgent - handle default reply trailers', async (t) => { const { statusCode, trailers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.same(trailers, { + t.strictEqual(statusCode, 200) + t.deepStrictEqual(trailers, { foo: 'bar', hello: 'there' }) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - return calculated content-length if specified', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1794,7 +1787,7 @@ test('MockAgent - return calculated content-length if specified', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1802,7 +1795,7 @@ test('MockAgent - return calculated content-length if specified', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1813,18 +1806,18 @@ test('MockAgent - return calculated content-length if specified', async (t) => { const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.same(headers, { + t.strictEqual(statusCode, 200) + t.deepStrictEqual(headers, { hello: 'there', - 'content-length': 3 + 'content-length': '3' }) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') }) test('MockAgent - return calculated content-length for object response if specified', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -1832,7 +1825,7 @@ test('MockAgent - return calculated content-length for object response if specif t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1840,7 +1833,7 @@ test('MockAgent - return calculated content-length for object response if specif const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1851,26 +1844,26 @@ test('MockAgent - return calculated content-length for object response if specif const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.same(headers, { + t.strictEqual(statusCode, 200) + t.deepStrictEqual(headers, { hello: 'there', - 'content-length': 13 + 'content-length': '13' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { foo: 'bar' }) + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) test('MockAgent - should activate and deactivate mock clients', async (t) => { - t.plan(9) + t = tspl(t, { plan: 9 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1878,7 +1871,7 @@ test('MockAgent - should activate and deactivate mock clients', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1890,10 +1883,10 @@ test('MockAgent - should activate and deactivate mock clients', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } mockAgent.deactivate() @@ -1902,11 +1895,11 @@ test('MockAgent - should activate and deactivate mock clients', async (t) => { const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') } mockAgent.activate() @@ -1915,23 +1908,23 @@ test('MockAgent - should activate and deactivate mock clients', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.equal(response, 'foo') + t.strictEqual(response, 'foo') } }) test('MockAgent - enableNetConnect should allow all original dispatches to be called if dispatch not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1939,7 +1932,7 @@ test('MockAgent - enableNetConnect should allow all original dispatches to be ca const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1952,23 +1945,23 @@ test('MockAgent - enableNetConnect should allow all original dispatches to be ca const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - enableNetConnect with a host string should allow all original dispatches to be called if mockDispatch not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -1976,7 +1969,7 @@ test('MockAgent - enableNetConnect with a host string should allow all original const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -1989,23 +1982,23 @@ test('MockAgent - enableNetConnect with a host string should allow all original const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - enableNetConnect when called with host string multiple times should allow all original dispatches to be called if mockDispatch not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2013,7 +2006,7 @@ test('MockAgent - enableNetConnect when called with host string multiple times s const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2027,23 +2020,23 @@ test('MockAgent - enableNetConnect when called with host string multiple times s const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - enableNetConnect with a host regex should allow all original dispatches to be called if mockDispatch not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2051,7 +2044,7 @@ test('MockAgent - enableNetConnect with a host regex should allow all original d const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2064,23 +2057,23 @@ test('MockAgent - enableNetConnect with a host regex should allow all original d const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - enableNetConnect with a function should allow all original dispatches to be called if mockDispatch not found', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2088,7 +2081,7 @@ test('MockAgent - enableNetConnect with a function should allow all original dis const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2101,19 +2094,19 @@ test('MockAgent - enableNetConnect with a function should allow all original dis const { statusCode, headers, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const response = await getResponse(body) - t.equal(response, 'hello') + t.strictEqual(response, 'hello') }) test('MockAgent - enableNetConnect with an unknown input should throw', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:9999') mockPool.intercept({ @@ -2125,14 +2118,14 @@ test('MockAgent - enableNetConnect with an unknown input should throw', async (t }) test('MockAgent - enableNetConnect should throw if dispatch not matched for path and the origin was not allowed by net connect', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2140,7 +2133,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for path const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2156,14 +2149,14 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for path }) test('MockAgent - enableNetConnect should throw if dispatch not matched for method and the origin was not allowed by net connect', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2171,7 +2164,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for meth const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2187,14 +2180,14 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for meth }) test('MockAgent - enableNetConnect should throw if dispatch not matched for body and the origin was not allowed by net connect', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2202,7 +2195,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for body const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2220,14 +2213,14 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for body }) test('MockAgent - enableNetConnect should throw if dispatch not matched for headers and the origin was not allowed by net connect', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2235,7 +2228,7 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for head const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2257,15 +2250,15 @@ test('MockAgent - enableNetConnect should throw if dispatch not matched for head }) test('MockAgent - disableNetConnect should throw if dispatch not found by net connect', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { - t.equal(req.url, '/foo') - t.equal(req.method, 'GET') + t.strictEqual(req.url, '/foo') + t.strictEqual(req.method, 'GET') res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2273,7 +2266,7 @@ test('MockAgent - disableNetConnect should throw if dispatch not found by net co const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ @@ -2289,14 +2282,14 @@ test('MockAgent - disableNetConnect should throw if dispatch not found by net co }) test('MockAgent - headers function interceptor', async (t) => { - t.plan(7) + t = tspl(t, { plan: 7 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2304,7 +2297,7 @@ test('MockAgent - headers function interceptor', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) // Disable net connect so we can make sure it matches properly @@ -2314,7 +2307,7 @@ test('MockAgent - headers function interceptor', async (t) => { path: '/foo', method: 'GET', headers (headers) { - t.equal(typeof headers, 'object') + t.strictEqual(typeof headers, 'object') return !Object.keys(headers).includes('authorization') } }).reply(200, 'foo').times(2) @@ -2333,27 +2326,27 @@ test('MockAgent - headers function interceptor', async (t) => { foo: 'bar' } }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) } { const { statusCode } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) } }) test('MockAgent - clients are not garbage collected', async (t) => { const samples = 250 - t.plan(2) + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { t.fail('should not be called') t.end() res.end('should not be called') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2394,16 +2387,18 @@ test('MockAgent - clients are not garbage collected', async (t) => { results.add(statusCode) } - t.equal(results.size, 1) + t.strictEqual(results.size, 1) t.ok(results.has(200)) }) // https://github.com/nodejs/undici/issues/1321 test('MockAgent - using fetch yields correct statusText', async (t) => { + t = tspl(t, { plan: 4 }) + const mockAgent = new MockAgent() mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:3000') @@ -2414,8 +2409,8 @@ test('MockAgent - using fetch yields correct statusText', async (t) => { const { status, statusText } = await fetch('http://localhost:3000/statusText') - t.equal(status, 200) - t.equal(statusText, 'OK') + t.strictEqual(status, 200) + t.strictEqual(statusText, 'OK') mockPool.intercept({ path: '/unknownStatusText', @@ -2423,17 +2418,19 @@ test('MockAgent - using fetch yields correct statusText', async (t) => { }).reply(420, 'Everyday') const unknownStatusCodeRes = await fetch('http://localhost:3000/unknownStatusText') - t.equal(unknownStatusCodeRes.status, 420) - t.equal(unknownStatusCodeRes.statusText, 'unknown') + t.strictEqual(unknownStatusCodeRes.status, 420) + t.strictEqual(unknownStatusCodeRes.statusText, 'unknown') t.end() }) // https://github.com/nodejs/undici/issues/1556 test('MockAgent - using fetch yields a headers object in the reply callback', async (t) => { + t = tspl(t, { plan: 1 }) + const mockAgent = new MockAgent() mockAgent.disableNetConnect() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:3000') @@ -2441,7 +2438,7 @@ test('MockAgent - using fetch yields a headers object in the reply callback', as path: '/headers', method: 'GET' }).reply(200, (opts) => { - t.same(opts.headers, { + t.deepStrictEqual(opts.headers, { accept: '*/*', 'accept-language': '*', 'sec-fetch-mode': 'cors', @@ -2456,15 +2453,17 @@ test('MockAgent - using fetch yields a headers object in the reply callback', as dispatcher: mockAgent }) - t.end() + await t.completed }) // https://github.com/nodejs/undici/issues/1579 test('MockAgent - headers in mock dispatcher intercept should be case-insensitive', async (t) => { + t = tspl(t, { plan: 1 }) + const mockAgent = new MockAgent() mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('https://example.com') @@ -2485,11 +2484,15 @@ test('MockAgent - headers in mock dispatcher intercept should be case-insensitiv } }) - t.end() + t.ok(true, 'end') + + await t.completed }) // https://github.com/nodejs/undici/issues/1757 test('MockAgent - reply callback can be asynchronous', async (t) => { + t = tspl(t, { plan: 2 }) + class MiniflareDispatcher extends Dispatcher { constructor (inner, options) { super(options) @@ -2514,7 +2517,7 @@ test('MockAgent - reply callback can be asynchronous', async (t) => { mockAgent.disableNetConnect() setGlobalDispatcher(new MiniflareDispatcher(mockAgent)) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) mockClient.intercept({ path: () => true, @@ -2538,7 +2541,7 @@ test('MockAgent - reply callback can be asynchronous', async (t) => { body: JSON.stringify({ foo: 'bar' }) }) - t.same(await response.json(), { foo: 'bar' }) + t.deepStrictEqual(await response.json(), { foo: 'bar' }) } { @@ -2557,11 +2560,13 @@ test('MockAgent - reply callback can be asynchronous', async (t) => { duplex: 'half' }) - t.same(await response.json(), { foo: 'bar' }) + t.deepStrictEqual(await response.json(), { foo: 'bar' }) } }) test('MockAgent - headers should be array of strings', async (t) => { + t = tspl(t, { plan: 1 }) + const mockAgent = new MockAgent() mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) @@ -2585,7 +2590,7 @@ test('MockAgent - headers should be array of strings', async (t) => { method: 'GET' }) - t.same(headers['set-cookie'], [ + t.deepStrictEqual(headers['set-cookie'], [ 'foo=bar', 'bar=baz', 'baz=qux' @@ -2594,7 +2599,7 @@ test('MockAgent - headers should be array of strings', async (t) => { // https://github.com/nodejs/undici/issues/2418 test('MockAgent - Sending ReadableStream body', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) @@ -2604,8 +2609,8 @@ test('MockAgent - Sending ReadableStream body', async (t) => { req.pipe(res) }) - t.teardown(mockAgent.close.bind(mockAgent)) - t.teardown(server.close.bind(server)) + after(() => mockAgent.close()) + after(() => server.close()) await promisify(server.listen.bind(server))(0) @@ -2622,18 +2627,18 @@ test('MockAgent - Sending ReadableStream body', async (t) => { duplex: 'half' }) - t.same(await response.text(), 'test') + t.deepStrictEqual(await response.text(), 'test') }) // https://github.com/nodejs/undici/issues/2616 test('MockAgent - headers should be array of strings (fetch)', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:3000') @@ -2652,5 +2657,5 @@ test('MockAgent - headers should be array of strings (fetch)', async (t) => { method: 'GET' }) - t.same(response.headers.getSetCookie(), ['foo=bar', 'bar=baz', 'baz=qux']) + t.deepStrictEqual(response.headers.getSetCookie(), ['foo=bar', 'bar=baz', 'baz=qux']) }) diff --git a/test/mock-client.js b/test/mock-client.js index 9622977cf1e..ef6721b42be 100644 --- a/test/mock-client.js +++ b/test/mock-client.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after, describe } = require('node:test') const { createServer } = require('node:http') const { promisify } = require('node:util') const { MockAgent, MockClient, setGlobalDispatcher, request } = require('..') @@ -11,37 +12,33 @@ const { MockInterceptor } = require('../lib/mock/mock-interceptor') const { getResponse } = require('../lib/mock/mock-utils') const Dispatcher = require('../lib/dispatcher') -test('MockClient - constructor', t => { - t.plan(3) - - t.test('fails if opts.agent does not implement `get` method', t => { - t.plan(1) +describe('MockClient - constructor', () => { + test('fails if opts.agent does not implement `get` method', t => { + t = tspl(t, { plan: 1 }) t.throws(() => new MockClient('http://localhost:9999', { agent: { get: 'not a function' } }), InvalidArgumentError) }) - t.test('sets agent', t => { - t.plan(1) + test('sets agent', t => { + t = tspl(t, { plan: 1 }) t.doesNotThrow(() => new MockClient('http://localhost:9999', { agent: new MockAgent({ connections: 1 }) })) }) - t.test('should implement the Dispatcher API', t => { - t.plan(1) + test('should implement the Dispatcher API', t => { + t = tspl(t, { plan: 1 }) const mockClient = new MockClient('http://localhost:9999', { agent: new MockAgent({ connections: 1 }) }) t.ok(mockClient instanceof Dispatcher) }) }) -test('MockClient - dispatch', t => { - t.plan(2) - - t.test('should handle a single interceptor', (t) => { - t.plan(1) +describe('MockClient - dispatch', () => { + test('should handle a single interceptor', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) @@ -70,13 +67,13 @@ test('MockClient - dispatch', t => { })) }) - t.test('should directly throw error from mockDispatch function if error is not a MockNotMatchedError', (t) => { - t.plan(1) + test('should directly throw error from mockDispatch function if error is not a MockNotMatchedError', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) @@ -107,12 +104,12 @@ test('MockClient - dispatch', t => { }) test('MockClient - intercept should return a MockInterceptor', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) @@ -124,62 +121,60 @@ test('MockClient - intercept should return a MockInterceptor', (t) => { t.ok(interceptor instanceof MockInterceptor) }) -test('MockClient - intercept validation', (t) => { - t.plan(4) - - t.test('it should error if no options specified in the intercept', t => { - t.plan(1) +describe('MockClient - intercept validation', () => { + test('it should error if no options specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get('http://localhost:9999') t.throws(() => mockClient.intercept(), new InvalidArgumentError('opts must be an object')) }) - t.test('it should error if no path specified in the intercept', t => { - t.plan(1) + test('it should error if no path specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get('http://localhost:9999') t.throws(() => mockClient.intercept({}), new InvalidArgumentError('opts.path must be defined')) }) - t.test('it should default to GET if no method specified in the intercept', t => { - t.plan(1) + test('it should default to GET if no method specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get('http://localhost:9999') t.doesNotThrow(() => mockClient.intercept({ path: '/foo' })) }) - t.test('it should uppercase the method - https://github.com/nodejs/undici/issues/1320', t => { - t.plan(1) + test('it should uppercase the method - https://github.com/nodejs/undici/issues/1320', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() const mockClient = mockAgent.get('http://localhost:3000') - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) mockClient.intercept({ path: '/test', method: 'patch' }).reply(200, 'Hello!') - t.equal(mockClient[kDispatches][0].method, 'PATCH') + t.strictEqual(mockClient[kDispatches][0].method, 'PATCH') }) }) test('MockClient - close should run without error', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) mockClient[kDispatches] = [ @@ -196,11 +191,12 @@ test('MockClient - close should run without error', async (t) => { } ] - await t.resolves(mockClient.close()) + await mockClient.close() + t.ok(true, 'pass') }) test('MockClient - should be able to set as globalDispatcher', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -208,14 +204,14 @@ test('MockClient - should be able to set as globalDispatcher', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -229,14 +225,14 @@ test('MockClient - should be able to set as globalDispatcher', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockClient - should support query params', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -244,14 +240,14 @@ test('MockClient - should support query params', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -270,14 +266,14 @@ test('MockClient - should support query params', async (t) => { method: 'GET', query }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockClient - should intercept query params with hardcoded path', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -285,14 +281,14 @@ test('MockClient - should intercept query params with hardcoded path', async (t) t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -310,14 +306,14 @@ test('MockClient - should intercept query params with hardcoded path', async (t) method: 'GET', query }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockClient - should intercept query params regardless of key ordering', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -325,14 +321,14 @@ test('MockClient - should intercept query params regardless of key ordering', as t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -358,14 +354,14 @@ test('MockClient - should intercept query params regardless of key ordering', as method: 'GET', query }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockClient - should be able to use as a local dispatcher', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -373,14 +369,14 @@ test('MockClient - should be able to use as a local dispatcher', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -394,14 +390,14 @@ test('MockClient - should be able to use as a local dispatcher', async (t) => { method: 'GET', dispatcher: mockClient }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockClient - basic intercept with MockClient.request', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -409,14 +405,14 @@ test('MockClient - basic intercept with MockClient.request', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent({ connections: 1 }) - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockClient = mockAgent.get(baseUrl) t.ok(mockClient instanceof MockClient) @@ -435,12 +431,12 @@ test('MockClient - basic intercept with MockClient.request', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) diff --git a/test/mock-errors.js b/test/mock-errors.js index 1087f0cf588..58ec2b52b9f 100644 --- a/test/mock-errors.js +++ b/test/mock-errors.js @@ -1,32 +1,27 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { describe, test } = require('node:test') const { mockErrors, errors } = require('..') -test('mockErrors', (t) => { - t.plan(1) +describe('MockNotMatchedError', () => { + test('should implement an UndiciError', t => { + t = tspl(t, { plan: 4 }) - t.test('MockNotMatchedError', t => { - t.plan(2) - - t.test('should implement an UndiciError', t => { - t.plan(4) - - const mockError = new mockErrors.MockNotMatchedError() - t.ok(mockError instanceof errors.UndiciError) - t.same(mockError.name, 'MockNotMatchedError') - t.same(mockError.code, 'UND_MOCK_ERR_MOCK_NOT_MATCHED') - t.same(mockError.message, 'The request does not match any registered mock dispatches') - }) + const mockError = new mockErrors.MockNotMatchedError() + t.ok(mockError instanceof errors.UndiciError) + t.deepStrictEqual(mockError.name, 'MockNotMatchedError') + t.deepStrictEqual(mockError.code, 'UND_MOCK_ERR_MOCK_NOT_MATCHED') + t.deepStrictEqual(mockError.message, 'The request does not match any registered mock dispatches') + }) - t.test('should set a custom message', t => { - t.plan(4) + test('should set a custom message', t => { + t = tspl(t, { plan: 4 }) - const mockError = new mockErrors.MockNotMatchedError('custom message') - t.ok(mockError instanceof errors.UndiciError) - t.same(mockError.name, 'MockNotMatchedError') - t.same(mockError.code, 'UND_MOCK_ERR_MOCK_NOT_MATCHED') - t.same(mockError.message, 'custom message') - }) + const mockError = new mockErrors.MockNotMatchedError('custom message') + t.ok(mockError instanceof errors.UndiciError) + t.deepStrictEqual(mockError.name, 'MockNotMatchedError') + t.deepStrictEqual(mockError.code, 'UND_MOCK_ERR_MOCK_NOT_MATCHED') + t.deepStrictEqual(mockError.message, 'custom message') }) }) diff --git a/test/mock-interceptor-unused-assertions.js b/test/mock-interceptor-unused-assertions.js index 2fe8b824f9f..e6f5360a3f9 100644 --- a/test/mock-interceptor-unused-assertions.js +++ b/test/mock-interceptor-unused-assertions.js @@ -1,6 +1,7 @@ 'use strict' -const { test, beforeEach, afterEach } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, beforeEach, afterEach } = require('node:test') const { MockAgent, setGlobalDispatcher } = require('..') const PendingInterceptorsFormatter = require('../lib/mock/pending-interceptors-formatter') const util = require('../lib/core/util') @@ -41,12 +42,14 @@ function mockAgentWithOneInterceptor () { } test('1 pending interceptor', t => { - t.plan(2) - - const err = t.throws(() => mockAgentWithOneInterceptor().assertNoPendingInterceptors({ pendingInterceptorsFormatter })) - - t.same(err.message, tableRowsAlignedToLeft - ? ` + t = tspl(t, { plan: 1 }) + + try { + mockAgentWithOneInterceptor().assertNoPendingInterceptors({ pendingInterceptorsFormatter }) + t.fail('Should have thrown') + } catch (err) { + t.deepStrictEqual(err.message, tableRowsAlignedToLeft + ? ` 1 interceptor is pending: ┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -55,7 +58,7 @@ test('1 pending interceptor', t => { │ 0 │ 'GET' │ 'https://example.com' │ '/' │ 200 │ '❌' │ 0 │ 1 │ └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim() - : ` + : ` 1 interceptor is pending: ┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -64,20 +67,22 @@ test('1 pending interceptor', t => { │ 0 │ 'GET' │ 'https://example.com' │ '/' │ 200 │ '❌' │ 0 │ 1 │ └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim()) + } }) test('2 pending interceptors', t => { - t.plan(2) + t = tspl(t, { plan: 1 }) const withTwoInterceptors = mockAgentWithOneInterceptor() withTwoInterceptors .get(origin) .intercept({ method: 'get', path: '/some/path' }) .reply(204, 'OK') - const err = t.throws(() => withTwoInterceptors.assertNoPendingInterceptors({ pendingInterceptorsFormatter })) - - t.same(err.message, tableRowsAlignedToLeft - ? ` + try { + withTwoInterceptors.assertNoPendingInterceptors({ pendingInterceptorsFormatter }) + } catch (err) { + t.deepStrictEqual(err.message, tableRowsAlignedToLeft + ? ` 2 interceptors are pending: ┌─────────┬────────┬──────────────────────────┬──────────────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -87,7 +92,7 @@ test('2 pending interceptors', t => { │ 1 │ 'GET' │ 'https://localhost:9999' │ '/some/path' │ 204 │ '❌' │ 0 │ 1 │ └─────────┴────────┴──────────────────────────┴──────────────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim() - : ` + : ` 2 interceptors are pending: ┌─────────┬────────┬──────────────────────────┬──────────────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -97,10 +102,11 @@ test('2 pending interceptors', t => { │ 1 │ 'GET' │ 'https://localhost:9999' │ '/some/path' │ 204 │ '❌' │ 0 │ 1 │ └─────────┴────────┴──────────────────────────┴──────────────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim()) + } }) test('Variations of persist(), times(), and pending status', async t => { - t.plan(7) + t = tspl(t, { plan: 6 }) // Agent with unused interceptor const agent = mockAgentWithOneInterceptor() @@ -118,20 +124,20 @@ test('Variations of persist(), times(), and pending status', async t => { .intercept({ method: 'GET', path: '/persistent/used' }) .reply(200, 'OK') .persist() - t.same((await agent.request({ origin, method: 'GET', path: '/persistent/used' })).statusCode, 200) + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/persistent/used' })).statusCode, 200) // Consumed without persist() agent.get(origin) .intercept({ method: 'post', path: '/transient/pending' }) .reply(201, 'Created') - t.same((await agent.request({ origin, method: 'POST', path: '/transient/pending' })).statusCode, 201) + t.deepStrictEqual((await agent.request({ origin, method: 'POST', path: '/transient/pending' })).statusCode, 201) // Partially pending with times() agent.get(origin) .intercept({ method: 'get', path: '/times/partial' }) .reply(200, 'OK') .times(5) - t.same((await agent.request({ origin, method: 'GET', path: '/times/partial' })).statusCode, 200) + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/times/partial' })).statusCode, 200) // Unused with times() agent.get(origin) @@ -144,13 +150,15 @@ test('Variations of persist(), times(), and pending status', async t => { .intercept({ method: 'get', path: '/times/pending' }) .reply(200, 'OK') .times(2) - t.same((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200) - t.same((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200) - - const err = t.throws(() => agent.assertNoPendingInterceptors({ pendingInterceptorsFormatter })) - - t.same(err.message, tableRowsAlignedToLeft - ? ` + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200) + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/times/pending' })).statusCode, 200) + + try { + agent.assertNoPendingInterceptors({ pendingInterceptorsFormatter }) + t.fail('Should have thrown') + } catch (err) { + t.deepStrictEqual(err.message, tableRowsAlignedToLeft + ? ` 4 interceptors are pending: ┌─────────┬────────┬──────────────────────────┬──────────────────────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -162,7 +170,7 @@ test('Variations of persist(), times(), and pending status', async t => { │ 3 │ 'GET' │ 'https://localhost:9999' │ '/times/unused' │ 200 │ '❌' │ 0 │ 2 │ └─────────┴────────┴──────────────────────────┴──────────────────────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim() - : ` + : ` 4 interceptors are pending: ┌─────────┬────────┬──────────────────────────┬──────────────────────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -174,45 +182,48 @@ test('Variations of persist(), times(), and pending status', async t => { │ 3 │ 'GET' │ 'https://localhost:9999' │ '/times/unused' │ 200 │ '❌' │ 0 │ 2 │ └─────────┴────────┴──────────────────────────┴──────────────────────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim()) + } }) test('works when no interceptors are registered', t => { - t.plan(2) + t = tspl(t, { plan: 2 }) const agent = new MockAgent() agent.disableNetConnect() - t.same(agent.pendingInterceptors(), []) + t.deepStrictEqual(agent.pendingInterceptors(), []) t.doesNotThrow(() => agent.assertNoPendingInterceptors()) }) test('works when all interceptors are pending', async t => { - t.plan(4) + t = tspl(t, { plan: 4 }) const agent = new MockAgent() agent.disableNetConnect() agent.get(origin).intercept({ method: 'get', path: '/' }).reply(200, 'OK') - t.same((await agent.request({ origin, method: 'GET', path: '/' })).statusCode, 200) + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/' })).statusCode, 200) agent.get(origin).intercept({ method: 'get', path: '/persistent' }).reply(200, 'OK') - t.same((await agent.request({ origin, method: 'GET', path: '/persistent' })).statusCode, 200) + t.deepStrictEqual((await agent.request({ origin, method: 'GET', path: '/persistent' })).statusCode, 200) - t.same(agent.pendingInterceptors(), []) + t.deepStrictEqual(agent.pendingInterceptors(), []) t.doesNotThrow(() => agent.assertNoPendingInterceptors()) }) test('defaults to rendering output with terminal color when process.env.CI is unset', t => { - t.plan(2) + t = tspl(t, { plan: 1 }) // This ensures that the test works in an environment where the CI env var is set. const oldCiEnvVar = process.env.CI delete process.env.CI - const err = t.throws( - () => mockAgentWithOneInterceptor().assertNoPendingInterceptors()) - t.same(err.message, tableRowsAlignedToLeft - ? ` + try { + mockAgentWithOneInterceptor().assertNoPendingInterceptors() + t.fail('Shoudl have thrown') + } catch (err) { + t.deepStrictEqual(err.message, tableRowsAlignedToLeft + ? ` 1 interceptor is pending: ┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -221,7 +232,7 @@ test('defaults to rendering output with terminal color when process.env.CI is un │ 0 │ \u001b[32m'GET'\u001b[39m │ \u001b[32m'https://example.com'\u001b[39m │ \u001b[32m'/'\u001b[39m │ \u001b[33m200\u001b[39m │ \u001b[32m'❌'\u001b[39m │ \u001b[33m0\u001b[39m │ \u001b[33m1\u001b[39m │ └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim() - : ` + : ` 1 interceptor is pending: ┌─────────┬────────┬───────────────────────┬──────┬─────────────┬────────────┬─────────────┬───────────┐ @@ -231,19 +242,20 @@ test('defaults to rendering output with terminal color when process.env.CI is un └─────────┴────────┴───────────────────────┴──────┴─────────────┴────────────┴─────────────┴───────────┘ `.trim()) - // Re-set the CI env var if it were set. - // Assigning `undefined` does not work, - // because reading the env var afterwards yields the string 'undefined', - // so we need to re-set it conditionally. - if (oldCiEnvVar != null) { - process.env.CI = oldCiEnvVar + // Re-set the CI env var if it were set. + // Assigning `undefined` does not work, + // because reading the env var afterwards yields the string 'undefined', + // so we need to re-set it conditionally. + if (oldCiEnvVar != null) { + process.env.CI = oldCiEnvVar + } } }) test('returns unused interceptors', t => { - t.plan(1) + t = tspl(t, { plan: 1 }) - t.same(mockAgentWithOneInterceptor().pendingInterceptors(), [ + t.deepStrictEqual(mockAgentWithOneInterceptor().pendingInterceptors(), [ { timesInvoked: 0, times: 1, diff --git a/test/mock-interceptor.js b/test/mock-interceptor.js index 787878395f4..036ea69df98 100644 --- a/test/mock-interceptor.js +++ b/test/mock-interceptor.js @@ -1,28 +1,26 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { describe, test, after } = require('node:test') const { MockInterceptor, MockScope } = require('../lib/mock/mock-interceptor') const MockAgent = require('../lib/mock/mock-agent') const { kDispatchKey } = require('../lib/mock/mock-symbols') const { InvalidArgumentError } = require('../lib/core/errors') -test('MockInterceptor - path', t => { - t.plan(1) - t.test('should remove hash fragment from paths', t => { - t.plan(1) +describe('MockInterceptor - path', () => { + test('should remove hash fragment from paths', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '#foobar', method: '' }, []) - t.equal(mockInterceptor[kDispatchKey].path, '') + t.strictEqual(mockInterceptor[kDispatchKey].path, '') }) }) -test('MockInterceptor - reply', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockInterceptor - reply', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' @@ -31,8 +29,8 @@ test('MockInterceptor - reply', t => { t.ok(result instanceof MockScope) }) - t.test('should error if passed options invalid', t => { - t.plan(2) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 2 }) const mockInterceptor = new MockInterceptor({ path: '', @@ -43,11 +41,9 @@ test('MockInterceptor - reply', t => { }) }) -test('MockInterceptor - reply callback', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockInterceptor - reply callback', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' @@ -56,23 +52,21 @@ test('MockInterceptor - reply callback', t => { t.ok(result instanceof MockScope) }) - t.test('should error if passed options invalid', t => { - t.plan(2) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 2 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' }, []) t.throws(() => mockInterceptor.reply(), new InvalidArgumentError('statusCode must be defined')) - t.throws(() => mockInterceptor.reply(200, () => {}, 'hello'), new InvalidArgumentError('responseOptions must be an object')) + t.throws(() => mockInterceptor.reply(200, () => { }, 'hello'), new InvalidArgumentError('responseOptions must be an object')) }) }) -test('MockInterceptor - reply options callback', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(2) +describe('MockInterceptor - reply options callback', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 2 }) const mockInterceptor = new MockInterceptor({ path: '', @@ -88,7 +82,7 @@ test('MockInterceptor - reply options callback', t => { const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -96,7 +90,7 @@ test('MockInterceptor - reply options callback', t => { path: '/test', method: 'GET' }).reply((options) => { - t.strictSame(options, { path: '/test', method: 'GET', headers: { foo: 'bar' } }) + t.deepStrictEqual(options, { path: '/test', method: 'GET', headers: { foo: 'bar' } }) return { statusCode: 200, data: 'hello' } }) @@ -105,25 +99,25 @@ test('MockInterceptor - reply options callback', t => { method: 'GET', headers: { foo: 'bar' } }, { - onHeaders: () => {}, - onData: () => {}, - onComplete: () => {} + onHeaders: () => { }, + onData: () => { }, + onComplete: () => { } }) }) - t.test('should error if passed options invalid', async (t) => { - t.plan(3) + test('should error if passed options invalid', async (t) => { + t = tspl(t, { plan: 3 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) mockPool.intercept({ path: '/test', method: 'GET' - }).reply(() => {}) + }).reply(() => { }) mockPool.intercept({ path: '/test3', @@ -146,36 +140,34 @@ test('MockInterceptor - reply options callback', t => { path: '/test', method: 'GET' }, { - onHeaders: () => {}, - onData: () => {}, - onComplete: () => {} + onHeaders: () => { }, + onData: () => { }, + onComplete: () => { } }), new InvalidArgumentError('reply options callback must return an object')) t.throws(() => mockPool.dispatch({ path: '/test3', method: 'GET' }, { - onHeaders: () => {}, - onData: () => {}, - onComplete: () => {} + onHeaders: () => { }, + onData: () => { }, + onComplete: () => { } }), new InvalidArgumentError('responseOptions must be an object')) t.throws(() => mockPool.dispatch({ path: '/test4', method: 'GET' }, { - onHeaders: () => {}, - onData: () => {}, - onComplete: () => {} + onHeaders: () => { }, + onData: () => { }, + onComplete: () => { } }), new InvalidArgumentError('statusCode must be defined')) }) }) -test('MockInterceptor - replyWithError', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockInterceptor - replyWithError', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' @@ -184,8 +176,8 @@ test('MockInterceptor - replyWithError', t => { t.ok(result instanceof MockScope) }) - t.test('should error if passed options invalid', t => { - t.plan(1) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', @@ -195,11 +187,9 @@ test('MockInterceptor - replyWithError', t => { }) }) -test('MockInterceptor - defaultReplyHeaders', t => { - t.plan(2) - - t.test('should return MockInterceptor', t => { - t.plan(1) +describe('MockInterceptor - defaultReplyHeaders', () => { + test('should return MockInterceptor', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' @@ -208,8 +198,8 @@ test('MockInterceptor - defaultReplyHeaders', t => { t.ok(result instanceof MockInterceptor) }) - t.test('should error if passed options invalid', t => { - t.plan(1) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', @@ -219,11 +209,9 @@ test('MockInterceptor - defaultReplyHeaders', t => { }) }) -test('MockInterceptor - defaultReplyTrailers', t => { - t.plan(2) - - t.test('should return MockInterceptor', t => { - t.plan(1) +describe('MockInterceptor - defaultReplyTrailers', () => { + test('should return MockInterceptor', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' @@ -232,8 +220,8 @@ test('MockInterceptor - defaultReplyTrailers', t => { t.ok(result instanceof MockInterceptor) }) - t.test('should error if passed options invalid', t => { - t.plan(1) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', @@ -243,11 +231,9 @@ test('MockInterceptor - defaultReplyTrailers', t => { }) }) -test('MockInterceptor - replyContentLength', t => { - t.plan(1) - - t.test('should return MockInterceptor', t => { - t.plan(1) +describe('MockInterceptor - replyContentLength', () => { + test('should return MockInterceptor', t => { + t = tspl(t, { plan: 1 }) const mockInterceptor = new MockInterceptor({ path: '', method: '' diff --git a/test/mock-pool.js b/test/mock-pool.js index ca812d90866..0b690fe6d48 100644 --- a/test/mock-pool.js +++ b/test/mock-pool.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after, describe } = require('node:test') const { createServer } = require('node:http') const { promisify } = require('node:util') const { MockAgent, MockPool, getGlobalDispatcher, setGlobalDispatcher, request } = require('..') @@ -12,37 +13,33 @@ const { getResponse } = require('../lib/mock/mock-utils') const Dispatcher = require('../lib/dispatcher') const { fetch } = require('..') -test('MockPool - constructor', t => { - t.plan(3) - - t.test('fails if opts.agent does not implement `get` method', t => { - t.plan(1) +describe('MockPool - constructor', () => { + test('fails if opts.agent does not implement `get` method', t => { + t = tspl(t, { plan: 1 }) t.throws(() => new MockPool('http://localhost:9999', { agent: { get: 'not a function' } }), InvalidArgumentError) }) - t.test('sets agent', t => { - t.plan(1) + test('sets agent', t => { + t = tspl(t, { plan: 1 }) t.doesNotThrow(() => new MockPool('http://localhost:9999', { agent: new MockAgent() })) }) - t.test('should implement the Dispatcher API', t => { - t.plan(1) + test('should implement the Dispatcher API', t => { + t = tspl(t, { plan: 1 }) const mockPool = new MockPool('http://localhost:9999', { agent: new MockAgent() }) t.ok(mockPool instanceof Dispatcher) }) }) -test('MockPool - dispatch', t => { - t.plan(2) - - t.test('should handle a single interceptor', (t) => { - t.plan(1) +describe('MockPool - dispatch', () => { + test('should handle a single interceptor', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -66,18 +63,18 @@ test('MockPool - dispatch', t => { method: 'GET' }, { onHeaders: (_statusCode, _headers, resume) => resume(), - onData: () => {}, - onComplete: () => {} + onData: () => { }, + onComplete: () => { } })) }) - t.test('should directly throw error from mockDispatch function if error is not a MockNotMatchedError', (t) => { - t.plan(1) + test('should directly throw error from mockDispatch function if error is not a MockNotMatchedError', (t) => { + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -101,19 +98,19 @@ test('MockPool - dispatch', t => { method: 'GET' }, { onHeaders: (_statusCode, _headers, resume) => { throw new Error('kaboom') }, - onData: () => {}, - onComplete: () => {} + onData: () => { }, + onComplete: () => { } }), new Error('kaboom')) }) }) test('MockPool - intercept should return a MockInterceptor', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -125,33 +122,31 @@ test('MockPool - intercept should return a MockInterceptor', (t) => { t.ok(interceptor instanceof MockInterceptor) }) -test('MockPool - intercept validation', (t) => { - t.plan(3) - - t.test('it should error if no options specified in the intercept', t => { - t.plan(1) +describe('MockPool - intercept validation', () => { + test('it should error if no options specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:9999') t.throws(() => mockPool.intercept(), new InvalidArgumentError('opts must be an object')) }) - t.test('it should error if no path specified in the intercept', t => { - t.plan(1) + test('it should error if no path specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:9999') t.throws(() => mockPool.intercept({}), new InvalidArgumentError('opts.path must be defined')) }) - t.test('it should default to GET if no method specified in the intercept', t => { - t.plan(1) + test('it should default to GET if no method specified in the intercept', t => { + t = tspl(t, { plan: 1 }) const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get('http://localhost:9999') t.doesNotThrow(() => mockPool.intercept({ path: '/foo' })) @@ -159,12 +154,12 @@ test('MockPool - intercept validation', (t) => { }) test('MockPool - close should run without error', async (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) @@ -182,11 +177,12 @@ test('MockPool - close should run without error', async (t) => { } ] - await t.resolves(mockPool.close()) + await mockPool.close() + t.ok(true, 'pass') }) test('MockPool - should be able to set as globalDispatcher', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -194,14 +190,14 @@ test('MockPool - should be able to set as globalDispatcher', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) t.ok(mockPool instanceof MockPool) @@ -215,14 +211,14 @@ test('MockPool - should be able to set as globalDispatcher', async (t) => { const { statusCode, body } = await request(`${baseUrl}/foo`, { method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockPool - should be able to use as a local dispatcher', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -230,14 +226,14 @@ test('MockPool - should be able to use as a local dispatcher', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) t.ok(mockPool instanceof MockPool) @@ -251,14 +247,14 @@ test('MockPool - should be able to use as a local dispatcher', async (t) => { method: 'GET', dispatcher: mockPool }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const response = await getResponse(body) - t.same(response, 'hello') + t.deepStrictEqual(response, 'hello') }) test('MockPool - basic intercept with MockPool.request', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { res.setHeader('content-type', 'text/plain') @@ -266,14 +262,14 @@ test('MockPool - basic intercept with MockPool.request', async (t) => { t.fail('should not be called') t.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) await promisify(server.listen.bind(server))(0) const baseUrl = `http://localhost:${server.address().port}` const mockAgent = new MockAgent() - t.teardown(mockAgent.close.bind(mockAgent)) + after(() => mockAgent.close()) const mockPool = mockAgent.get(baseUrl) t.ok(mockPool instanceof MockPool) @@ -292,25 +288,27 @@ test('MockPool - basic intercept with MockPool.request', async (t) => { method: 'POST', body: 'form1=data1&form2=data2' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'application/json') - t.same(trailers, { 'content-md5': 'test' }) + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'application/json') + t.deepStrictEqual(trailers, { 'content-md5': 'test' }) const jsonResponse = JSON.parse(await getResponse(body)) - t.same(jsonResponse, { + t.deepStrictEqual(jsonResponse, { foo: 'bar' }) }) // https://github.com/nodejs/undici/issues/1546 test('MockPool - correct errors when consuming invalid JSON body', async (t) => { + t = tspl(t, { plan: 1 }) + const oldDispatcher = getGlobalDispatcher() const mockAgent = new MockAgent() mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) - t.teardown(() => setGlobalDispatcher(oldDispatcher)) + after(() => setGlobalDispatcher(oldDispatcher)) const mockPool = mockAgent.get('https://google.com') mockPool.intercept({ @@ -324,6 +322,8 @@ test('MockPool - correct errors when consuming invalid JSON body', async (t) => }) test('MockPool - allows matching headers in fetch', async (t) => { + t = tspl(t, { plan: 2 }) + const oldDispatcher = getGlobalDispatcher() const baseUrl = 'http://localhost:9999' @@ -331,7 +331,7 @@ test('MockPool - allows matching headers in fetch', async (t) => { mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) - t.teardown(async () => { + after(async () => { await mockAgent.close() setGlobalDispatcher(oldDispatcher) }) @@ -345,23 +345,19 @@ test('MockPool - allows matching headers in fetch', async (t) => { } }).reply(200, { ok: 1 }).times(3) - await t.resolves( - fetch(`${baseUrl}/foo`, { - headers: { - accept: 'application/json' - } - }) - ) + await fetch(`${baseUrl}/foo`, { + headers: { + accept: 'application/json' + } + }) // no 'accept: application/json' header sent, not matched await t.rejects(fetch(`${baseUrl}/foo`)) // not 'accept: application/json', not matched - await t.rejects(fetch(`${baseUrl}/foo`), { + await t.rejects(fetch(`${baseUrl}/foo`, { headers: { accept: 'text/plain' } - }, TypeError) - - t.end() + }), new TypeError('fetch failed')) }) diff --git a/test/mock-scope.js b/test/mock-scope.js index d8f41d90198..0344b99d0bc 100644 --- a/test/mock-scope.js +++ b/test/mock-scope.js @@ -1,14 +1,13 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, describe } = require('node:test') const { MockScope } = require('../lib/mock/mock-interceptor') const { InvalidArgumentError } = require('../lib/core/errors') -test('MockScope - delay', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockScope - delay', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockScope = new MockScope({ path: '', method: '' @@ -17,8 +16,8 @@ test('MockScope - delay', t => { t.ok(result instanceof MockScope) }) - t.test('should error if passed options invalid', t => { - t.plan(4) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 4 }) const mockScope = new MockScope({ path: '', @@ -31,11 +30,9 @@ test('MockScope - delay', t => { }) }) -test('MockScope - persist', t => { - t.plan(1) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockScope - persist', () => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockScope = new MockScope({ path: '', method: '' @@ -45,11 +42,9 @@ test('MockScope - persist', t => { }) }) -test('MockScope - times', t => { - t.plan(2) - - t.test('should return MockScope', t => { - t.plan(1) +describe('MockScope - times', t => { + test('should return MockScope', t => { + t = tspl(t, { plan: 1 }) const mockScope = new MockScope({ path: '', method: '' @@ -58,8 +53,8 @@ test('MockScope - times', t => { t.ok(result instanceof MockScope) }) - t.test('should error if passed options invalid', t => { - t.plan(4) + test('should error if passed options invalid', t => { + t = tspl(t, { plan: 4 }) const mockScope = new MockScope({ path: '', diff --git a/test/mock-utils.js b/test/mock-utils.js index 4acc8558390..baf0933ba57 100644 --- a/test/mock-utils.js +++ b/test/mock-utils.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, describe } = require('node:test') const { MockNotMatchedError } = require('../lib/mock/mock-errors') const { deleteMockDispatch, @@ -11,7 +12,7 @@ const { } = require('../lib/mock/mock-utils') test('deleteMockDispatch - should do nothing if not able to find mock dispatch', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const key = { url: 'url', @@ -23,11 +24,9 @@ test('deleteMockDispatch - should do nothing if not able to find mock dispatch', t.doesNotThrow(() => deleteMockDispatch([], key)) }) -test('getMockDispatch', (t) => { - t.plan(3) - - t.test('it should find a mock dispatch', (t) => { - t.plan(1) +describe('getMockDispatch', () => { + test('it should find a mock dispatch', (t) => { + t = tspl(t, { plan: 1 }) const dispatches = [ { path: 'path', @@ -40,15 +39,15 @@ test('getMockDispatch', (t) => { path: 'path', method: 'method' }) - t.same(result, { + t.deepStrictEqual(result, { path: 'path', method: 'method', consumed: false }) }) - t.test('it should skip consumed dispatches', (t) => { - t.plan(1) + test('it should skip consumed dispatches', (t) => { + t = tspl(t, { plan: 1 }) const dispatches = [ { path: 'path', @@ -66,15 +65,15 @@ test('getMockDispatch', (t) => { path: 'path', method: 'method' }) - t.same(result, { + t.deepStrictEqual(result, { path: 'path', method: 'method', consumed: false }) }) - t.test('it should throw if dispatch not found', (t) => { - t.plan(1) + test('it should throw if dispatch not found', (t) => { + t = tspl(t, { plan: 1 }) const dispatches = [ { path: 'path', @@ -90,29 +89,29 @@ test('getMockDispatch', (t) => { }) }) -test('getResponseData', (t) => { - t.plan(3) - - t.test('it should stringify objects', (t) => { - t.plan(1) +describe('getResponseData', () => { + test('it should stringify objects', (t) => { + t = tspl(t, { plan: 1 }) const responseData = getResponseData({ str: 'string', num: 42 }) - t.equal(responseData, '{"str":"string","num":42}') + t.strictEqual(responseData, '{"str":"string","num":42}') }) - t.test('it should return strings untouched', (t) => { - t.plan(1) + test('it should return strings untouched', (t) => { + t = tspl(t, { plan: 1 }) const responseData = getResponseData('test') - t.equal(responseData, 'test') + t.strictEqual(responseData, 'test') }) - t.test('it should return buffers untouched', (t) => { - t.plan(1) + test('it should return buffers untouched', (t) => { + t = tspl(t, { plan: 1 }) const responseData = getResponseData(Buffer.from('test')) t.ok(Buffer.isBuffer(responseData)) }) }) test('getStatusText', (t) => { + t = tspl(t, { plan: 64 }) + for (const statusCode of [ 100, 101, 102, 103, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, @@ -126,23 +125,25 @@ test('getStatusText', (t) => { t.ok(getStatusText(statusCode)) } - t.equal(getStatusText(420), 'unknown') + t.strictEqual(getStatusText(420), 'unknown') t.end() }) test('getHeaderByName', (t) => { + t = tspl(t, { plan: 6 }) + const headersRecord = { key: 'value' } - t.equal(getHeaderByName(headersRecord, 'key'), 'value') - t.equal(getHeaderByName(headersRecord, 'anotherKey'), undefined) + t.strictEqual(getHeaderByName(headersRecord, 'key'), 'value') + t.strictEqual(getHeaderByName(headersRecord, 'anotherKey'), undefined) const headersArray = ['key', 'value'] - t.equal(getHeaderByName(headersArray, 'key'), 'value') - t.equal(getHeaderByName(headersArray, 'anotherKey'), undefined) + t.strictEqual(getHeaderByName(headersArray, 'key'), 'value') + t.strictEqual(getHeaderByName(headersArray, 'anotherKey'), undefined) const { Headers } = require('../index') @@ -150,8 +151,8 @@ test('getHeaderByName', (t) => { ['key', 'value'] ]) - t.equal(getHeaderByName(headers, 'key'), 'value') - t.equal(getHeaderByName(headers, 'anotherKey'), null) + t.strictEqual(getHeaderByName(headers, 'key'), 'value') + t.strictEqual(getHeaderByName(headers, 'anotherKey'), null) t.end() })