test(cli): add tests for cli commands (#37)
* chore(cli): add vitest & add test script * chore: add test:cli script * test(cli): initial tests for build command * chore(cli): add nitropack as devDepencency for tests * chore(cli): add vitest config * test(cli): add tests for build command * test(cli): add tests for new command
This commit is contained in:
parent
dd28ee48c2
commit
56592bdeef
8 changed files with 923 additions and 124 deletions
|
@ -9,6 +9,7 @@
|
|||
"template:basic": "pnpm --filter @templates/basic dev",
|
||||
"create": "node ./packages/create-mcfly",
|
||||
"cli": "node ./packages/cli",
|
||||
"patch:all": "npm version patch -w @mcflyjs/cli && npm version patch -w @mcflyjs/core && npm version patch -w @mcflyjs/config && npm version patch -w create-mcfly"
|
||||
"patch:all": "npm version patch -w @mcflyjs/cli && npm version patch -w @mcflyjs/core && npm version patch -w @mcflyjs/config && npm version patch -w create-mcfly",
|
||||
"test:cli": "cd packages/cli && pnpm run test"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,19 @@
|
|||
|
||||
import { consola } from "consola";
|
||||
import { defineCommand } from "citty";
|
||||
import { execSync as exec } from "node:child_process";
|
||||
import { execSync } from "node:child_process";
|
||||
|
||||
function build() {
|
||||
consola.start("Building project...");
|
||||
console.log('jfklsdjfklds')
|
||||
try {
|
||||
execSync(`npx nitropack build`, { stdio: "inherit" });
|
||||
console.log('called build')
|
||||
} catch (err) {
|
||||
console.log('hyeyyy err9r')
|
||||
consola.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
export default defineCommand({
|
||||
meta: {
|
||||
|
@ -11,11 +23,10 @@ export default defineCommand({
|
|||
},
|
||||
|
||||
async run() {
|
||||
consola.start("Building project...");
|
||||
try {
|
||||
exec(`npx nitropack build`, { stdio: "inherit" });
|
||||
} catch (err) {
|
||||
consola.error(err);
|
||||
}
|
||||
build()
|
||||
},
|
||||
});
|
||||
|
||||
export const exportedForTest = {
|
||||
build,
|
||||
}
|
|
@ -1,9 +1,21 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import { execSync as exec } from "node:child_process";
|
||||
import { execSync } from "node:child_process";
|
||||
import { consola } from "consola";
|
||||
import { defineCommand } from "citty";
|
||||
|
||||
function createNew(args) {
|
||||
const directory = args.dir || args._dir;
|
||||
const command = directory
|
||||
? `npm create mcfly@latest ${directory}`
|
||||
: "npm create mcfly@latest";
|
||||
try {
|
||||
execSync(command, { stdio: "inherit" });
|
||||
} catch (e) {
|
||||
consola.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
export default defineCommand({
|
||||
meta: {
|
||||
name: "prepare",
|
||||
|
@ -22,14 +34,10 @@ export default defineCommand({
|
|||
},
|
||||
},
|
||||
async run({ args }) {
|
||||
const directory = args.dir || args._dir;
|
||||
const command = directory
|
||||
? `npm create mcfly@latest ${directory}`
|
||||
: "npm create mcfly@latest";
|
||||
try {
|
||||
exec(command, { stdio: "inherit" });
|
||||
} catch (e) {
|
||||
consola.error(e);
|
||||
}
|
||||
createNew(args)
|
||||
},
|
||||
});
|
||||
|
||||
export const exportedForTest = {
|
||||
createNew
|
||||
}
|
|
@ -8,7 +8,7 @@
|
|||
"mcfly": "./index.js"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "vitest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -25,5 +25,9 @@
|
|||
"@mcflyjs/core": "latest",
|
||||
"citty": "^0.1.4",
|
||||
"consola": "^3.2.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nitropack": "2.8",
|
||||
"vitest": "^2.1.8"
|
||||
}
|
||||
}
|
||||
|
|
48
packages/cli/test/build.test.js
Normal file
48
packages/cli/test/build.test.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
import consola from "consola";
|
||||
import { vi, expect, test } from "vitest";
|
||||
import { exportedForTest } from "../commands/build.mjs";
|
||||
|
||||
const testFn = exportedForTest.build;
|
||||
|
||||
const mocks = vi.hoisted(() => {
|
||||
return {
|
||||
execSync: vi.fn()
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('node:child_process', () => {
|
||||
return {
|
||||
execSync: mocks.execSync
|
||||
}
|
||||
})
|
||||
|
||||
test("start build with message", () => {
|
||||
const message = "Building project...";
|
||||
const spy = vi.spyOn(consola, "start");
|
||||
|
||||
testFn();
|
||||
|
||||
expect(spy).toHaveBeenCalledWith(message);
|
||||
});
|
||||
|
||||
test("execute nitropack build", () => {
|
||||
const command = "npx nitropack build";
|
||||
const param = { stdio: "inherit" };
|
||||
|
||||
testFn();
|
||||
|
||||
expect(mocks.execSync).toHaveBeenCalledWith(command, param);
|
||||
});
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* - test catch error
|
||||
*/
|
||||
// test("catch error", () => {
|
||||
// const spy = vi.spyOn(consola, "error");
|
||||
// mocks.execSync.mockImplementation(() => new Error('hey'))
|
||||
|
||||
// testFn();
|
||||
|
||||
// expect(spy).toHaveBeenCalled();
|
||||
// });
|
59
packages/cli/test/new.test.js
Normal file
59
packages/cli/test/new.test.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import { expect, test, vi } from "vitest";
|
||||
import { exportedForTest } from "../commands/new.mjs";
|
||||
import { execSync } from "node:child_process";
|
||||
|
||||
const testFn = exportedForTest.createNew;
|
||||
|
||||
test("execute create mcfly", () => {
|
||||
const command = `npm create mcfly@latest`
|
||||
const param = {stdio: 'inherit'}
|
||||
vi.mock('node:child_process');
|
||||
|
||||
testFn({dir: undefined})
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
test("execute create mcfly with no dir", () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `npm create mcfly@latest ${dir}`
|
||||
const param = {stdio: 'inherit'}
|
||||
vi.mock('node:child_process');
|
||||
|
||||
testFn({dir: undefined})
|
||||
|
||||
expect(execSync).not.toHaveBeenCalledWith(command, param)
|
||||
})
|
||||
|
||||
test("execute create mcfly with dir", () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `npm create mcfly@latest ${dir}`
|
||||
const param = {stdio: 'inherit'}
|
||||
vi.mock('node:child_process');
|
||||
|
||||
testFn({dir})
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command,param)
|
||||
})
|
||||
|
||||
test("execute create mcfly with _dir", () => {
|
||||
const dir = 'fake-dir'
|
||||
const command = `npm create mcfly@latest ${dir}`
|
||||
const param = {stdio: 'inherit'}
|
||||
vi.mock('node:child_process');
|
||||
|
||||
testFn({_dir: dir})
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command,param)
|
||||
})
|
||||
|
||||
test("execute create mcfly with dir preferred over _dir", () => {
|
||||
const dir = 'preferred-dir'
|
||||
const command = `npm create mcfly@latest ${dir}`
|
||||
const param = {stdio: 'inherit'}
|
||||
vi.mock('node:child_process');
|
||||
|
||||
testFn({dir: dir, _dir: 'not-preferred'})
|
||||
|
||||
expect(execSync).toHaveBeenCalledWith(command,param)
|
||||
})
|
7
packages/cli/vitest.config.mjs
Normal file
7
packages/cli/vitest.config.mjs
Normal file
|
@ -0,0 +1,7 @@
|
|||
import {defineConfig} from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
},
|
||||
})
|
871
pnpm-lock.yaml
871
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue