forked from folke/devmoji
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(jest): 馃毃 added cli mock for jest
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { MockCli } from "../src/cli-tester" | ||
|
||
test("sync", () => { | ||
const mockCli = new MockCli() | ||
mockCli.test(() => { | ||
console.log("test") | ||
}) | ||
expect(mockCli.stdout?.data).toMatch(/test/) | ||
}) | ||
|
||
test("stderr", () => { | ||
const mockCli = new MockCli() | ||
mockCli.test(() => { | ||
console.error("test") | ||
}) | ||
expect(mockCli.stderr?.data).toMatch(/test/) | ||
}) | ||
|
||
test("sync2", () => { | ||
const mockCli = new MockCli() | ||
mockCli.test(() => { | ||
console.log("test") | ||
}) | ||
expect(mockCli.stdout?.data).toMatch(/test/) | ||
}) | ||
|
||
test("async1", () => { | ||
const mockCli = new MockCli() | ||
mockCli.testAsync(async () => { | ||
await new Promise(done => { | ||
process.stdout.write("test") | ||
done() | ||
}) | ||
}) | ||
expect(mockCli.stdout?.data).toMatch(/test/) | ||
}) | ||
|
||
test("async2", () => { | ||
const mockCli = new MockCli() | ||
mockCli.testAsync(async () => { | ||
console.log("foo") | ||
await new Promise(done => { | ||
console.log("test") | ||
done() | ||
}) | ||
}) | ||
expect(mockCli.stdout?.data).toMatch(/foo/) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Writable, WritableOptions } from "stream" | ||
import { StringDecoder } from "string_decoder" | ||
import { Console } from "console" | ||
|
||
class StringWritable extends Writable { | ||
data = "" | ||
decoder: StringDecoder | ||
constructor(options?: WritableOptions) { | ||
super(options) | ||
this.decoder = new StringDecoder("utf-8") | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
_write(chunk: any, encoding: string, callback: () => void) { | ||
this.data += this.decoder.write(chunk) | ||
callback() | ||
} | ||
|
||
_destroy(err: Error, callback: () => void) { | ||
this.data += this.decoder.end() | ||
callback() | ||
} | ||
} | ||
|
||
export class MockCli { | ||
mocks: [Writable, Writable, Writable["write"]][] = [] | ||
stdout?: StringWritable | ||
stderr?: StringWritable | ||
console = global.console | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
|
||
mock(stream: Writable) { | ||
const mock = new StringWritable() | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
this.mocks.push([mock, stream, stream.write]) | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
stream.write = mock.write.bind(mock) | ||
return mock | ||
} | ||
|
||
restore() { | ||
for (const [mock, stream, write] of this.mocks) { | ||
mock.destroy() | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
stream.write = write.bind(stream) | ||
} | ||
global.console = this.console | ||
} | ||
|
||
setup() { | ||
this.stdout = this.mock(global.process.stdout) | ||
this.stderr = this.mock(global.process.stderr) | ||
this.console = global.console | ||
global.console = new Console({ stdout: this.stdout, stderr: this.stderr }) | ||
} | ||
|
||
test(func: () => void) { | ||
try { | ||
this.setup() | ||
func() | ||
} finally { | ||
this.restore() | ||
} | ||
} | ||
|
||
async testAsync<T>(func: () => Promise<T>) { | ||
this.setup() | ||
try { | ||
await func() | ||
} finally { | ||
this.restore() | ||
} | ||
} | ||
} |