Skip to content

Commit 70bb67b

Browse files
committed
Update nock to 15.0.0-beta.4 to work with native fetch
Nock 15 adds support for native fetch. Change-type: patch See: https://github.com/nock/nock/blob/beta/migration_guides/migrating_to_15.md
1 parent cc4fff8 commit 70bb67b

File tree

9 files changed

+65
-100
lines changed

9 files changed

+65
-100
lines changed

npm-shrinkwrap.json

Lines changed: 6 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"mocha": "^10.6.0",
154154
"mock-fs": "^5.2.0",
155155
"mock-require": "^3.0.3",
156-
"nock": "^14.0.4",
156+
"nock": "^15.0.0-beta.4",
157157
"oclif": "^4.17.0",
158158
"rewire": "^7.0.0",
159159
"simple-git": "^3.14.1",

tests/auth/utils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ describe('Utils:', async function () {
108108
});
109109
});
110110

111-
return describe('given the token does authenticate with the server', function () {
111+
describe('given the token does authenticate with the server', function () {
112112
beforeEach(function () {
113113
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
114114
return this.balenaAuthIsLoggedInStub.resolves(true);
@@ -118,7 +118,7 @@ describe('Utils:', async function () {
118118
return this.balenaAuthIsLoggedInStub.restore();
119119
});
120120

121-
return it('should eventually be true', function () {
121+
it('should eventually be true', function () {
122122
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
123123
return expect(promise).to.eventually.be.true;
124124
});

tests/commands/deploy.spec.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import type { Request as ReleaseRequest } from '@balena/compose/dist/release';
2020
import { expect } from 'chai';
2121
import { promises as fs } from 'fs';
2222
import * as _ from 'lodash';
23-
import type * as nock from 'nock';
2423
import * as path from 'path';
2524
import * as sinon from 'sinon';
2625

@@ -181,8 +180,8 @@ describe('balena deploy', function () {
181180
];
182181

183182
api.expectPostRelease({
184-
inspectRequest: (_uri: string, requestBody: nock.Body) => {
185-
const body = requestBody.valueOf() as Partial<ReleaseRequest>;
183+
inspectRequest: async (request: Request) => {
184+
const body = (await request.json()) as Partial<ReleaseRequest>;
186185
expect(body.contract).to.deep.equal({
187186
name: 'testContract',
188187
type: 'sw.application',
@@ -232,8 +231,8 @@ describe('balena deploy', function () {
232231
];
233232

234233
api.expectPostRelease({
235-
inspectRequest: (_uri: string, requestBody: nock.Body) => {
236-
const body = requestBody.valueOf() as Partial<ReleaseRequest>;
234+
inspectRequest: async (request: Request) => {
235+
const body = (await request.json()) as Partial<ReleaseRequest>;
237236
expect(body.contract).to.deep.equal({
238237
name: 'testContract',
239238
type: 'sw.application',
@@ -298,21 +297,21 @@ describe('balena deploy', function () {
298297
statusCode: 500,
299298
// b/c failed requests are retried
300299
times: maxRequestRetries,
301-
inspectRequest: (_uri, requestBody) => {
302-
const imageBody = requestBody as Partial<
300+
inspectRequest: async (request: Request) => {
301+
const body = (await request.json()) as Partial<
303302
import('@balena/compose/dist/release/models').ImageModel
304303
>;
305-
expect(imageBody.status).to.equal('success');
304+
expect(body.status).to.equal('success');
306305
failedImagePatchRequests++;
307306
},
308307
});
309308
// Check that the CLI patches the release with status="failed"
310309
api.expectPatchRelease({
311-
inspectRequest: (_uri, requestBody) => {
312-
const releaseBody = requestBody as Partial<
310+
inspectRequest: async (request: Request) => {
311+
const body = (await request.json()) as Partial<
313312
import('@balena/compose/dist/release/models').ReleaseModel
314313
>;
315-
expect(releaseBody.status).to.equal('failed');
314+
expect(body.status).to.equal('failed');
316315
},
317316
});
318317
api.expectPostImageLabel();
@@ -386,8 +385,8 @@ describe('balena deploy', function () {
386385
let succesfullImagePatchRequests = 0;
387386
api
388387
.optPatch(/^\/v7\/image($|[(?])/, { times: maxRequestRetries })
389-
.reply((_uri, requestBody) => {
390-
const imageBody = requestBody as Partial<
388+
.reply(async (request) => {
389+
const imageBody = (await request.json()) as Partial<
391390
import('@balena/compose/dist/release/models').ImageModel
392391
>;
393392
expect(imageBody.status).to.equal('success');

tests/nock/balena-api-mock.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ export class BalenaAPIMock extends NockMock {
6161
}
6262

6363
public expectDownloadConfig(opts: ScopeOpts = {}) {
64-
this.optPost('/download-config', opts).reply(200, (_uri, body) => {
65-
let deviceType = 'raspberrypi3';
66-
if (typeof body === 'object' && 'deviceType' in body) {
67-
deviceType = body.deviceType;
68-
}
69-
return JSON.parse(`{
64+
this.optPost('/download-config', opts).reply(
65+
200,
66+
async (request: Request) => {
67+
const body = await request.json();
68+
let deviceType = 'raspberrypi3';
69+
if (typeof body === 'object' && 'deviceType' in body) {
70+
deviceType = body.deviceType;
71+
}
72+
return JSON.parse(`{
7073
"applicationId":1301645,
7174
"deviceType":"${deviceType}",
7275
"userId":43699,
@@ -79,7 +82,8 @@ export class BalenaAPIMock extends NockMock {
7982
"deltaEndpoint":"https://delta.balena-cloud.com",
8083
"apiKey":"nothingtoseehere"
8184
}`);
82-
});
85+
},
86+
);
8387
}
8488

8589
public expectApplicationProvisioning(opts: ScopeOpts = {}) {
@@ -284,8 +288,8 @@ export class BalenaAPIMock extends NockMock {
284288

285289
public expectGetAppServiceVars(opts: ScopeOpts = {}) {
286290
this.optGet(/^\/v\d+\/service_environment_variable($|\?)/, opts).reply(
287-
function (uri, _requestBody) {
288-
const match = uri.match(/service_name%20eq%20%27(.+?)%27/);
291+
function (request) {
292+
const match = request.url.match(/service_name%20eq%20%27(.+?)%27/);
289293
const serviceName = match?.[1] || undefined;
290294
let varArray: any[];
291295
if (serviceName) {
@@ -332,8 +336,8 @@ export class BalenaAPIMock extends NockMock {
332336
this.optGet(
333337
/^\/v\d+\/device_service_environment_variable($|\?)/,
334338
opts,
335-
).reply(function (uri, _requestBody) {
336-
const match = uri.match(/service_name%20eq%20%27(.+?)%27/);
339+
).reply(function (request) {
340+
const match = request.url.match(/service_name%20eq%20%27(.+?)%27/);
337341
const serviceName = match?.[1] || undefined;
338342
let varArray: any[];
339343
if (serviceName) {

tests/nock/builder-mock.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
*/
1717

1818
import * as path from 'path';
19-
import * as zlib from 'zlib';
20-
2119
import { NockMock } from './nock-mock';
22-
import { promisify } from 'util';
23-
24-
const gunzipAsync = promisify(zlib.gunzip);
2520

2621
export const builderResponsePath = path.normalize(
2722
path.join(__dirname, '..', 'test-data', 'builder-response'),
@@ -40,20 +35,16 @@ export class BuilderMock extends NockMock {
4035
checkURI: (uri: string) => Promise<void> | void;
4136
checkBuildRequestBody: (requestBody: string | Buffer) => Promise<void>;
4237
}) {
43-
this.optPost(/^\/v3\/build($|[(?])/, opts).reply(
44-
async function (uri, requestBody) {
45-
await opts.checkURI(uri);
46-
if (typeof requestBody === 'string') {
47-
const gzipped = Buffer.from(requestBody, 'hex');
48-
const gunzipped = await gunzipAsync(gzipped);
49-
await opts.checkBuildRequestBody(gunzipped);
50-
return [opts.responseCode, opts.responseBody];
51-
} else {
52-
throw new Error(
53-
`unexpected requestBody type "${typeof requestBody}"`,
54-
);
55-
}
56-
},
57-
);
38+
this.optPost(/^\/v3\/build($|[(?])/, opts).reply(async function (
39+
request: Request,
40+
) {
41+
await opts.checkURI(request.url);
42+
const requestBody = await request.text();
43+
if (typeof requestBody !== 'string') {
44+
throw new Error(`unexpected requestBody type "${typeof requestBody}"`);
45+
}
46+
await opts.checkBuildRequestBody(requestBody);
47+
return [opts.responseCode, opts.responseBody];
48+
});
5849
}
5950
}

tests/nock/docker-mock.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ export class DockerMock extends NockMock {
8181
this.optPost(
8282
new RegExp(`^/build\\?(|.+&)${qs.stringify({ t: opts.tag })}&`),
8383
opts,
84-
).reply(async function (uri, requestBody) {
85-
await opts.checkURI(uri);
86-
if (typeof requestBody === 'string') {
87-
await opts.checkBuildRequestBody(requestBody);
88-
return [opts.responseCode, opts.responseBody];
89-
} else {
84+
).reply(async function (request) {
85+
await opts.checkURI(request.url);
86+
const requestBody = await request.text();
87+
if (typeof requestBody !== 'string') {
9088
throw new Error(`unexpected requestBody type "${typeof requestBody}"`);
9189
}
90+
await opts.checkBuildRequestBody(requestBody);
91+
return [opts.responseCode, opts.responseBody];
9292
});
9393
}
9494

tests/nock/nock-mock.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,47 +100,28 @@ export class NockMock {
100100
return this.optMethod('post', uri, opts);
101101
}
102102

103-
protected inspectNoOp(_uri: string, _requestBody: nock.Body): void {
104-
return undefined;
103+
protected inspectNoOp(_request: Request): void | Promise<void> {
104+
return;
105105
}
106106

107107
protected getInspectedReplyBodyFunction(
108-
inspectRequest: (uri: string, requestBody: nock.Body) => void,
108+
inspectRequest: (request: Request) => void | Promise<void>,
109109
replyBody: nock.ReplyBody,
110110
) {
111-
return function (
112-
this: nock.ReplyFnContext,
113-
uri: string,
114-
requestBody: nock.Body,
115-
cb: (err: NodeJS.ErrnoException | null, result: nock.ReplyBody) => void,
116-
) {
117-
try {
118-
inspectRequest(uri, requestBody);
119-
} catch (err) {
120-
cb(err, '');
121-
}
122-
cb(null, replyBody);
111+
return async function (request: Request): Promise<nock.ReplyBody> {
112+
await inspectRequest(request);
113+
return replyBody;
123114
};
124115
}
125116

126117
protected getInspectedReplyFileFunction(
127-
inspectRequest: (uri: string, requestBody: nock.Body) => void,
118+
inspectRequest: (request: Request) => void | Promise<void>,
128119
replyBodyFile: string,
129120
) {
130-
return function (
131-
this: nock.ReplyFnContext,
132-
uri: string,
133-
requestBody: nock.Body,
134-
cb: (err: NodeJS.ErrnoException | null, result: nock.ReplyBody) => void,
135-
) {
136-
try {
137-
inspectRequest(uri, requestBody);
138-
} catch (err) {
139-
cb(err, '');
140-
}
141-
121+
return async function (request: Request): Promise<nock.ReplyBody> {
122+
await inspectRequest(request);
142123
const replyBody = fs.readFileSync(replyBodyFile);
143-
cb(null, replyBody);
124+
return replyBody;
144125
};
145126
}
146127

tests/nock/supervisor-mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export class SupervisorMock extends NockMock {
4848
}, 10);
4949
},
5050
});
51-
this.optGet('/v2/local/logs', opts).reply((_uri, _reqBody, cb) => {
52-
cb(null, [200, chunkedStream]);
51+
this.optGet('/v2/local/logs', opts).reply(() => {
52+
return [200, chunkedStream];
5353
});
5454
}
5555
}

0 commit comments

Comments
 (0)