Skip to content

Commit d8c94a7

Browse files
committed
add utils directory to place helper functions for tests
1 parent a40cf4c commit d8c94a7

File tree

8 files changed

+46
-37
lines changed

8 files changed

+46
-37
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ node_modules/
22
bin/
33
src/**/*.js
44
src/**/*.d.ts
5-
__tests__/auth.json
5+
**/auth.json

__tests__/__snapshots__/atcoder.ts.snap renamed to tests/__tests__/__snapshots__/atcoder.ts.snap

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`AtCoder get contest and task information contest abc101 1`] = `
3+
exports[`AtCoder get information contest and tasks contest abc101 1`] = `
44
Object {
55
"id": "abc101",
66
"title": "AtCoder Beginner Contest 101",
77
"url": "https://atcoder.jp/contests/abc101",
88
}
99
`;
1010

11-
exports[`AtCoder get contest and task information contest arc101 1`] = `
11+
exports[`AtCoder get information contest and tasks contest arc101 1`] = `
1212
Object {
1313
"id": "arc101",
1414
"title": "AtCoder Regular Contest 101",
1515
"url": "https://atcoder.jp/contests/arc101",
1616
}
1717
`;
1818

19-
exports[`AtCoder get contest and task information task abc101 abc101_a 1`] = `
19+
exports[`AtCoder get information contest and tasks task abc101 abc101_a 1`] = `
2020
Object {
2121
"id": "abc101_a",
2222
"label": "A",
@@ -25,7 +25,7 @@ Object {
2525
}
2626
`;
2727

28-
exports[`AtCoder get contest and task information task abc101 abc101_b 1`] = `
28+
exports[`AtCoder get information contest and tasks task abc101 abc101_b 1`] = `
2929
Object {
3030
"id": "abc101_b",
3131
"label": "B",
@@ -34,7 +34,7 @@ Object {
3434
}
3535
`;
3636

37-
exports[`AtCoder get contest and task information task arc101 arc101_a 1`] = `
37+
exports[`AtCoder get information contest and tasks task arc101 arc101_a 1`] = `
3838
Object {
3939
"id": "arc101_a",
4040
"label": "C",
@@ -43,7 +43,7 @@ Object {
4343
}
4444
`;
4545

46-
exports[`AtCoder get contest and task information tasks abc101 1`] = `
46+
exports[`AtCoder get information contest and tasks tasks abc101 1`] = `
4747
Array [
4848
Object {
4949
"id": "abc101_a",
@@ -72,7 +72,7 @@ Array [
7272
]
7373
`;
7474

75-
exports[`AtCoder get contest and task information tasks arc101 1`] = `
75+
exports[`AtCoder get information contest and tasks tasks arc101 1`] = `
7676
Array [
7777
Object {
7878
"id": "arc101_a",

__tests__/atcoder.ts renamed to tests/__tests__/atcoder.ts

+8-28
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
jest.useFakeTimers();
22
import {username, password} from "./auth.json";
3-
import inquirer from "inquirer";
4-
import {AtCoder} from "../src/atcoder";
5-
import {Cookie} from "../src/cookie";
3+
import {AtCoder} from "../../src/atcoder";
4+
import {disableCookieFileIO, mockLogin} from "../utils";
65

7-
jest.mock("inquirer");
8-
9-
/*
10-
* Cookieファイルの実装を差し替えておく
11-
* コンフィグファイルとの読み書きを行わず、必ず空のセッションが返るようにする
12-
*/
13-
Cookie.prototype.loadConfigFile = jest.fn(async function () {
14-
// @ts-ignore
15-
this.cookies = []
16-
});
17-
Cookie.prototype.saveConfigFile = jest.fn(async function () {
18-
});
19-
20-
// 標準入力を求めることなくログインを行う
21-
const mockLogin = async (atcoder: AtCoder) => {
22-
// ユーザー名とパスワードを標準入力で受け付けるかわりに、JSONファイルから取得した情報を流し込む
23-
// @ts-ignore
24-
inquirer.prompt.mockResolvedValueOnce({username, password});
25-
return await atcoder.login();
26-
};
6+
// ログイン情報が実際にコンフィグファイルに書き込まれないようにする
7+
disableCookieFileIO();
278

289
/*
2910
このテストが失敗する場合、まず以下の点を確認してください
@@ -35,17 +16,16 @@ const mockLogin = async (atcoder: AtCoder) => {
3516
test("AtCoder Login", async () => {
3617
const atcoder = new AtCoder();
3718
expect(await atcoder.checkSession()).toBe(false);
38-
39-
expect(await mockLogin(atcoder)).toBe(true);
19+
expect(await mockLogin(atcoder, {username, password})).toBe(true);
4020
expect(await atcoder.checkSession(true)).toBe(true);
4121
});
4222

43-
describe("AtCoder", async () => {
23+
describe("AtCoder get information", async () => {
4424
const atcoder = new AtCoder();
4525
beforeAll(async () => {
46-
await mockLogin(atcoder);
26+
await mockLogin(atcoder, {username, password});
4727
});
48-
describe("get contest and task information", async()=> {
28+
describe("contest and tasks", async()=> {
4929
const contests = ["abc101", "arc101"];
5030
describe("contest", async () => {
5131
test.each(contests)("%s", async (contest_id) => {
File renamed without changes.

tests/utils/cookie.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Cookie} from "../../src/cookie";
2+
3+
/**
4+
* Cookieクラスの実装を差し替える
5+
* コンフィグファイルとの読み書きを行わず、必ず空のセッションが返るようにする
6+
*/
7+
export function disableCookieFileIO() {
8+
Cookie.prototype.loadConfigFile = jest.fn(async function () {
9+
// @ts-ignore
10+
this.cookies = []
11+
});
12+
Cookie.prototype.saveConfigFile = jest.fn(async function () {
13+
});
14+
}

tests/utils/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./cookie";
2+
export * from "./login";

tests/utils/login.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {AtCoder} from "../../src/atcoder";
2+
import inquirer from "inquirer";
3+
jest.mock("inquirer");
4+
5+
/**
6+
* 標準入力を求めることなくログインを行う
7+
*/
8+
export async function mockLogin(atcoder: AtCoder, {username, password}: { username: string, password: string }) {
9+
// ユーザー名とパスワードを標準入力で受け付けるかわりに、JSONファイルから取得した情報を流し込む
10+
// @ts-ignore
11+
inquirer.prompt.mockResolvedValueOnce({username, password});
12+
return await atcoder.login();
13+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
},
1414
"exclude": [
1515
"node_modules",
16-
"__tests__"
16+
"tests"
1717
]
1818
}

0 commit comments

Comments
 (0)