Skip to content

Commit

Permalink
refactor(tests): move to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 committed Jul 5, 2021
1 parent 7e1424a commit e9a1df6
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 186 deletions.
18 changes: 14 additions & 4 deletions package.json
Expand Up @@ -21,7 +21,7 @@
"lint": "prettier --check '{bin,lib,scripts,test}/**/*.{js,json}' index.js README.md package.json",
"lint:fix": "prettier --write '{bin,lib,scripts,test}/**/*.{js,json}' index.js README.md package.json",
"pretest": "npm run -s lint",
"test": "tap --100 --coverage 'test/**/*-test.js'",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --coverage -i 'test/.*/.*'",
"test:e2e": "npm run test:e2e:smoke && npm run test:e2e:custom-fixtures",
"test:e2e:smoke": "test/end-to-end/smoke-test.sh",
"test:e2e:custom-fixtures": "test/end-to-end/custom-fixtures-test.sh"
Expand All @@ -42,13 +42,13 @@
"yargs": "^17.0.0"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"axios": "^0.21.0",
"jest": "^27.0.5",
"pkg": "^5.0.0",
"prettier": "^2.0.1",
"proxyquire": "^2.0.0",
"semantic-release": "^17.2.1",
"supertest": "^6.0.0",
"tap": "^15.0.0"
"supertest": "^6.0.0"
},
"release": {
"plugins": [
Expand Down Expand Up @@ -79,5 +79,15 @@
"extends": [
"github>octokit/.github"
]
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
}
}
@@ -1,11 +1,10 @@
import express from "express";
import supertest from "supertest";
import { test } from "tap";

import { getScenarioFixture } from "../util.js";
import middleware from "../../index.js";

test("release asset (gr2m/octokit-rest-browser-experimental#5)", async (t) => {
test("release asset (gr2m/octokit-rest-browser-experimental#5)", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -32,8 +31,7 @@ test("release asset (gr2m/octokit-rest-browser-experimental#5)", async (t) => {
authorization: "token 0000000000000000000000000000000000000001",
});

t.is(
updateUrl,
expect(updateUrl).toBe(
`http://localhost:3000/uploads.github.com/${fixtureId}/repos/octokit-fixture-org/release-assets/releases/1000/assets{?name,label}`
);

Expand All @@ -54,7 +52,5 @@ test("release asset (gr2m/octokit-rest-browser-experimental#5)", async (t) => {
})
.catch((error) => console.log(error.stack));

t.is(result.body.name, "test-upload.txt");

t.end();
expect(result.body.name).toBe("test-upload.txt");
});
Expand Up @@ -2,12 +2,11 @@ import { URL } from "url";

import express from "express";
import supertest from "supertest";
import { test } from "tap";

import { getScenarioFixture } from "../util.js";
import middleware from "../../index.js";

test("binary response (octokit/rest.js#743)", async (t) => {
test("binary response (octokit/rest.js#743)", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -34,9 +33,8 @@ test("binary response (octokit/rest.js#743)", async (t) => {
})
.catch((error) => error.response);

t.is(getArchiveResponse.status, 302);
t.is(
getArchiveResponse.headers.location,
expect(getArchiveResponse.status).toBe(302);
expect(getArchiveResponse.headers.location).toBe(
`http://localhost:3000/codeload.github.com/${fixtureId}/octokit-fixture-org/get-archive/legacy.tar.gz/refs/heads/main`
);

Expand All @@ -47,7 +45,5 @@ test("binary response (octokit/rest.js#743)", async (t) => {
authorization: "token 0000000000000000000000000000000000000001",
});

t.is(status, 200);

t.end();
expect(status).toBe(200);
});
@@ -1,11 +1,10 @@
import express from "express";
import supertest from "supertest";
import { test } from "tap";

import { getScenarioFixture } from "../util.js";
import middleware from "../../index.js";

test("conflicts test (#8)", async (t) => {
test("conflicts test (#8)", async () => {
const app = express();
app.use(
middleware({
Expand Down Expand Up @@ -35,8 +34,7 @@ test("conflicts test (#8)", async (t) => {
authorization: "token 0000000000000000000000000000000000000001",
});

t.is(
uploadUrl,
expect(uploadUrl).toBe(
`http://localhost:3000/uploads.github.com/${fixtureId}/repos/octokit-fixture-org/release-assets/releases/1000/assets{?name,label}`
);

Expand All @@ -57,7 +55,5 @@ test("conflicts test (#8)", async (t) => {
})
.catch((error) => console.log(error.stack));

t.is(result.body.name, "test-upload.txt");

t.end();
expect(result.body.name).toBe("test-upload.txt");
});
Expand Up @@ -2,12 +2,11 @@ import { URL } from "url";

import express from "express";
import supertest from "supertest";
import { test } from "tap";

import { getScenarioFixture } from "../util.js";
import middleware from "../../index.js";

test("get repository success", async (t) => {
test("get repository success", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -31,11 +30,10 @@ test("get repository success", async (t) => {
authorization: "token 0000000000000000000000000000000000000001",
});

t.is(body.name, "hello-world");
t.end();
expect(body.name).toBe("hello-world");
});

test("get repository without Accept header", async (t) => {
test("get repository without Accept header", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -52,12 +50,11 @@ test("get repository without Accept header", async (t) => {
.get("/api.github.com/fixturesid123/repos/octokit-fixture-org/hello-world")
.catch((error) => error.response);

t.is(status, 400);
t.is(body.error, "Accept header required");
t.end();
expect(status).toBe(400);
expect(body.error).toBe("Accept header required");
});

test("get repository with invalid X-Fixtures-Id header", async (t) => {
test("get repository with invalid X-Fixtures-Id header", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -78,12 +75,11 @@ test("get repository with invalid X-Fixtures-Id header", async (t) => {
})
.catch((error) => error.response);

t.is(status, 404);
t.is(body.error, 'Fixture "fixturesid123" not found');
t.end();
expect(status).toBe(404);
expect(body.error).toBe('Fixture "fixturesid123" not found');
});

test("get repository with incorrect path", async (t) => {
test("get repository with incorrect path", async () => {
const app = express();
app.use(
middleware({
Expand All @@ -108,10 +104,8 @@ test("get repository with incorrect path", async (t) => {
})
.catch((error) => error.response);

t.is(status, 404);
t.is(
body.error,
expect(status).toBe(404);
expect(body.error).toBe(
"GET /foo does not match next fixture: GET /repos/octokit-fixture-org/hello-world"
);
t.end();
});
@@ -1,10 +1,9 @@
import express from "express";
import supertest from "supertest";
import { test } from "tap";

import middleware from "../../index.js";

test("create fixture success", (t) => {
test("create fixture success", (done) => {
const app = express();
app.use(
middleware({
Expand All @@ -18,14 +17,14 @@ test("create fixture success", (t) => {
.send({ scenario: "get-repository" })
.then((response) => {
const { id, url } = response.body;
t.ok(id);
t.is(url, `http://localhost:3000/api.github.com/${id}`);
t.end();
expect(id).toBeTruthy();
expect(url).toBe(`http://localhost:3000/api.github.com/${id}`);
done();
})
.catch(t.error);
.catch(done.fail);
});

test("create fixture error", (t) => {
test("create fixture error", (done) => {
const app = express();
app.use(
middleware({
Expand All @@ -39,14 +38,14 @@ test("create fixture error", (t) => {
.send({ scenario: "nope" })
.catch((error) => error.response)
.then((response) => {
t.is(response.status, 400);
t.is(response.body.error, 'Scenario "nope" not found');
t.end();
expect(response.status).toBe(400);
expect(response.body.error).toBe('Scenario "nope" not found');
done();
})
.catch(t.error);
.catch(done.fail);
});

test("create fixture with custom url", (t) => {
test("create fixture with custom url", (done) => {
const app = express();
app.use(
middleware({
Expand All @@ -61,9 +60,11 @@ test("create fixture with custom url", (t) => {
.send({ scenario: "get-repository" })
.then((response) => {
const { id, url } = response.body;
t.ok(id);
t.is(url, `https://deployment-123.my-fixtures.com/api.github.com/${id}`);
t.end();
expect(id).toBeTruthy();
expect(url).toBe(
`https://deployment-123.my-fixtures.com/api.github.com/${id}`
);
done();
})
.catch(t.error);
.catch(done.fail);
});
Expand Up @@ -2,12 +2,11 @@ import { URL } from "url";

import express from "express";
import supertest from "supertest";
import { test } from "tap";

import { getScenarioFixture } from "../util.js";
import middleware from "../../index.js";

test("get repository redirect (gr2m/octokit-rest-browser-experimental#6)", async (t) => {
test("get repository redirect (gr2m/octokit-rest-browser-experimental#6)", async (done) => {
const app = express();
app.use(
middleware({
Expand All @@ -23,9 +22,9 @@ test("get repository redirect (gr2m/octokit-rest-browser-experimental#6)", async
const fixtureResponse = await agent
.post("/fixtures")
.send({ scenario: "rename-repository" })
.catch(t.error);
.catch(done.fail);

t.is(fixtureResponse.status, 201, fixtureResponse.body.error);
expect(fixtureResponse.status).toBe(201);
const path = new URL(fixtureResponse.body.url).pathname;

const renameResponse = await agent
Expand All @@ -38,13 +37,9 @@ test("get repository redirect (gr2m/octokit-rest-browser-experimental#6)", async
.send({
name: "rename-repository-newname",
})
.catch(t.error);
.catch(done.fail);

t.is(
renameResponse.status,
200,
renameResponse.body.detail || renameResponse.body.error
);
expect(renameResponse.status).toBe(200);

const getResponse = await agent
.get(`${path}/repos/octokit-fixture-org/rename-repository`)
Expand All @@ -54,21 +49,13 @@ test("get repository redirect (gr2m/octokit-rest-browser-experimental#6)", async
})
.catch((error) => error.response);

t.is(
getResponse.status,
301,
getResponse.body.detail || getResponse.body.error
);
t.is(
getResponse.headers.location,
`http://localhost:3000${path}/repositories/1000`,
"redirect URL is prefixed correctly"
expect(getResponse.status).toBe(301);
expect(getResponse.headers.location).toBe(
`http://localhost:3000${path}/repositories/1000`
);

t.end();
});

test("get repository success (redirect with custom URL test)", async (t) => {
test("get repository success (redirect with custom URL test)", async (done) => {
const app = express();
app.use(
middleware({
Expand All @@ -85,9 +72,9 @@ test("get repository success (redirect with custom URL test)", async (t) => {
const fixtureResponse = await agent
.post("/fixtures")
.send({ scenario: "rename-repository" })
.catch(t.error);
.catch(done.fail);

t.is(fixtureResponse.status, 201, fixtureResponse.body.error);
expect(fixtureResponse.status).toBe(201);
const path = new URL(fixtureResponse.body.url).pathname;

const renameResponse = await agent
Expand All @@ -100,13 +87,9 @@ test("get repository success (redirect with custom URL test)", async (t) => {
.send({
name: "rename-repository-newname",
})
.catch(t.error);
.catch(done.fail);

t.is(
renameResponse.status,
200,
renameResponse.body.detail || renameResponse.body.error
);
expect(renameResponse.status).toBe(200);

const getResponse = await agent
.get(`${path}/repos/octokit-fixture-org/rename-repository`)
Expand All @@ -116,16 +99,8 @@ test("get repository success (redirect with custom URL test)", async (t) => {
})
.catch((error) => error.response);

t.is(
getResponse.status,
301,
getResponse.body.detail || getResponse.body.error
);
t.is(
getResponse.headers.location,
`https://deployment123.my-mock-server.com${path}/repositories/1000`,
"redirect URL is prefixed correctly"
expect(getResponse.status).toBe(301);
expect(getResponse.headers.location).toBe(
`https://deployment123.my-mock-server.com${path}/repositories/1000`
);

t.end();
});

0 comments on commit e9a1df6

Please sign in to comment.