1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 22:08:02 +00:00

use stdout-stderr

This commit is contained in:
Nebel 2022-04-15 16:52:45 +09:00
parent 3e29baac96
commit 15a69ddc1e
7 changed files with 35 additions and 31 deletions

View file

@ -25,6 +25,7 @@
"esbuild-register": "^3.3.2",
"npm-run-all": "^4.1.5",
"oclif": "^3.0.0",
"stdout-stderr": "^0.1.13",
"tsup": "^5.12.5",
"typescript": "^4.6.3",
"vitest": "^0.9.3"

View file

@ -1,8 +1,8 @@
import { test, expect } from "vitest";
import run from "../../run";
import { stdout } from "stdout-stderr";
import Hello from "../../../src/commands/hello/index";
test("hello", async () => {
const res = await run(() => Hello.run(["friend", "--from=oclif"]));
expect(res).toContain("hello friend from oclif!");
await Hello.run(["friend", "--from=oclif"]);
expect(stdout.output).toContain("hello friend from oclif!");
});

View file

@ -1,13 +1,8 @@
import { test, expect } from "vitest";
import run from "../../run";
import { stdout } from "stdout-stderr";
import World from "../../../src/commands/hello/world";
test("hello world", async () => {
const res = await run(() => World.run());
expect(res).toContain("hello world!");
});
test("hello world", async () => {
const res = await run(() => World.run());
expect(res).toContain("hello world!");
await World.run();
expect(stdout.output).toContain("hello world!");
});

View file

@ -1,19 +0,0 @@
import { vi } from "vitest";
/**
*
* @param proc
* @return
*/
async function run(proc: () => PromiseLike<void>): Promise<string> {
const out: string[] = [];
const stdout = vi.spyOn(process.stdout, "write").mockImplementation((val) => {
out.push(val as string);
return true;
});
await proc();
stdout.mockRestore();
return out.join("");
}
export default run;

12
oclif/tests/setup.ts Normal file
View file

@ -0,0 +1,12 @@
import { afterEach, beforeEach } from "vitest";
import { stdout, stderr } from "stdout-stderr";
beforeEach(() => {
stdout.start();
stderr.start();
});
afterEach(() => {
stdout.stop();
stderr.stop();
});

7
oclif/vitest.config.ts Normal file
View file

@ -0,0 +1,7 @@
import { defineConfig } from "vite";
export default defineConfig({
test: {
setupFiles: "tests/setup.ts",
},
});

View file

@ -3615,6 +3615,14 @@ ssri@^9.0.0:
dependencies:
minipass "^3.1.1"
stdout-stderr@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/stdout-stderr/-/stdout-stderr-0.1.13.tgz#54e3450f3d4c54086a49c0c7f8786a44d1844b6f"
integrity sha512-Xnt9/HHHYfjZ7NeQLvuQDyL1LnbsbddgMFKCuaQKwGCdJm8LnstZIXop+uOY36UR1UXXoHXfMbC1KlVdVd2JLA==
dependencies:
debug "^4.1.1"
strip-ansi "^6.0.0"
string-width@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"