Skip to content

Commit

Permalink
feat: scaffold tsd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nullishamy committed Aug 28, 2023
1 parent b7e92b9 commit addbddf
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 52 deletions.
8 changes: 8 additions & 0 deletions jest.config.tsd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
displayName: {
color: 'blue',
name: 'types'
},
runner: 'jest-runner-tsd',
testMatch: ['**/test/test-d/*.test-d.ts']
}
218 changes: 174 additions & 44 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"build:watch": "tsc --watch --preserveWatchOutput",
"cleaner": "ts-cleaner --dist lib --watch",
"test": "jest",
"test:full": "npm run build && COMPILE_EXAMPLES=1 jest",
"test:full": "npm run build && COMPILE_EXAMPLES=1 jest && npm run test:types",
"test:types": "jest --config jest.config.tsd.js",
"test:watch": "jest --watch",
"test:coverage": "jest --config jest.coverage.js",
"test:coverage:watch": "jest --watchAll --config jest.coverage.js",
Expand All @@ -33,6 +34,7 @@
},
"homepage": "https://github.com/nullishamy/args-ts#readme",
"devDependencies": {
"@tsd/typescript": "^5.2.2",
"@types/jest": "^29.0.3",
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.37.0",
Expand All @@ -46,6 +48,7 @@
"eslint-plugin-tsdoc": "^0.2.17",
"husky": "^8.0.1",
"jest": "^29.0.3",
"jest-runner-tsd": "^6.0.0",
"lint-staged": "^13.0.3",
"nodemon": "^2.0.20",
"ts-cleaner": "^1.0.5",
Expand Down
6 changes: 3 additions & 3 deletions src/builder/command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Args, DefaultArgTypes } from '../args'
import { CommandError } from '../error'
import { InternalCommand } from '../internal/parse/types'
import { ExtractArgType } from '../internal/types'
import { CommandOpts, StoredCommandOpts, defaultCommandOpts, defaultParserOpts } from '../opts'
import { ArgType } from '../util'

/**
* Base class for all commands, including subcommands. Any user implemented command must extend from this class.
Expand All @@ -28,14 +28,14 @@ export abstract class Command {
// Must use any for it to accept the subtyping this function actually performs
// Black magic happens later on to extract the real subtype out of this `any`
abstract args: <T extends {}> (parser: Args<T>) => Args<any>
abstract run: (args: ExtractArgType<ReturnType<this['args']>>) => Promise<unknown>
abstract run: (args: ArgType<ReturnType<this['args']>>) => Promise<unknown>

/**
* Creates a runner function for use with {@link Command#run}. This exists to provide type inference to the callback, which is not available without a function call.
* @param runFn - the run function
* @returns - the run implementation
*/
runner (runFn: (args: (ExtractArgType<ReturnType<this['args']>> & DefaultArgTypes)) => Promise<unknown>): (args: ExtractArgType<ReturnType<this['args']>>) => Promise<unknown> {
runner (runFn: (args: (ArgType<ReturnType<this['args']>> & DefaultArgTypes)) => Promise<unknown>): (args: ArgType<ReturnType<this['args']>>) => Promise<unknown> {
return async (args) => await runFn(args)
}

Expand Down
3 changes: 0 additions & 3 deletions src/internal/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './help'
export * from './completion'
export * from './argv'
export * from './logging'
export * from './types'
3 changes: 3 additions & 0 deletions src/util/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Args } from '../args'

export type ArgType<ArgObject, Default = never> = ArgObject extends Args<infer TArgs> ? TArgs : Default
15 changes: 15 additions & 0 deletions test/test-d/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Args, a } from '../../src'
import { ArgType } from '../../src/util'
import { parserOpts } from '../shared'
import { expectAssignable } from 'tsd-lite'

describe('Type testing', () => {
it('infers basic flags', () => {
const parser = new Args(parserOpts)
.arg(['--flag'], a.bool())

expectAssignable<ArgType<typeof parser>>({
flag: true
})
})
})
Empty file added test/test-d/util.ts
Empty file.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "lib", /* Redirect output structure to the directory. */
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
//"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true, /* Do not emit comments to output. */
Expand Down

0 comments on commit addbddf

Please sign in to comment.