Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pkoch@toolbox:~/github.com/pkoch/jest-mock-process$ ./node_modules/.bin/ts-node
> const w = process.stderr.write;
'use strict'
> let aa: ArgsType<typeof w>;
undefined
> .type aa
let aa: [string, string?, Function?]
>
pkoch@toolbox:~/github.com/pkoch/jest-mock-process$ git d
diff --git a/lib/__tests__/index.test.ts b/lib/__tests__/index.test.ts
index e9d8092..e729dd3 100644
--- a/lib/__tests__/index.test.ts
+++ b/lib/__tests__/index.test.ts
@@ -99,7 +99,8 @@ describe('Mock Process Stdout', () => {
});
describe('Mock Process Stderr', () => {
- let mockStderr: jest.SpyInstance<(buffer: Buffer | string, encoding?: string, cb?: Function) => boolean>;
+ // let mockStderr: jest.SpyInstance<(buffer: Buffer | string, encoding?: string, cb?: Function) => boolean>;
+ let mockStderr: jest.SpyInstance<boolean, [string, string?, Function?]>;
beforeEach(() => {
mockStderr = mockProcessStderr();
diff --git a/lib/index.ts b/lib/index.ts
index b671a4d..a15a005 100644
--- a/lib/index.ts
+++ b/lib/index.ts
@@ -4,7 +4,7 @@ export function spyOnImplementing<
T extends object,
M extends keyof T,
F extends T[M] extends (...args: any[]) => any ? T[M] : never,
->(target: T, property: M, impl: F): jest.SpyInstance {
+>(target: T, property: M, impl: F): jest.SpyInstance<ReturnType<F>, ArgsType<F>> {
maybeMockRestore(target[property])
return jest.spyOn(target, property).mockImplementation(impl)
}
pkoch@toolbox:~/github.com/pkoch/jest-mock-process$ tsc lib/__tests__/index.test.ts
lib/__tests__/index.test.ts:53:9 - error TS2322: Type 'SpyInstance<boolean, []>' is not assignable to type 'SpyInstance<(buffer: string | Buffer, encoding?: string, cb?: Function) => boolean, any>'.
Type 'boolean' is not assignable to type '(buffer: string | Buffer, encoding?: string, cb?: Function) => boolean'.
53 mockStdout = mockProcessStdout();
~~~~~~~~~~
lib/__tests__/index.test.ts:106:9 - error TS2322: Type 'SpyInstance<boolean, []>' is not assignable to type 'SpyInstance<boolean, [string, string?, Function?]>'.
Property '0' is missing in type '[]' but required in type '[string, string?, Function?]'.
106 mockStderr = mockProcessStderr();
~~~~~~~~~~
lib/__tests__/index.test.ts:158:9 - error TS2322: Type 'SpyInstance<void, []>' is not assignable to type 'SpyInstance<(message?: any, ...optionalParams: any[]) => void, any>'.
Type 'void' is not assignable to type '(message?: any, ...optionalParams: any[]) => void'.
158 mockLog = mockConsoleLog();
~~~~~~~
Found 3 errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment