Skip to content

Commit 80a7f22

Browse files
committed
initail commit
0 parents  commit 80a7f22

20 files changed

+3868
-0
lines changed

.github/workflows/ci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: ci
2+
on:
3+
push:
4+
5+
jobs:
6+
test:
7+
name: Build and Test
8+
timeout-minutes: 15
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Check out code
12+
uses: actions/checkout@v3
13+
with:
14+
fetch-depth: 2
15+
- name: Setup Node.js environment
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: 18
19+
- run: corepack enable yarn
20+
- id: yarn
21+
run: echo "cache-dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
22+
- uses: actions/cache@v3
23+
name: Setup pnpm cache
24+
with:
25+
path: ${{ steps.yarn.outputs.cache-dir }}
26+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
27+
restore-keys: |
28+
${{ runner.os }}-yarn-
29+
- name: Install dependencies
30+
run: yarn
31+
- name: Test
32+
run: yarn test

.gitignore

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*

.vscode/extensions.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["ZixuanChen.vitest-explorer"]
3+
}

.yarnrc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ueq-study-vitest

__tests__/script.dom.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { test, expect, describe, beforeEach } from "vitest";
2+
import { screen } from "@testing-library/dom";
3+
4+
import { main } from "../script_dom";
5+
6+
beforeEach(() => {
7+
document.body.innerHTML = `
8+
<a data-testid="a" href=""></a>
9+
<section data-testid="section">
10+
<div data-testid="temp" id="temp">temp content</div>
11+
</section>
12+
`;
13+
});
14+
15+
describe("DOM操作の問題", () => {
16+
test("リンクが正しく追加されている", () => {
17+
main();
18+
const a = screen.queryByTestId<HTMLAnchorElement>("a");
19+
expect(a?.textContent).toEqual("REDIMPULZ");
20+
expect(a?.href).toEqual("https://redimpulz.com/");
21+
});
22+
23+
test("section要素の下に指定の要素が追加されている", () => {
24+
main();
25+
const section = screen.queryByTestId("section");
26+
expect(section).toContainHTML(
27+
'<p class="highlight">We hope you enjoy JavaScript.</p>'
28+
);
29+
});
30+
31+
test("IDがtempの要素が削除されている", () => {
32+
main();
33+
expect(screen.queryByTestId("temp")).toEqual(null);
34+
});
35+
});

__tests__/script_class.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, test, expect, beforeEach } from "vitest";
2+
import { Calculator } from "../script_class";
3+
4+
describe("Calculatorのテスト", () => {
5+
let calculator;
6+
7+
beforeEach(() => {
8+
calculator = new Calculator();
9+
});
10+
11+
test("足し算ができる", () => {
12+
const result = calculator.add(3, 5);
13+
expect(result).toBe(8);
14+
});
15+
16+
test("引き算ができる", () => {
17+
const result = calculator.subtract(10, 4);
18+
expect(result).toBe(6);
19+
});
20+
});

__tests__/script_func.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, test, expect } from "vitest";
2+
import { add, subtract, getOjb, getArr } from "../script_func";
3+
4+
describe("add関数のテスト", () => {
5+
test("引数を足し算する", () => expect(add(3, 5)).toBe(8));
6+
});
7+
8+
describe("substruct関数のテスト", () => {
9+
test("引数を引き算する", () => expect(subtract(10, 4)).toBe(6));
10+
});
11+
12+
describe("getObj関数のテスト", () => {
13+
test("オブジェクトが等しい", () =>
14+
expect(getOjb()).toEqual({
15+
id: 1,
16+
name: "hoge",
17+
}));
18+
});
19+
20+
describe("getArr関数のテスト", () => {
21+
test("オブジェクトが等しい", () => expect(getArr()).toContain(1));
22+
});

__tests__/script_mock.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, test, expect, vi } from "vitest";
2+
import { func1, func2, obj, func3 } from "../script_mock";
3+
4+
describe("func1のテスト", () => {
5+
test("モックした関数が呼ばれる", () => {
6+
const mockFunction = vi.fn();
7+
func1(3, 5, mockFunction);
8+
expect(mockFunction).toHaveBeenCalledWith(8);
9+
});
10+
});
11+
12+
describe("func2のテスト", () => {
13+
test("モックしたオブジェクトのメソッドが呼ばれる", () => {
14+
const spy = vi.spyOn(obj, "f");
15+
func2(3, 5);
16+
expect(spy).toHaveBeenCalledWith(8);
17+
// const spy = vi.spyOn(global.console, "log");
18+
});
19+
});
20+
21+
describe("func3のテスト", () => {
22+
test("モックしたモジュールが使われる", () => {
23+
vi.mock("../calc", () => {
24+
return {
25+
multi: () => 10,
26+
};
27+
});
28+
expect(func3(3, 5)).toBe(10);
29+
});
30+
});

calc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const multi = (a, b) => {
2+
return a * b;
3+
};

index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<link rel="stylesheet" type="text/css" href="./style.css">
6+
</head>
7+
<body>
8+
9+
<a href=""></a>
10+
<section>
11+
<div id="temp">temp content</div>
12+
</section>
13+
14+
</body>
15+
<script type="module" src="./script_dom.js"></script>
16+
</html>
17+

package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "ueq-study-vitest",
3+
"version": "1.0.0",
4+
"devDependencies": {
5+
"@testing-library/dom": "^9.3.3",
6+
"@testing-library/jest-dom": "^6.1.3",
7+
"@vitest/coverage-v8": "^0.34.1",
8+
"@vitest/ui": "^1.5.0",
9+
"vitest": "^0.34.1"
10+
},
11+
"dependencies": {
12+
"happy-dom": "^12.9.0",
13+
"vite": "^5.2.9"
14+
},
15+
"type": "module",
16+
"scripts": {
17+
"test": "vitest"
18+
},
19+
"volta": {
20+
"node": "18.17.1",
21+
"yarn": "4.0.2"
22+
}
23+
}

script_class.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class Calculator {
2+
add(a, b) {
3+
return a + b;
4+
}
5+
subtract(a, b) {
6+
return a - b;
7+
}
8+
}

script_dom.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export const main = () => {
2+
const link = document.querySelector("a");
3+
link.textContent = "REDIMPULZ";
4+
link.href = "https://redimpulz.com/";
5+
6+
const section = document.querySelector("section");
7+
const p = document.createElement("p");
8+
p.textContent = "We hope you enjoy JavaScript.";
9+
section.appendChild(p);
10+
p.setAttribute("class", "highlight");
11+
12+
const temp = document.querySelector("#temp");
13+
section.removeChild(temp);
14+
};
15+
16+
window.addEventListener("load", main);

script_func.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const add = (a, b) => {
2+
return a + b;
3+
};
4+
5+
export const subtract = (a, b) => {
6+
return a - b;
7+
};
8+
9+
export const getOjb = () => {
10+
return {
11+
id: 1,
12+
name: "hoge",
13+
};
14+
};
15+
16+
export const getArr = () => {
17+
return [1, 2, 3];
18+
};

script_mock.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { multi } from "./calc";
2+
3+
export const func1 = (a, b, f) => {
4+
f(a + b);
5+
};
6+
7+
export const obj = {
8+
f: (v) => console.log(v),
9+
};
10+
11+
export const func2 = (a, b) => {
12+
obj.f(a + b);
13+
};
14+
15+
export const func3 = (a, b) => {
16+
return multi(a, b);
17+
};

0 commit comments

Comments
 (0)