Skip to content

Commit 28ae21d

Browse files
committed
added jest test
1 parent af0f7ed commit 28ae21d

File tree

8 files changed

+2979
-297
lines changed

8 files changed

+2979
-297
lines changed

babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
};

index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ export default async function main({
9191
);
9292
}
9393
}
94+
95+
export { cache, calculate, clean, download, history, success };

lib/adapters/bitbucket.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ const toStandardBuildConfig = (build) => ({
1919
refName: build.target.ref_name,
2020
});
2121

22-
async function getTotalBuilds(
22+
export async function getTotalBuilds(
2323
user: string,
2424
repo: string,
25-
{ auth }: { auth: string }
25+
{ auth }: { auth?: string }
2626
): Promise<number> {
2727
let res = await got(getBaseUrl(user, repo), { auth });
2828
let resJson = JSON.parse(res.body);
2929
return resJson.size;
3030
}
3131

3232
interface DownloadOptions {
33-
auth: string;
33+
auth?: string;
3434
concurrency: number;
3535
downloadHook?: Function;
3636
repo: string;

lib/adapters/tests/bitbucket.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import got from "got";
2+
import fetchBitbucketPipeline, { getTotalBuilds } from "../bitbucket";
3+
4+
jest.mock("got");
5+
6+
describe("bitbucket", () => {
7+
describe("getTotalBuild", () => {
8+
afterEach(() => {
9+
jest.clearAllMocks();
10+
});
11+
it("should retuns the size from pipeline API", async () => {
12+
got.mockResolvedValue({ body: JSON.stringify({ size: 5 }) });
13+
const r = await getTotalBuilds("test-user", "test-repo", {
14+
auth: undefined,
15+
});
16+
expect(r).toBe(5);
17+
});
18+
19+
it("should throw and exit the error if API errors out", async () => {
20+
got.mockRejectedValue(new Error("The Authorisation failed"));
21+
await expect(
22+
getTotalBuilds("test-user", "test-repo", {
23+
auth: undefined,
24+
})
25+
).rejects.toThrow("The Authorisation failed");
26+
});
27+
});
28+
});

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dev": "ts-node ./cli.ts",
4040
"format": "prettier --write \"{lib/**/*.{js, ts}, *.{js, ts}}\"",
4141
"prepublishOnly": "yarn build",
42-
"test": "ava"
42+
"test": "jest"
4343
},
4444
"dependencies": {
4545
"chalk": "^3.0.0",
@@ -58,12 +58,21 @@
5858
"string-to-stream": "^1.1.0"
5959
},
6060
"devDependencies": {
61+
"@babel/core": "^7.16.0",
62+
"@babel/preset-env": "^7.16.4",
63+
"@babel/preset-typescript": "^7.16.0",
64+
"@types/chai": "^4.3.0",
65+
"@types/jest": "^27.0.3",
6166
"@types/meow": "^5.0.0",
6267
"@types/node": "^16.11.12",
68+
"@types/sinon": "^10.0.6",
6369
"ava": "^3.15.0",
70+
"babel-jest": "^27.4.4",
71+
"chai": "^4.3.4",
6472
"fixturez": "^1.0.1",
73+
"jest": "^27.4.4",
6574
"prettier": "2.5.1",
66-
"sinon": "^7.5.0",
75+
"sinon": "^12.0.1",
6776
"ts-node": "^8.6.2",
6877
"typescript": "3.9.10"
6978
}

test/adapters/bitbucket.test.ts

-4
This file was deleted.

test/utils/math.test.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import test from "ava";
21
import * as math from "../../lib/utils/math";
32

4-
test("math.getMinMax()", (t) => {
5-
t.deepEqual(math.getMinMax([1, 2, 3]), { min: 1, max: 3 });
6-
t.deepEqual(math.getMinMax([4, 2, 3]), { min: 2, max: 4 });
7-
t.deepEqual(math.getMinMax([]), { min: 0, max: 0 });
3+
test("math.getMinMax()", () => {
4+
expect(math.getMinMax([1, 2, 3])).toEqual({ min: 1, max: 3 });
5+
expect(math.getMinMax([4, 2, 3])).toEqual({ min: 2, max: 4 });
6+
expect(math.getMinMax([])).toEqual({ min: 0, max: 0 });
87
});
98

10-
test("math.getMean()", (t) => {
11-
t.deepEqual(math.getMean([1, 2, 3]), 2);
12-
t.deepEqual(math.getMean([6, 4, 5]), 5);
13-
t.deepEqual(math.getMean([1, 2]), 1.5);
9+
test("math.getMean()", () => {
10+
expect(math.getMean([1, 2, 3])).toBe(2);
11+
expect(math.getMean([6, 4, 5])).toBe(5);
12+
expect(math.getMean([1, 2])).toBe(1.5);
1413
});
1514

16-
test("math.getPercentileMean()", (t) => {
17-
t.deepEqual(math.getPercentileMean([1, 2, 3], 75), 1.5);
18-
t.deepEqual(math.getPercentileMean([1, 2, 3, 4], 75), 2);
15+
test("math.getPercentileMean()", () => {
16+
expect(math.getPercentileMean([1, 2, 3], 75)).toBe(1.5);
17+
expect(math.getPercentileMean([1, 2, 3, 4], 75)).toBe(2);
1918
});

0 commit comments

Comments
 (0)