diff --git a/oclif/package.json b/oclif/package.json index e005d82..5bf412e 100644 --- a/oclif/package.json +++ b/oclif/package.json @@ -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" diff --git a/oclif/tests/commands/hello/index.test.ts b/oclif/tests/commands/hello/index.test.ts index d192bfd..2b8abba 100644 --- a/oclif/tests/commands/hello/index.test.ts +++ b/oclif/tests/commands/hello/index.test.ts @@ -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!"); }); diff --git a/oclif/tests/commands/hello/world.test.ts b/oclif/tests/commands/hello/world.test.ts index d17660e..0576a2a 100644 --- a/oclif/tests/commands/hello/world.test.ts +++ b/oclif/tests/commands/hello/world.test.ts @@ -1,13 +1,8 @@ -import { test, expect } from "vitest"; -import run from "../../run"; +import { test, expect } from "vitest"; +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!"); }); diff --git a/oclif/tests/run.ts b/oclif/tests/run.ts deleted file mode 100644 index fe81da0..0000000 --- a/oclif/tests/run.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { vi } from "vitest"; - -/** - * 標準出力のテストのためのヘルパー - * @param proc 実行する関数 - * @return 標準出力の結果 - */ -async function run(proc: () => PromiseLike): Promise { - 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; diff --git a/oclif/tests/setup.ts b/oclif/tests/setup.ts new file mode 100644 index 0000000..07aa12f --- /dev/null +++ b/oclif/tests/setup.ts @@ -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(); +}); diff --git a/oclif/vitest.config.ts b/oclif/vitest.config.ts new file mode 100644 index 0000000..21ee1ff --- /dev/null +++ b/oclif/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from "vite"; + +export default defineConfig({ + test: { + setupFiles: "tests/setup.ts", + }, +}); diff --git a/oclif/yarn.lock b/oclif/yarn.lock index 4f160dc..7adeda1 100644 --- a/oclif/yarn.lock +++ b/oclif/yarn.lock @@ -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"