Skip to content

Commit

Permalink
[Tech Debt] Update supertest, use async/await in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
levinmr committed Nov 6, 2024
1 parent 7dd2f5f commit 0436acb
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 201 deletions.
43 changes: 20 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^17.0.1",
"supertest": "^6.3.3"
"supertest": "^7.0.0"
},
"dependencies": {
"express": "^4.18.2",
Expand Down
143 changes: 61 additions & 82 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ routes.forEach((route) => {
db.query = () => Promise.resolve();
});

it("should pass params from the url to db.query and render the result", (done) => {
it("should pass params from the url to db.query and render the result", async () => {
db.query = (params) => {
expect(params.reportAgency).to.equal("fake-agency");
expect(params.reportName).to.equal("fake-report");
Expand All @@ -46,19 +46,16 @@ routes.forEach((route) => {
.get(`/${route}/agencies/fake-agency/reports/fake-report/data`)
.expect(200);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});

it("should not pass the agency param if the request does not specify an agency", (done) => {
it("should not pass the agency param if the request does not specify an agency", async () => {
db.query = (params) => {
expect(params.reportAgency).to.be.undefined;
expect(params.reportName).to.equal("fake-report");
Expand All @@ -73,19 +70,16 @@ routes.forEach((route) => {
.get(`/${route}/reports/fake-report/data`)
.expect(200);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});

it("should merge the params in the url with query params", (done) => {
it("should merge the params in the url with query params", async () => {
db.query = (params) => {
expect(params.reportAgency).to.equal("fake-agency");
expect(params.reportName).to.equal("fake-report");
Expand All @@ -101,39 +95,33 @@ routes.forEach((route) => {
.get(`/${route}/agencies/fake-agency/reports/fake-report/data?limit=50`)
.expect(200);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{ id: 1, date: "2017-01-01" },
{ id: 2, date: "2017-01-02" },
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});

it("should respond with a 400 if db.query rejects", (done) => {
it("should respond with a 400 if db.query rejects", async () => {
db.query = () =>
Promise.reject("This is a test of the emergency broadcast system.");

const dataRequest = request(app)
.get(`/${route}/agencies/fake-agency/reports/fake-report/data`)
.expect(400);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = {
message: "An error occurred. Please check the application logs.",
status: 400,
};
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = {
message: "An error occurred. Please check the application logs.",
status: 400,
};
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});

it("should respond with a 400 if the domain report is not one of the acceptable kinds of reports", (done) => {
it("should respond with a 400 if the domain report is not one of the acceptable kinds of reports", async () => {
db.query = (params) => {
expect(params.domain).to.equal("fakeiscool.gov");
expect(params.reportName).to.equal("browser");
Expand All @@ -155,20 +143,17 @@ routes.forEach((route) => {
.get(`/${route}/domain/fakeiscool.gov/reports/browser/data`)
.expect(400);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = {
message:
"You are requesting a report that cannot be filtered on domain. Please try one of the following reports: site, domain, download, second-level-domain.",
status: 400,
};
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = {
message:
"You are requesting a report that cannot be filtered on domain. Please try one of the following reports: site, domain, download, second-level-domain.",
status: 400,
};
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});

it("should pass params from the url to db.query and render the result for a domain query for a non-download report", (done) => {
it("should pass params from the url to db.query and render the result for a domain query for a non-download report", async () => {
db.query = (params) => {
expect(params.domain).to.equal("fakeiscool.gov");
expect(params.reportName).to.equal("site");
Expand All @@ -187,20 +172,17 @@ routes.forEach((route) => {
.get(`/${route}/domain/fakeiscool.gov/reports/site/data`)
.expect(200);

dataRequest
.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{
id: 1,
date: "2017-01-01",
report_name: "site",
domain: "fakeiscool.gov",
},
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponseBody = handleIfRouteNotice(route, [
{
id: 1,
date: "2017-01-01",
report_name: "site",
domain: "fakeiscool.gov",
},
]);
expect(actualResponse.body).to.deep.equal(expectedResponseBody);
});
});
});
});
Expand All @@ -210,7 +192,7 @@ describe(`app with unspupported version`, () => {
db.query = () => Promise.resolve();
});

it("should not accept unsupported versions", (done) => {
it("should not accept unsupported versions", async () => {
const unsupportedVersion = "v2.x";

db.query = (params) => {
Expand All @@ -232,15 +214,12 @@ describe(`app with unspupported version`, () => {
)
.expect(404);

dataRequest
.then((actualResponse) => {
const expectedResponse = {
_body: expectedErrorMessage,
status: 404,
};
expect(actualResponse).to.include(expectedResponse);
done();
})
.catch(done);
await dataRequest.then((actualResponse) => {
const expectedResponse = {
_body: expectedErrorMessage,
status: 404,
};
expect(actualResponse).to.include(expectedResponse);
});
});
});
Loading

0 comments on commit 0436acb

Please sign in to comment.