Skip to content

Commit 38692e3

Browse files
committed
test: add end-to-end tests
1 parent 8baacbf commit 38692e3

File tree

12 files changed

+171
-10
lines changed

12 files changed

+171
-10
lines changed

.github/workflows/checks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ jobs:
3737
- run: npm ci
3838
- run: npm test
3939

40+
e2e:
41+
name: E2E
42+
runs-on: ubuntu-latest
43+
steps:
44+
- uses: actions/checkout@v4
45+
46+
- name: Use Node.js 20.x
47+
uses: actions/setup-node@v3
48+
with:
49+
node-version: 20.x
50+
cache: "npm"
51+
52+
- run: npm ci
53+
- run: npm run e2e
54+
4055
lint:
4156
name: Lint
4257
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
/dist
33
/coverage
4+
/e2e/out

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"tasks": [
44
{
55
"type": "typescript",
6-
"tsconfig": "tsconfig.json",
6+
"tsconfig": "tsconfig.lib.json",
77
"option": "watch",
88
"problemMatcher": ["$tsc-watch"],
99
"group": {
1010
"kind": "build",
1111
"isDefault": true
1212
},
13-
"label": "tsc: watch - tsconfig.json"
13+
"label": "tsc: watch - tsconfig.lib.json"
1414
}
1515
]
1616
}

e2e/stable-bump.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { expect, it } from "@jest/globals";
2+
import sandbox from "./utils/sandbox.js";
3+
import json from "./utils/json.js";
4+
import { CONVER } from "./utils/binary.js";
5+
6+
it("bumps stable version", async () => {
7+
await sandbox("stable-bump", async ({ $, name, sandbox }) => {
8+
// package.json
9+
await sandbox.writeFiles({
10+
"package.json": json({
11+
name,
12+
version: "1.2.3",
13+
}),
14+
});
15+
16+
// init sandbox
17+
await $`git init`;
18+
await $`${CONVER} init --yes`;
19+
await $`git commit -m ${"chore: first commit"} --allow-empty`;
20+
21+
// check status
22+
let status = await getStatus();
23+
expect(status).toEqual([]);
24+
25+
// add commit
26+
await sandbox.writeFiles({
27+
"foo.txt": "foo",
28+
});
29+
await $`git add foo.txt`;
30+
await $`git commit -m ${"feat: add foo"}`;
31+
32+
// check status
33+
status = await getStatus();
34+
expect(status).toEqual([
35+
{
36+
name,
37+
oldVersion: "1.2.3",
38+
newVersion: "1.3.0",
39+
bump: "minor",
40+
},
41+
]);
42+
43+
async function getStatus() {
44+
const result = await $`${CONVER} status --json`;
45+
const status = JSON.parse(result.stdout) as unknown;
46+
return status;
47+
}
48+
});
49+
});

e2e/utils/binary.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { resolve } from "node:path";
2+
import packageJson from "../../package.json";
3+
4+
export const CONVER = resolve(packageJson.bin.conver);

e2e/utils/json.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function json(data: unknown) {
2+
return JSON.stringify(data, undefined, 2);
3+
}

e2e/utils/sandbox.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { $, ExecaScriptMethod } from "execa";
2+
import { mkdir, rm, writeFile } from "node:fs/promises";
3+
import { join, resolve } from "node:path";
4+
5+
export interface SandboxContext {
6+
$: ExecaScriptMethod<{}>;
7+
root: string;
8+
name: string;
9+
sandbox: Sandbox;
10+
}
11+
12+
export default async function sandbox(
13+
name: string,
14+
callback: (sandbox: SandboxContext) => Promise<void>,
15+
) {
16+
const root = resolve("e2e/out", name);
17+
await mkdir(root, { recursive: true });
18+
try {
19+
await callback({
20+
$: $({ cwd: root }),
21+
root,
22+
name,
23+
sandbox: new Sandbox(root),
24+
});
25+
} finally {
26+
await rm(root, { force: true, recursive: true });
27+
}
28+
}
29+
30+
export class Sandbox {
31+
constructor(private root: string) {}
32+
33+
async writeFiles(files: Record<string, string>): Promise<void> {
34+
for (const [name, data] of Object.entries(files)) {
35+
await writeFile(join(this.root, name), data);
36+
}
37+
}
38+
39+
async removeFiles(files: string[]): Promise<void> {
40+
for (const name of files) {
41+
await rm(join(this.root, name), { force: true, recursive: true });
42+
}
43+
}
44+
}

eslint.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export default [
88
languageOptions: {
99
globals: globals.node,
1010
parserOptions: {
11-
project: ["tsconfig.json", "test/tsconfig.json"],
11+
project: ["tsconfig.lib.json", "tsconfig.test.json"],
1212
},
1313
},
14-
ignores: ["**/*", "!src/**/*"],
14+
ignores: ["**/*", "!src/**/*", "!e2e/**/*"],
1515
},
1616
pluginJs.configs.recommended,
1717
...tseslint.configs.recommendedTypeChecked,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"lint": "eslint src",
4343
"prepare": "husky",
4444
"release": "npm run build && npm run version && npm publish",
45-
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js",
45+
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js src",
46+
"e2e": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js e2e",
4647
"version": "node ./dist/cli/main.js version"
4748
},
4849
"dependencies": {

tsconfig.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"extends": ["@tsconfig/strictest", "@tsconfig/node20"],
33
"compilerOptions": {
4-
"types": ["node", "@jest/globals"],
5-
"rootDir": "src",
6-
"outDir": "dist",
7-
"declaration": true
4+
"types": ["node"]
85
},
9-
"include": ["src"]
6+
"references": [
7+
{ "path": "./tsconfig.lib.json" },
8+
{ "path": "./tsconfig.test.json" }
9+
],
10+
"include": []
1011
}

0 commit comments

Comments
 (0)