Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate a batch of tests to node test runner no. 4 #2739

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
775 changes: 390 additions & 385 deletions test/mock-agent.js

Large diffs are not rendered by default.

140 changes: 68 additions & 72 deletions test/mock-client.js
Original file line number Diff line number Diff line change
@@ -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('..')
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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] = [
Expand All @@ -196,26 +191,27 @@ 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')
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)

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)
Expand All @@ -229,29 +225,29 @@ 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')
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)

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)
Expand All @@ -270,29 +266,29 @@ 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')
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)

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)
Expand All @@ -310,29 +306,29 @@ 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')
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)

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)
Expand All @@ -358,29 +354,29 @@ 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')
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)

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)
Expand All @@ -394,29 +390,29 @@ 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')
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)

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)

Expand All @@ -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'
})
})