Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: use generic tuples from ts3
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Sep 14, 2018
1 parent 33ae116 commit d89fed0
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"repository": "jdxcode/fancy-test",
"scripts": {
"build": "rm -rf lib && tsc",
"version": "markdown-toc -i README.md && git add README.md",
"lint": "tsc -p test --noEmit && tslint -p test -t stylish",
"posttest": "yarn run lint",
"prepublishOnly": "yarn run build",
"test": "mocha --forbid-only \"test/**/*.test.ts\""
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"version": "markdown-toc -i README.md && git add README.md"
},
"types": "lib/index.d.ts"
}
2 changes: 1 addition & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ export default base(context)
.register('only', () => ({
init: ctx => { ctx.test = it.only }
}))
.register('retries', (count?: number) => ({
.register('retries', (count: number) => ({
init: ctx => ctx.retries = count
}))
2 changes: 1 addition & 1 deletion src/catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as _ from 'lodash'

import {expect} from './chai'

export default (arg?: RegExp | string | ((err: Error) => any), opts: {raiseIfNotThrown?: boolean} = {}) => ({
export default (arg: RegExp | string | ((err: Error) => any), opts: {raiseIfNotThrown?: boolean} = {}) => ({
run() {
if (opts.raiseIfNotThrown !== false) {
throw new Error('expected error to be thrown')
Expand Down
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as _ from 'lodash'

import {EnvOptions} from './types'

export default (env?: {[k: string]: string | null | undefined}, opts: EnvOptions = {}) => {
export default (env: {[k: string]: string | null | undefined}, opts: EnvOptions = {}) => {
const envs: (typeof process.env)[] = []
return {
run() {
Expand Down
3 changes: 1 addition & 2 deletions src/nock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import Nock = require('nock')

import {NockCallback, NockOptions} from './types'

export function nock(host?: string, options?: NockCallback | NockOptions, cb?: NockCallback) {
if (host === undefined) throw new Error('host is undefined')
export function nock(host: string, options: NockCallback | NockOptions, cb?: NockCallback) {
if (typeof options === 'function') {
cb = options
options = {}
Expand Down
2 changes: 1 addition & 1 deletion src/stdmock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const create = <T extends 'stdout' | 'stderr'>(std: T) => (opts: {print?: boolea

export const stdout = create('stdout')
export const stderr = create('stderr')
export const stdin = (input?: string, delay = 0) => {
export const stdin = (input: string, delay = 0) => {
let stdin: any
return {
run: () => {
Expand Down
2 changes: 1 addition & 1 deletion src/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as _ from 'lodash'
/**
* mocks an object's property
*/
export default function (object?: any, path?: string, value?: any) {
export default function<T extends object, K extends keyof T> (object: T, path: K, value: () => T[K]) {
if (object === undefined || path === undefined) throw new Error('should not be undefined')
return {
run(ctx: {stubs: any[]}) {
Expand Down
13 changes: 5 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type PluginBuilder<I, A1 = undefined, A2 = undefined, A3 = undefined, A4 = undefined> = (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<I>
export type PluginBuilder<I, Args extends any[]> = (...args: Args) => Plugin<I>
import Nock = require('nock')

export interface Context {
test: (typeof it | typeof it.skip)
plugins: {[k: string]: PluginBuilder<any, any, any, any, any>}
plugins: {[k: string]: PluginBuilder<any, any>}
expectation?: string
chain: Plugin<any>[]
error?: Error & {code?: string}
Expand All @@ -20,10 +20,7 @@ export interface Plugin<I> {

export interface PluginDef {
output: object
a1: any
a2: any
a3: any
a4: any
args: any[]
}

export interface Plugins {[k: string]: PluginDef}
Expand All @@ -48,8 +45,8 @@ export type Base<I extends Context, T extends Plugins> = {
add<K extends string, O>(key: K, cb: ((context: I) => Promise<O> | O) | Promise<O> | O): Base<I & {[P in K]: O}, T>
do<O>(cb: (context: I & O) => any): Base<O & I, T>
finally(cb: (context: I) => any): Base<I, T>
register<K extends string, O, A1, A2, A3, A4>(key: K, plugin: (arg1?: A1, arg2?: A2, arg3?: A3, arg4?: A4) => Plugin<O & I>): Base<I, T & {[P in K]: {output: O, a1: A1, a2: A2, a3: A3, a4: A4}}>
} & {[P in keyof T]: (arg1?: T[P]['a1'], arg2?: T[P]['a2'], arg3?: T[P]['a3'], arg4?: T[P]['a4']) => Base<T[P]['output'] & I, T>}
register<K extends string, O, A extends any[]>(key: K, plugin: (...args: A) => Plugin<O & I>): Base<I, T & {[P in K]: {output: O, args: A}}>
} & {[P in keyof T]: (...args: T[P]['args']) => Base<T[P]['output'] & I, T>}

export interface EnvOptions {
clear?: boolean
Expand Down
4 changes: 2 additions & 2 deletions test/catch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ describe('catch', () => {

fancy
.do(() => { throw new Error('foobar') })
.catch()
.catch(undefined as any)
.catch('no arg provided to catch')
.end('errors if no args passed')

fancy
.catch()
.catch(undefined as any)
.catch('expected error to be thrown')
.end('errors if no error thrown')

Expand Down

0 comments on commit d89fed0

Please sign in to comment.