Skip to content

Commit

Permalink
Test library typings
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed Nov 10, 2022
1 parent 2d72929 commit 2be09bb
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 18 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
.nyc_output
packages/*/node_modules
packages/*/__snapshots__
*.test-d.ts
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ jobs:
env:
CI: true
FORCE_COLOR: 1
- name: Compile TS code
run: yarn lerna run tsc

- name: Eslint
run: yarn eslint . --ext .js --ext .ts
- name: Test inquirer package (current version)
run: yarn lerna exec npm test --scope inquirer
- name: Test type definitions
run: yarn tsc -p tsconfig.test.json
- name: Test monorepo packages (new version)
run: |
yarn lerna run tsc
yarn jest
run: yarn jest

- name: Upload code coverage
uses: codecov/codecov-action@v2
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"publish": "lerna run tsc && lerna publish",
"pretest": "lerna run tsc && eslint . --ext .js --ext .ts",
"jest": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": "yarn jest && lerna exec npm test --scope inquirer",
"test": "yarn tsc -p tsconfig.test.json && yarn jest && lerna exec npm test --scope inquirer",
"ts-node": "lerna run tsc && ts-node"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/canary/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const answers = await inquirer.prompt([
{
type: 'input',
name: 'last_name',
when: (answers) => Boolean(answers['ask_last_name']),
when: (answers) => Boolean(answers.ask_last_name),
message: "What's your last name",
},
{
Expand Down
33 changes: 21 additions & 12 deletions packages/canary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,26 @@ function createPromptModule() {
>
? Answer
: never;
type GetPromptConfig<U extends keyof typeof promptStore> = GetPrompt<U> extends Prompt<
any,
infer Config
>
? Config
type GetPromptFnConfig<U> = U extends any
? U extends Prompt<any, infer Config>
? Config
: never
: never;
type GetPromptConfig<U extends keyof typeof promptStore> = U extends any
? GetPromptFnConfig<GetPrompt<U>>
: never;

type Answers = Record<string, unknown>;
type ControlFlowConfig<U extends keyof typeof promptStore> = {
name: string;
when?: (answers: Answers) => boolean | Promise<boolean>;
filter?: (answer: GetAnswerType<U>, answers: Answers) => AsyncValue<GetAnswerType<U>>;
};
type ControlFlowConfig<U extends keyof typeof promptStore> = U extends any
? {
name: string;
when?: (answers: Answers) => boolean | Promise<boolean>;
filter?: (
answer: GetAnswerType<U>,
answers: Answers
) => AsyncValue<GetAnswerType<U>>;
}
: never;
type PromptNameToFullConfig<U extends keyof typeof promptStore> = U extends any
? ControlFlowConfig<U> & GetPromptConfig<U> & { type: U }
: never;
Expand All @@ -57,15 +64,17 @@ function createPromptModule() {
const promptSeries = Array.isArray(config) ? config : [config];

for (const promptConfig of promptSeries) {
promptConfig as PromptNameToFullConfig<typeof promptConfig.type>;
const { type, name, when, filter, ...configRest } = promptConfig;

const promptFn = promptStore[type];

if (when != null && !(await when(answers))) {
continue;
}

const answer = await promptFn(configRest as GetPromptConfig<typeof type>, context);
answers[name] = filter ? await filter(answer, answers) : answer;
const answer = await promptFn(configRest as any, context);
answers[name] = filter ? await (filter as any)(answer, answers) : answer;
}

return answers;
Expand Down
2 changes: 1 addition & 1 deletion packages/canary/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
},
"include": ["./src"]
}
84 changes: 84 additions & 0 deletions packages/canary/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import inquirer from './src/index.js';

describe('Library types work properly', () => {
it('input', () => {
expect(() => {
return inquirer.prompt([
{
type: 'input',
name: 'first_name',
message: "What's your first name",
},
{
type: 'input',
name: 'first_name',
message: "What's your first name",
default: 'John',
},
{
type: 'input',
name: 'first_name',
message: "What's your first name",
/* @ts-expect-error: type shouldn't allow this format */
default: 6,
},
{
type: 'input',
name: 'foo',
message: 'foo',
filter(answer: string) {
return 'other string';
},
transformer(answer: string, { isFinal }: { isFinal: boolean }) {
return 'other string';
},
validate(answer: string) {
return 'oh no';
},
when() {
return true;
},
},
{
type: 'input',
name: 'foo',
message: 'foo',
/* @ts-expect-error: type shouldn't allow this format */
filter(answer: number) {
return 3;
},
/* @ts-expect-error: type shouldn't allow this format */
transformer(answer: string) {
return 45;
},
/* @ts-expect-error: type shouldn't allow this format */
validate(answer: string) {
return 1;
},
},
]);
}).toBeInstanceOf(Function);
});

it('select', () => {
expect(() => {
return inquirer.prompt([
{
type: 'select',
name: 'ask_last_name',
message: 'Are you willing to share your last name',
choices: [
{ value: '1', name: 'Yes' },
{ value: '', name: 'No' },
],
},
/* @ts-expect-error: choices option is required */
{
type: 'select',
name: 'ask_last_name',
message: 'Are you willing to share your last name',
},
]);
}).toBeInstanceOf(Function);
});
});
9 changes: 9 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
"noUnusedLocals": false,
"noUnusedParameters": false
},
"include": ["**/*.test-d.ts"]
}

0 comments on commit 2be09bb

Please sign in to comment.