Skip to content

Commit

Permalink
Replace nock with fetch-mock (#217)
Browse files Browse the repository at this point in the history
* Change: migrate test from `nock` to `fetch-mock`

* Change: all tests passing

* fixup

Cleanup: restore original Prettier conf and reformat code

* Cleanup: remove `nock`

* Change: restore payload assertions in all POST requests

* Change: restore payload assertions in all PATCH requests

* Enforce using `fetch` in tests

* Change: reformat again with the original prettier conf

* Cleanup: fix `fetch-mock` dep location
  • Loading branch information
ggalmazor authored Oct 11, 2024
1 parent 114d24b commit 2fd1b24
Show file tree
Hide file tree
Showing 31 changed files with 1,236 additions and 936 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
setupFilesAfterEnv: ["./jest.setup.js"],
};
5 changes: 5 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const fetchMock = require("fetch-mock").default;

fetchMock.config.overwriteRoutes = true;

afterEach(() => fetchMock.restore());
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"fetch-mock": "^11.1.5",
"jest": "^29.7.0",
"js-yaml": ">=3.13.1",
"lodash": ">=4.17.13",
"nock": "^13.5.4",
"prettier": "3.2.5",
"prettier-plugin-organize-imports": "3.2.4",
"ts-jest": "^29.1.2",
Expand Down
11 changes: 6 additions & 5 deletions test/accounts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as nock from "nock";
import { createTestClient, readFixtureAt } from "./util";
import { createTestClient, fetchMockResponse } from "./util";
import fetchMock from "fetch-mock";

const dnsimple = createTestClient();

describe("accounts", () => {
describe("#listAccounts", () => {
it("produces an account list", async () => {
nock("https://api.dnsimple.com")
.get("/v2/accounts")
.reply(readFixtureAt("listAccounts/success-account.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/accounts",
fetchMockResponse("listAccounts/success-account.http")
);

const result = await dnsimple.accounts.listAccounts();

Expand Down
25 changes: 14 additions & 11 deletions test/billing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as nock from "nock";
import { ClientError } from "../lib/main";
import { createTestClient, readFixtureAt } from "./util";
import { createTestClient, fetchMockResponse } from "./util";
import fetchMock from "fetch-mock";

const dnsimple = createTestClient();

Expand All @@ -9,9 +9,10 @@ describe("billing", () => {
const accountId = 1010;

it("produces a charges list", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/billing/charges")
.reply(readFixtureAt("listCharges/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/billing/charges",
fetchMockResponse("listCharges/success.http")
);

const response = await dnsimple.billing.listCharges(accountId);

Expand Down Expand Up @@ -76,9 +77,10 @@ describe("billing", () => {
});

it("throws an error on bad filter", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/billing/charges")
.reply(readFixtureAt("listCharges/fail-400-bad-filter.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/billing/charges",
fetchMockResponse("listCharges/fail-400-bad-filter.http")
);

try {
await dnsimple.billing.listCharges(accountId);
Expand All @@ -93,9 +95,10 @@ describe("billing", () => {
});

it("throws an error on missing scope", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/billing/charges")
.reply(readFixtureAt("listCharges/fail-403.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/billing/charges",
fetchMockResponse("listCharges/fail-403.http")
);

try {
await dnsimple.billing.listCharges(accountId);
Expand Down
146 changes: 77 additions & 69 deletions test/certificates.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as nock from "nock";
import { NotFoundError } from "../lib/main";
import { createTestClient, readFixtureAt } from "./util";
import { createTestClient, fetchMockResponse } from "./util";
import fetchMock from "fetch-mock";

const dnsimple = createTestClient();

Expand All @@ -10,45 +10,49 @@ describe("certificates", () => {
const domainId = "example.com";

it("supports pagination", async () => {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?page=1")
.reply(readFixtureAt("listCertificates/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?page=1",
fetchMockResponse("listCertificates/success.http")
);

await dnsimple.certificates.listCertificates(accountId, domainId, {
page: 1,
});

expect(scope.isDone()).toBeTruthy();
expect(fetchMock.calls()).not.toEqual([]);
});

it("supports extra request options", async () => {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?foo=bar")
.reply(readFixtureAt("listCertificates/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?foo=bar",
fetchMockResponse("listCertificates/success.http")
);

await dnsimple.certificates.listCertificates(accountId, domainId, {
foo: "bar",
});

expect(scope.isDone()).toBeTruthy();
expect(fetchMock.calls()).not.toEqual([]);
});

it("supports sorting", async () => {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?sort=expiration%3Aasc")
.reply(readFixtureAt("listCertificates/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?sort=expiration%3Aasc",
fetchMockResponse("listCertificates/success.http")
);

await dnsimple.certificates.listCertificates(accountId, domainId, {
sort: "expiration:asc",
});

expect(scope.isDone()).toBeTruthy();
expect(fetchMock.calls()).not.toEqual([]);
});

it("produces a certificate list", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates")
.reply(readFixtureAt("listCertificates/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates",
fetchMockResponse("listCertificates/success.http")
);

const response = await dnsimple.certificates.listCertificates(
accountId,
Expand All @@ -63,9 +67,10 @@ describe("certificates", () => {
});

it("exposes the pagination info", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates")
.reply(readFixtureAt("listCertificates/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates",
fetchMockResponse("listCertificates/success.http")
);

const response = await dnsimple.certificates.listCertificates(
accountId,
Expand All @@ -83,17 +88,20 @@ describe("certificates", () => {
const domainId = "example.com";

it("produces a complete list", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?page=1")
.reply(readFixtureAt("pages-1of3.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?page=1",
fetchMockResponse("pages-1of3.http")
);

nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?page=2")
.reply(readFixtureAt("pages-2of3.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?page=2",
fetchMockResponse("pages-2of3.http")
);

nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates?page=3")
.reply(readFixtureAt("pages-3of3.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates?page=3",
fetchMockResponse("pages-3of3.http")
);

const certificates =
await dnsimple.certificates.listCertificates.collectAll(
Expand All @@ -113,9 +121,10 @@ describe("certificates", () => {
const certificateId = 1;

it("produces a certificate", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/1")
.reply(readFixtureAt("getCertificate/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/1",
fetchMockResponse("getCertificate/success.http")
);

const response = await dnsimple.certificates.getCertificate(
accountId,
Expand All @@ -135,9 +144,10 @@ describe("certificates", () => {

describe("when the certificate does not exist", () => {
it("produces an error", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/0")
.reply(readFixtureAt("notfound-certificate.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/0",
fetchMockResponse("notfound-certificate.http")
);

try {
await dnsimple.certificates.getCertificate(accountId, domainId, 0);
Expand All @@ -156,9 +166,10 @@ describe("certificates", () => {
const certificateId = 1;

it("produces a certificate", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/1/download")
.reply(readFixtureAt("downloadCertificate/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/1/download",
fetchMockResponse("downloadCertificate/success.http")
);

const response = await dnsimple.certificates.downloadCertificate(
accountId,
Expand All @@ -175,9 +186,10 @@ describe("certificates", () => {

describe("when the certificate does not exist", () => {
it("produces an error", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/0/download")
.reply(readFixtureAt("notfound-certificate.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/0/download",
fetchMockResponse("notfound-certificate.http")
);

await expect(
dnsimple.certificates.downloadCertificate(accountId, domainId, 0)
Expand All @@ -192,9 +204,10 @@ describe("certificates", () => {
const certificateId = 1;

it("produces a certificate", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/1/private_key")
.reply(readFixtureAt("getCertificatePrivateKey/success.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/1/private_key",
fetchMockResponse("getCertificatePrivateKey/success.http")
);

const response = await dnsimple.certificates.getCertificatePrivateKey(
accountId,
Expand All @@ -209,9 +222,10 @@ describe("certificates", () => {

describe("when the certificate does not exist", () => {
it("produces an error", async () => {
nock("https://api.dnsimple.com")
.get("/v2/1010/domains/example.com/certificates/0/private_key")
.reply(readFixtureAt("notfound-certificate.http"));
fetchMock.get(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/0/private_key",
fetchMockResponse("notfound-certificate.http")
);

try {
await dnsimple.certificates.getCertificatePrivateKey(
Expand All @@ -231,9 +245,10 @@ describe("certificates", () => {
const domainId = "example.com";

it("purchases a certificate", async () => {
nock("https://api.dnsimple.com")
.post("/v2/1010/domains/example.com/certificates/letsencrypt")
.reply(readFixtureAt("purchaseLetsencryptCertificate/success.http"));
fetchMock.post(
"https://api.dnsimple.com/v2/1010/domains/example.com/certificates/letsencrypt",
fetchMockResponse("purchaseLetsencryptCertificate/success.http")
);

const response =
await dnsimple.certificates.purchaseLetsencryptCertificate(
Expand All @@ -253,11 +268,10 @@ describe("certificates", () => {
const certificateId = 101967;

it("issues a certificate", async () => {
nock("https://api.dnsimple.com")
.post(
`/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/issue`
)
.reply(readFixtureAt("issueLetsencryptCertificate/success.http"));
fetchMock.post(
`https://api.dnsimple.com/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/issue`,
fetchMockResponse("issueLetsencryptCertificate/success.http")
);

const response = await dnsimple.certificates.issueLetsencryptCertificate(
accountId,
Expand All @@ -276,13 +290,10 @@ describe("certificates", () => {
const certificateId = 101967;

it("purchases a certificate renewal", async () => {
nock("https://api.dnsimple.com")
.post(
`/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/renewals`
)
.reply(
readFixtureAt("purchaseRenewalLetsencryptCertificate/success.http")
);
fetchMock.post(
`https://api.dnsimple.com/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/renewals`,
fetchMockResponse("purchaseRenewalLetsencryptCertificate/success.http")
);

const response =
await dnsimple.certificates.purchaseLetsencryptCertificateRenewal(
Expand All @@ -307,13 +318,10 @@ describe("certificates", () => {
const newCertificateId = 101972;

it("issues a certificate renewal", async () => {
nock("https://api.dnsimple.com")
.post(
`/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/renewals/${certificateRenewalId}/issue`
)
.reply(
readFixtureAt("issueRenewalLetsencryptCertificate/success.http")
);
fetchMock.post(
`https://api.dnsimple.com/v2/1010/domains/example.com/certificates/letsencrypt/${certificateId}/renewals/${certificateRenewalId}/issue`,
fetchMockResponse("issueRenewalLetsencryptCertificate/success.http")
);

const response =
await dnsimple.certificates.issueLetsencryptCertificateRenewal(
Expand Down
Loading

0 comments on commit 2fd1b24

Please sign in to comment.