Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions test/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { test, after, describe } = require('node:test')
const { createServer } = require('node:http')
const { promisify } = require('node:util')
const { MockAgent, MockClient, setGlobalDispatcher, request } = require('..')
const { MockAgent, MockClient, getGlobalDispatcher, setGlobalDispatcher, request } = require('..')
const { kUrl } = require('../lib/core/symbols')
const { kDispatches } = require('../lib/mock/mock-symbols')
const { InvalidArgumentError } = require('../lib/core/errors')
Expand Down Expand Up @@ -220,7 +220,10 @@ test('MockClient - should be able to set as globalDispatcher', async (t) => {

const mockClient = mockAgent.get(baseUrl)
t.assert.ok(mockClient instanceof MockClient)

const originalDispatcher = getGlobalDispatcher()
setGlobalDispatcher(mockClient)
t.after(() => setGlobalDispatcher(originalDispatcher))

mockClient.intercept({
path: '/foo',
Expand Down Expand Up @@ -258,7 +261,6 @@ test('MockClient - should support query params', async (t) => {

const mockClient = mockAgent.get(baseUrl)
t.assert.ok(mockClient instanceof MockClient)
setGlobalDispatcher(mockClient)

const query = {
pageNum: 1
Expand All @@ -271,6 +273,7 @@ test('MockClient - should support query params', async (t) => {

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET',
dispatcher: mockClient,
query
})
t.assert.strictEqual(statusCode, 200)
Expand Down Expand Up @@ -301,7 +304,6 @@ test('MockClient - should intercept query params with hardcoded path', async (t)

const mockClient = mockAgent.get(baseUrl)
t.assert.ok(mockClient instanceof MockClient)
setGlobalDispatcher(mockClient)

const query = {
pageNum: 1
Expand All @@ -313,6 +315,7 @@ test('MockClient - should intercept query params with hardcoded path', async (t)

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET',
dispatcher: mockClient,
query
})
t.assert.strictEqual(statusCode, 200)
Expand Down Expand Up @@ -343,7 +346,6 @@ test('MockClient - should intercept query params regardless of key ordering', as

const mockClient = mockAgent.get(baseUrl)
t.assert.ok(mockClient instanceof MockClient)
setGlobalDispatcher(mockClient)

const query = {
pageNum: 1,
Expand All @@ -363,6 +365,7 @@ test('MockClient - should intercept query params regardless of key ordering', as

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET',
dispatcher: mockClient,
query
})
t.assert.strictEqual(statusCode, 200)
Expand Down Expand Up @@ -477,7 +480,6 @@ test('MockClient - cleans mocks', async (t) => {

const mockClient = mockAgent.get(baseUrl)
t.assert.ok(mockClient instanceof MockClient)
setGlobalDispatcher(mockClient)

mockClient.intercept({
path: '/foo',
Expand All @@ -491,7 +493,8 @@ test('MockClient - cleans mocks', async (t) => {
t.assert.strictEqual(mockClient[kDispatches].length, 0)

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET'
method: 'GET',
dispatcher: mockClient
})
t.assert.strictEqual(statusCode, 200)

Expand Down
Loading