Skip to content

Commit f45d98c

Browse files
committed
chore: refactor tests
1 parent 0d50d4d commit f45d98c

File tree

4 files changed

+273
-49
lines changed

4 files changed

+273
-49
lines changed

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
. "$(dirname "$0")/_/husky.sh"
33

44
npx pretty-quick --staged
5+
npx lint-staged

package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"dev": "npm run tsup -- --watch",
1010
"prepublishOnly": "npm run clean && npm run build",
1111
"release": "release-it",
12-
"test": "npm run build && vitest --run",
13-
"toc": "mdmod README.md",
14-
"tsup": "tsup src/index.ts src/cli.ts -d lib"
12+
"test": "run-p build typecheck && vitest --run",
13+
"tsup": "tsup src/index.ts src/cli.ts -d lib",
14+
"typecheck": "tsc --noEmit"
1515
},
1616
"types": "lib/index.d.ts",
1717
"main": "lib/index.js",
@@ -43,8 +43,10 @@
4343
"@types/node": "^18.0.0",
4444
"@types/uuid": "^8.3.4",
4545
"husky": "^8.0.1",
46+
"lint-staged": "^13.0.3",
4647
"mdmod": "^2.0.0",
4748
"mdmod-plugin-toc": "^0.1.1",
49+
"npm-run-all": "^4.1.5",
4850
"prettier": "^2.7.1",
4951
"pretty-quick": "^3.1.3",
5052
"release-it": "^15.1.1",
@@ -54,6 +56,9 @@
5456
"typescript": "^4.7.4",
5557
"vitest": "^0.16.0"
5658
},
59+
"lint-staged": {
60+
"*.md": "mdmod"
61+
},
5762
"homepage": "https://github.com/uetchy/create-create-app",
5863
"repository": {
5964
"type": "git",

tests/index.test.ts

+21-36
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { test, expect } from 'vitest';
88
const pkg = require('../package.json');
99

1010
const SCRIPT_PATH = resolve(__dirname, '..', pkg.bin['create-create-app']);
11+
const FIXTURE_PATH = join(__dirname, 'fixtures');
1112
const TMP_PREFIX = join(tmpdir(), 'create-create-app-');
1213

1314
const DEFAULT_ANSWERS = [
@@ -19,22 +20,31 @@ const DEFAULT_ANSWERS = [
1920
2021
];
2122

22-
test('show usage', async () => {
23-
const { stdout } = await execa(SCRIPT_PATH, []);
24-
expect(stdout).toBe('create-create-app <name>');
25-
}, 300000);
26-
27-
test('template', async () => {
23+
async function useFixture(projectName: string, opts: readonly string[]) {
2824
const tmpDir = mkdtempSync(TMP_PREFIX);
29-
const projectPath = './tests/fixtures/create-test';
25+
const projectPath = join(FIXTURE_PATH, projectName);
3026
const cliPath = resolve(join(projectPath, 'src/cli.js'));
3127

3228
await execa('yarn', ['install'], {
3329
cwd: projectPath,
3430
});
3531

32+
const { stdout } = await execa('node', [cliPath, ...opts], {
33+
cwd: tmpDir,
34+
});
35+
36+
return { stdout, tmpDir };
37+
}
38+
39+
// Tests
40+
41+
test('show usage', async () => {
42+
const { stdout } = await execa(SCRIPT_PATH, []);
43+
expect(stdout).toBe('create-create-app <name>');
44+
}, 300000);
45+
46+
test('template', async () => {
3647
const opts = [
37-
cliPath,
3848
'test',
3949
...DEFAULT_ANSWERS,
4050
'--template',
@@ -45,9 +55,7 @@ test('template', async () => {
4555
'macOS',
4656
];
4757

48-
const { stdout } = await execa('node', opts, {
49-
cwd: tmpDir,
50-
});
58+
const { stdout, tmpDir } = await useFixture('create-test', opts);
5159

5260
expect(readdirSync(join(tmpDir, 'test'))).toEqual(
5361
expect.arrayContaining([
@@ -217,16 +225,7 @@ test('create unlicensed app', async () => {
217225
}, 300000);
218226

219227
test('should create project with minimal footprint', async () => {
220-
const tmpDir = mkdtempSync(TMP_PREFIX);
221-
const projectPath = './tests/fixtures/create-test';
222-
const cliPath = resolve(join(projectPath, 'src/cli.js'));
223-
224-
await execa('yarn', ['install'], {
225-
cwd: projectPath,
226-
});
227-
228228
const opts = [
229-
cliPath,
230229
'test',
231230
...DEFAULT_ANSWERS,
232231
'--license',
@@ -239,29 +238,15 @@ test('should create project with minimal footprint', async () => {
239238
'--skip-git',
240239
];
241240

242-
await execa('node', opts, {
243-
cwd: tmpDir,
244-
});
241+
const { tmpDir } = await useFixture('create-test', opts);
245242

246243
expect(existsSync(`${tmpDir}/test/LICENSE`)).toBeFalsy();
247244
expect(existsSync(`${tmpDir}/test/node_modules`)).toBeFalsy();
248245
expect(existsSync(`${tmpDir}/test/.git`)).toBeFalsy();
249246
}, 300000);
250247

251248
test('should create project with minimal questions', async () => {
252-
const tmpDir = mkdtempSync(TMP_PREFIX);
253-
const projectPath = './tests/fixtures/minimal';
254-
const cliPath = resolve(join(projectPath, 'src/cli.js'));
255-
256-
await execa('yarn', ['install'], {
257-
cwd: projectPath,
258-
});
259-
260-
const opts = [cliPath, 'test'];
261-
262-
await execa('node', opts, {
263-
cwd: tmpDir,
264-
});
249+
const { tmpDir } = await useFixture('minimal', ['test']);
265250

266251
expect(existsSync(`${tmpDir}/test/.git`)).toBeFalsy();
267252
expect(existsSync(`${tmpDir}/test/yarn.lock`)).toBeTruthy();

0 commit comments

Comments
 (0)