Skip to content

Commit e5f81fa

Browse files
committed
Assert mocked URLs are called
Nock will not implicitly assert that the URLs that are mocked by it are being called. You must explicitly keep track of the scope object that it returns, and call `scope.done()` to assert that all the mocked endpoints have actually been called.
1 parent d3ba3e6 commit e5f81fa

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

src/__tests__/transmitter.test.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Transmitter } from "../transmitter"
2-
import nock from "nock"
2+
import nock, { Scope } from "nock"
33
import https from "https"
44
import fs from "fs"
55
import http, { ClientRequest } from "http"
@@ -33,8 +33,8 @@ describe("Transmitter", () => {
3333
return new Transmitter("https://example.com/foo", "request body")
3434
}
3535

36-
function mockSampleRequest(responseBody: object | string, query = {}) {
37-
nock("https://example.com")
36+
function mockSampleRequest(responseBody: object | string, query = {}): Scope {
37+
return nock("https://example.com")
3838
.post("/foo", "request body")
3939
.query({
4040
api_key: "",
@@ -59,17 +59,21 @@ describe("Transmitter", () => {
5959
it("resolves to a response on success", async () => {
6060
const transmitter = sampleTransmitter()
6161

62-
mockSampleRequest({ json: "response" })
62+
const scope = mockSampleRequest({ json: "response" })
6363

6464
await expectResponse(transmitter.transmit(), { json: "response" })
65+
66+
scope.done()
6567
})
6668

6769
it("resolves to an empty object if the response is not JSON", async () => {
6870
const transmitter = sampleTransmitter()
6971

70-
mockSampleRequest("not JSON")
72+
const scope = mockSampleRequest("not JSON")
7173

7274
await expectResponse(transmitter.transmit(), {})
75+
76+
scope.done()
7377
})
7478

7579
it("rejects on error", async () => {
@@ -91,7 +95,7 @@ describe("Transmitter", () => {
9195

9296
const transmitter = sampleTransmitter()
9397

94-
mockSampleRequest(
98+
const scope = mockSampleRequest(
9599
{},
96100
{
97101
api_key: "some_api_key",
@@ -102,14 +106,16 @@ describe("Transmitter", () => {
102106
)
103107

104108
await expectResponse(transmitter.transmit(), {})
109+
110+
scope.done()
105111
})
106112
})
107113

108114
describe(".downloadStream", () => {
109115
const transmitter = new Transmitter("http://example.com/foo")
110116

111117
it("resolves to a response stream on success", async () => {
112-
nock("http://example.com").get("/foo").reply(200, "response body")
118+
const scope = nock("http://example.com").get("/foo").reply(200, "response body")
113119

114120
const stream = await transmitter.downloadStream()
115121

@@ -118,15 +124,19 @@ describe("Transmitter", () => {
118124
stream.on("data", resolve)
119125
})
120126
).resolves.toEqual(Buffer.from("response body"))
127+
128+
scope.done()
121129
})
122130

123131
it("rejects if the status code is not successful", async () => {
124-
nock("http://example.com").get("/foo").reply(404, "not found")
132+
const scope = nock("http://example.com").get("/foo").reply(404, "not found")
125133

126134
await expect(transmitter.downloadStream()).rejects.toMatchObject({
127135
kind: "statusCode",
128136
statusCode: 404
129137
})
138+
139+
scope.done()
130140
})
131141

132142
it("rejects if there's a request error", async () => {
@@ -194,7 +204,7 @@ describe("Transmitter", () => {
194204
}
195205

196206
it("performs an HTTP GET request", async () => {
197-
nock("http://example.invalid").get("/foo").reply(200, "response body")
207+
const scope = nock("http://example.invalid").get("/foo").reply(200, "response body")
198208

199209
const { callback, onData, onError } = await transmitterRequest(
200210
"GET",
@@ -207,10 +217,12 @@ describe("Transmitter", () => {
207217

208218
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
209219
expect(onError).not.toHaveBeenCalled()
220+
221+
scope.done()
210222
})
211223

212224
it("performs an HTTP POST request", async () => {
213-
nock("http://example.invalid")
225+
const scope = nock("http://example.invalid")
214226
.post("/foo", "request body")
215227
.reply(200, "response body")
216228

@@ -226,10 +238,12 @@ describe("Transmitter", () => {
226238

227239
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
228240
expect(onError).not.toHaveBeenCalled()
241+
242+
scope.done()
229243
})
230244

231245
it("performs an HTTP request with query parameters", async () => {
232-
nock("http://example.invalid")
246+
const scope = nock("http://example.invalid")
233247
.get("/foo")
234248
.query({ bar: "baz" })
235249
.reply(200, "response body")
@@ -247,6 +261,8 @@ describe("Transmitter", () => {
247261

248262
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
249263
expect(onError).not.toHaveBeenCalled()
264+
265+
scope.done()
250266
})
251267

252268
it("listens to errors on the request", async () => {
@@ -264,7 +280,7 @@ describe("Transmitter", () => {
264280
})
265281

266282
it("follows redirects", async () => {
267-
nock("http://example.invalid")
283+
const scope = nock("http://example.invalid")
268284
.get("/301")
269285
.reply(301, undefined, {
270286
Location: "http://example.invalid/302",
@@ -301,10 +317,12 @@ describe("Transmitter", () => {
301317

302318
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
303319
expect(onError).not.toHaveBeenCalled()
320+
321+
scope.done()
304322
})
305323

306324
it("redirects to GET method for status code 301/302/303", async () => {
307-
nock("http://example.invalid")
325+
const scope = nock("http://example.invalid")
308326
.post("/301", "request body")
309327
.reply(301, undefined, {
310328
Location: "http://example.invalid/foo"
@@ -335,10 +353,12 @@ describe("Transmitter", () => {
335353
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
336354
expect(onError).not.toHaveBeenCalled()
337355
}
356+
357+
scope.done()
338358
})
339359

340360
it("redirects to the same method for status code 307/308", async () => {
341-
nock("http://example.invalid")
361+
const scope = nock("http://example.invalid")
342362
.post("/307", "request body")
343363
.reply(307, undefined, {
344364
Location: "http://example.invalid/foo"
@@ -365,10 +385,12 @@ describe("Transmitter", () => {
365385
expect(onData).toHaveBeenCalledWith(Buffer.from("response body"))
366386
expect(onError).not.toHaveBeenCalled()
367387
}
388+
389+
scope.done()
368390
})
369391

370392
it("throws an error on a redirect loop", async () => {
371-
nock("http://example.invalid")
393+
const scope = nock("http://example.invalid")
372394
.persist()
373395
.get("/foo")
374396
.reply(302, undefined, {
@@ -392,6 +414,8 @@ describe("Transmitter", () => {
392414
message: "Maximum number of redirects reached"
393415
})
394416
)
417+
418+
scope.done()
395419
})
396420

397421
it("uses the CA file from the config", async () => {

0 commit comments

Comments
 (0)