Skip to content

Commit 46fbca2

Browse files
authored
Update tests to ensure all requests are mocked (#137)
* Change expectation to hasAssertions for exception handling * Disable network access and warn on unmatched mocks
1 parent 8a352c3 commit 46fbca2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

index.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@ import Replicate, { ApiError, Prediction } from 'replicate';
33
import nock from 'nock';
44
import fetch from 'cross-fetch';
55

6-
describe('Replicate client', () => {
7-
let client: Replicate;
6+
let client: Replicate;
7+
const BASE_URL = 'https://api.replicate.com/v1';
8+
9+
nock.disableNetConnect();
810

9-
const BASE_URL = 'https://api.replicate.com/v1';
11+
describe('Replicate client', () => {
12+
let unmatched: Object[] = [];
13+
const handleNoMatch = (req: unknown, options: any, body: string) =>
14+
unmatched.push({ req, options, body });
1015

1116
beforeEach(() => {
1217
client = new Replicate({ auth: 'test-token' });
1318
client.fetch = fetch;
19+
20+
unmatched = [];
21+
nock.emitter.on("no match", handleNoMatch);
22+
});
23+
24+
afterEach(() => {
25+
nock.emitter.off("no match", handleNoMatch);
26+
expect(unmatched).toStrictEqual([]);
27+
28+
nock.abortPendingRequests();
29+
nock.cleanAll();
1430
});
1531

1632
describe('constructor', () => {
@@ -188,7 +204,7 @@ describe('Replicate client', () => {
188204
}, { "Content-Type": "application/json" })
189205

190206
try {
191-
expect.assertions(2);
207+
expect.hasAssertions();
192208

193209
await client.predictions.create({
194210
version: '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa',

0 commit comments

Comments
 (0)