Skip to content

Commit

Permalink
Chore(testing): Typecheck test files
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Jul 9, 2024
1 parent 139ab02 commit 55ae385
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 40 deletions.
10 changes: 5 additions & 5 deletions packages/checkbox/checkbox.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe('checkbox prompt', () => {
const { answer, events, getScreen } = await render(checkbox, {
message: 'Select a number',
choices: numberedChoices,
validate(items: ReadonlyArray<unknown>) {
validate: (items: ReadonlyArray<unknown>) => {
if (items.length !== 1) {
return 'Please select only one choice';
}
Expand Down Expand Up @@ -732,9 +732,9 @@ describe('checkbox prompt', () => {
choices: numberedChoices,
theme: {
style: {
renderSelectedChoices(selected: { value: number }[]) {
renderSelectedChoices: (selected: { value: number }[]) => {
if (selected.length > 1) {
return `You have selected ${selected[0].value} and ${selected.length - 1} more.`;
return `You have selected ${(selected[0] as { value: number }).value} and ${selected.length - 1} more.`;
}
return `You have selected ${selected
.slice(0, 1)
Expand Down Expand Up @@ -764,10 +764,10 @@ describe('checkbox prompt', () => {
choices: numberedChoices,
theme: {
style: {
renderSelectedChoices(
renderSelectedChoices: (
selected: { value: number }[],
all: ({ value: number } | Separator)[],
) {
) => {
return `You have selected ${selected.length} out of ${all.length} options.`;
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/confirm/confirm.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('confirm prompt', () => {
it('supports transformer option', async () => {
const { answer, events, getScreen } = await render(confirm, {
message: 'Do you want to proceed?',
transformer: (value) => (value ? 'Oui!' : 'Oh non!'),
transformer: (value: boolean) => (value ? 'Oui!' : 'Oh non!'),
});

expect(getScreen()).toMatchInlineSnapshot('"? Do you want to proceed? (Y/n)"');
Expand Down
6 changes: 3 additions & 3 deletions packages/core/core.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('createPrompt()', () => {
});

it('useEffect: works with setting state at once with objects', async () => {
const Prompt = (config: { message: string }, done: (value: string) => void) => {
const Prompt = (_config: { message: string }, done: (value: string) => void) => {
const [value, setValue] = useState([1, 2]);

useEffect(() => {
Expand Down Expand Up @@ -518,7 +518,7 @@ describe('Error handling', () => {
});

it('surface errors in useEffect cleanup functions', async () => {
const Prompt = (config: object, done: (value: string) => void) => {
const Prompt = (_config: object, done: (value: string) => void) => {
useEffect(() => {
done('done');

Expand All @@ -537,7 +537,7 @@ describe('Error handling', () => {
});

it('prevent returning promises from useEffect hook', async () => {
const Prompt = (config: object, done: (value: string) => void) => {
const Prompt = (_config: object, done: (value: string) => void) => {
// @ts-expect-error: Testing an invalid behavior.
useEffect(async () => {
done('done');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/pagination/lines.test.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest';
import { lines } from './lines.mjs';

function renderResult(result) {
function renderResult(result: string[]) {
return `\n${result.join('\n')}\n`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/editor/editor.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('editor prompt', () => {
it('handles validation', async () => {
const { answer, events, getScreen } = await render(editor, {
message: 'Add a description',
validate(value) {
validate: (value: string) => {
switch (value) {
case '1': {
return true;
Expand Down
17 changes: 9 additions & 8 deletions packages/input/input.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('input prompt', () => {
it('handle transformer', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'What is your name',
transformer: (value, { isFinal }) => (isFinal ? 'Transformed' : `t+${value}`),
transformer: (value: string, { isFinal }: { isFinal: boolean }) =>
isFinal ? 'Transformed' : `t+${value}`,
});

expect(getScreen()).toMatchInlineSnapshot(`"? What is your name t+"`);
Expand All @@ -39,7 +40,7 @@ describe('input prompt', () => {
it('handle synchronous validation', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer 2 ===',
validate: (value) => value === '2',
validate: (value: string) => value === '2',
});

expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 ==="`);
Expand All @@ -65,8 +66,8 @@ describe('input prompt', () => {
it('handle asynchronous validation', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer 2 ===',
validate(value) {
return new Promise((resolve) => {
validate: (value: string) => {
return new Promise<string | boolean>((resolve) => {
if (value === '2') {
resolve(true);
} else {
Expand Down Expand Up @@ -198,13 +199,13 @@ describe('input prompt', () => {
it('is theme-able', async () => {
const { answer, events, getScreen } = await render(input, {
message: 'Answer must be: 2',
validate: (value) => value === '2',
validate: (value?: string) => value === '2',
theme: {
prefix: 'Q:',
style: {
message: (text) => `${text} ===`,
error: (text) => `!! ${text} !!`,
answer: (text) => `_${text}_`,
message: (text: string) => `${text} ===`,
error: (text: string) => `!! ${text} !!`,
answer: (text: string) => `_${text}_`,
},
},
});
Expand Down
14 changes: 7 additions & 7 deletions packages/number/number.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('number prompt', () => {
it('handle synchronous validation', async () => {
const { answer, events, getScreen } = await render(number, {
message: 'Answer 2 ===',
validate: (value) => value === 2,
validate: (value?: number) => value === 2,
});

expect(getScreen()).toMatchInlineSnapshot(`"? Answer 2 ==="`);
Expand All @@ -182,8 +182,8 @@ describe('number prompt', () => {
it('handle asynchronous validation', async () => {
const { answer, events, getScreen } = await render(number, {
message: 'Answer 2 ===',
validate(value) {
return new Promise((resolve) => {
validate: (value?: number) => {
return new Promise<string | boolean>((resolve) => {
if (value === 2) {
resolve(true);
} else {
Expand Down Expand Up @@ -320,13 +320,13 @@ describe('number prompt', () => {
it('is theme-able', async () => {
const { answer, events, getScreen } = await render(number, {
message: 'Answer must be: 2',
validate: (value) => value === 2,
validate: (value?: number) => value === 2,
theme: {
prefix: 'Q:',
style: {
message: (text) => `${text} ===`,
error: (text) => `!! ${text} !!`,
answer: (text) => `_${text}_`,
message: (text: string) => `${text} ===`,
error: (text: string) => `!! ${text} !!`,
answer: (text: string) => `_${text}_`,
},
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/password/password.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('password prompt', () => {
const { answer, events, getScreen } = await render(password, {
message: 'Enter your password',
mask: true,
validate: (value) => value.length >= 8,
validate: (value: string) => value.length >= 8,
});

expect(getScreen()).toMatchInlineSnapshot(`"? Enter your password"`);
Expand Down
14 changes: 8 additions & 6 deletions packages/prompts/prompts.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ describe('@inquirer/prompts', () => {
/**
* Type assertions to validate the interfaces.
*/
Expect<
Equal<
Parameters<typeof checkbox>[0]['theme']['helpMode'],
Parameters<typeof select>[0]['theme']['helpMode']
>
>;
export interface Test {
1: Expect<
Equal<
NonNullable<Parameters<typeof checkbox>[0]['theme']>['helpMode'],
NonNullable<Parameters<typeof select>[0]['theme']>['helpMode']
>
>;
}
2 changes: 1 addition & 1 deletion packages/select/select.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const numberedChoices = [
{ value: 10 },
{ value: 11 },
{ value: 12 },
];
] as const;

afterEach(() => {
vi.useRealTimers();
Expand Down
12 changes: 6 additions & 6 deletions packages/testing/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Stream } from 'node:stream';
import MuteStream from 'mute-stream';
import stripAnsi from 'strip-ansi';
import ansiEscapes from 'ansi-escapes';
import type { Prompt } from '@inquirer/type';
import type { Prompt, Context } from '@inquirer/type';

const ignoredAnsi = new Set([ansiEscapes.cursorHide, ansiEscapes.cursorShow]);

Expand Down Expand Up @@ -42,11 +42,11 @@ class BufferedStream extends Stream.Writable {
}
}

export async function render<TestedPrompt extends Prompt<unknown, unknown>>(
prompt: TestedPrompt,
props: Parameters<TestedPrompt>[0],
options?: Parameters<TestedPrompt>[1],
) {
export async function render<
const Props,
const Value,
const TestedPrompt extends Prompt<Value, Props>,
>(prompt: TestedPrompt, props: Props, options?: Context) {
const input = new MuteStream();
input.unmute();

Expand Down
13 changes: 13 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig.json",
"include": ["**/*.test.mts"],
"exclude": ["packages/inquirer/test/**"],
"compilerOptions": {
"lib": ["es2023"],
"target": "es2022",
"module": "NodeNext",
"moduleResolution": "nodenext",
"skipLibCheck": true,
"noEmit": true
}
}

0 comments on commit 55ae385

Please sign in to comment.