Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename assertPromptSnapshot to assertSnapshotCall and move to new tes…
…ting module
  • Loading branch information
c4spar committed Apr 4, 2023
commit 6045a35c5e721f27b6b9e3b2a5336e3d164e1f9b
1 change: 0 additions & 1 deletion prompt/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export {
yellow,
} from "https://deno.land/std@0.170.0/fmt/colors.ts";
export {
basename,
dirname,
join,
normalize,
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/checkbox_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ansi } from "../../../ansi/ansi.ts";
import { Checkbox } from "../../checkbox.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "checkbox prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
5 changes: 3 additions & 2 deletions prompt/test/integration/confirm_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Confirm } from "../../confirm.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "confirm prompt",
meta: import.meta,
osSuffix: ["windows"],
// args: ["--allow-env=ASSERT_SNAPSHOT_CALL_TEST_NAME"],
steps: {
"should confirm": ["y", "\n"],
"should not confirm": ["n", "\n"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_no_location_flag_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt with no location flag",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_no_prefix_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt without prefix",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_output_stream_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt with writer set to stderr",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_prefix_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt with prefix",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_suggestions_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt with suggestions",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/input_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from "../../input.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "input prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/list_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { List } from "../../list.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "list prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/number_decimal_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ansi } from "../../../ansi/ansi.ts";
import { Number } from "../../number.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "number prompt with float",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/number_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ansi } from "../../../ansi/ansi.ts";
import { Number } from "../../number.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "number prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/prompt_list_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Input } from "../../input.ts";
import { Number } from "../../number.ts";
import { prompt, PromptMiddleware, PromptOptions } from "../../prompt.ts";
import { Toggle } from "../../toggle.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "prompt list",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/secret_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Secret } from "../../secret.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "secret prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/select_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ansi } from "../../../ansi/ansi.ts";
import { Select } from "../../select.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "select prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
4 changes: 2 additions & 2 deletions prompt/test/integration/toggle_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Toggle } from "../../toggle.ts";
import { assertPromptSnapshot } from "../../testing.ts";
import { assertSnapshotCall } from "../../../testing/assert_snapshot_call.ts";

await assertPromptSnapshot({
await assertSnapshotCall({
name: "toggle prompt",
meta: import.meta,
osSuffix: ["windows"],
Expand Down
24 changes: 24 additions & 0 deletions testing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Testing

Read the full documentation at https://cliffy.io/docs/testing.

### Snapshot testing

```ts
import { assertSnapshotCall } from "./assert_snapshot_call.ts";
import { Input } from "../prompt/input.ts";

await assertSnapshotCall({
name: "test name",
meta: import.meta,
steps: {
"should enter som text": ["foo bar", "\n"],
},
async fn() {
await Input.prompt({
message: "Whats your name?",
default: "foo",
});
},
});
```
27 changes: 13 additions & 14 deletions prompt/testing.ts → testing/assert_snapshot_call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assertSnapshot } from "https://deno.land/std@0.170.0/testing/snapshot.t
import { eraseDown } from "../ansi/ansi_escapes.ts";
import { basename, red } from "./deps.ts";

export interface AssertPromptSnapshotOptions {
export interface assertSnapshotCallOptions {
/** Test name. */
name: string;
/** Import meta. Required to determine the import url of the test file. */
Expand All @@ -22,7 +22,7 @@ export interface AssertPromptSnapshotOptions {
osSuffix?: Array<typeof Deno.build.os>;
/**
* Arguments passed to the `deno test` command when executing the snapshot
* tests. `--allow-env=ASSERT_PROMPT_SNAPSHOT` is passed by default.
* tests. `--allow-env=ASSERT_SNAPSHOT_CALL` is passed by default.
*/
args?: Array<string>;
/** Enable/disable colors. Default is `false`. */
Expand All @@ -42,14 +42,13 @@ const encoder = new TextEncoder();
* Run prompt snapshot tests.
*
* ```ts
* import { assertPromptSnapshot } from "./testing.ts";
* import { Input } from "./input.ts";
* import { assertSnapshotCall } from "./assert_snapshot_call.ts";
* import { Input } from "../prompt/input.ts";
*
* await assertPromptSnapshot({
* await assertSnapshotCall({
* name: "test name",
* meta: import.meta,
* osSuffix: ["windows"],
* args: [],
* steps: {
* "should enter som text": ["foo bar", "\n"],
* },
Expand All @@ -64,8 +63,8 @@ const encoder = new TextEncoder();
*
* @param options Test options
*/
export async function assertPromptSnapshot(
options: AssertPromptSnapshotOptions,
export async function assertSnapshotCall(
options: assertSnapshotCallOptions,
): Promise<void> {
if (options.meta.main) {
await runTest(options);
Expand All @@ -74,7 +73,7 @@ export async function assertPromptSnapshot(
}
}

function registerTest(options: AssertPromptSnapshotOptions) {
function registerTest(options: assertSnapshotCallOptions) {
const fileName = basename(options.meta.url);

Deno.test({
Expand Down Expand Up @@ -108,7 +107,7 @@ function registerTest(options: AssertPromptSnapshotOptions) {

async function runPrompt(
inputs: Array<string>,
options: AssertPromptSnapshotOptions,
options: assertSnapshotCallOptions,
): Promise<string> {
let output: Deno.CommandOutput | undefined;
let stdout: string | undefined;
Expand All @@ -123,11 +122,11 @@ async function runPrompt(
"run",
...options.args?.length
? options.args
: ["--allow-env=ASSERT_PROMPT_SNAPSHOT"],
: ["--allow-env=ASSERT_SNAPSHOT_CALL"],
options.meta.url,
],
env: {
ASSERT_PROMPT_SNAPSHOT: options.name,
ASSERT_SNAPSHOT_CALL: options.name,
NO_COLOR: options.colors ? "false" : "true",
},
});
Expand Down Expand Up @@ -169,8 +168,8 @@ async function runPrompt(
"\nstderr:\n" + stderr.replaceAll(eraseDown(), eraseDown() + "\n");
}

async function runTest(options: AssertPromptSnapshotOptions) {
const testName = Deno.env.get("ASSERT_PROMPT_SNAPSHOT");
async function runTest(options: assertSnapshotCallOptions) {
const testName = Deno.env.get("ASSERT_SNAPSHOT_CALL");
if (testName === options.name) {
await options.fn();
}
Expand Down
2 changes: 2 additions & 0 deletions testing/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { red } from "https://deno.land/std@0.170.0/fmt/colors.ts";
export { basename } from "https://deno.land/std@0.170.0/path/mod.ts";
1 change: 1 addition & 0 deletions testing/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./assert_snapshot_call.ts";