Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export all types in ts-jest/utils/testing.d.ts file #2086

Closed
mrdulin opened this issue Oct 29, 2020 · 4 comments 路 Fixed by #2096
Closed

Export all types in ts-jest/utils/testing.d.ts file #2086

mrdulin opened this issue Oct 29, 2020 · 4 comments 路 Fixed by #2096
Labels
馃殌 Feature Request new suggested feature

Comments

@mrdulin
Copy link

mrdulin commented Oct 29, 2020

馃殌 Feature Proposal

Consider exporting all types in the ts-jest/utils/testing.d.ts file

Motivation

Declare variable type manually.

Example

import { mocked } from 'ts-jest/utils';

interface SomeInterface {
  validate(): boolean;
}
declare type MockableFunction = (...args: any[]) => any;
declare type MethodKeysOf<T> = {
  [K in keyof T]: T[K] extends MockableFunction ? K : never;
}[keyof T];
declare type PropertyKeysOf<T> = {
  [K in keyof T]: T[K] extends MockableFunction ? never : K;
}[keyof T];
declare type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never;
declare type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never;
interface MockWithArgs<T extends MockableFunction> extends jest.MockInstance<ReturnType<T>, ArgumentsOf<T>> {
  new (...args: ConstructorArgumentsOf<T>): T;
  (...args: ArgumentsOf<T>): ReturnType<T>;
}
declare type MaybeMockedConstructor<T> = T extends new (...args: any[]) => infer R ? jest.MockInstance<R, ConstructorArgumentsOf<T>> : {};
declare type MockedFunction<T extends MockableFunction> = MockWithArgs<T> &
  {
    [K in keyof T]: T[K];
  };
declare type MockedObject<T> = MaybeMockedConstructor<T> &
  {
    [K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunction<T[K]> : T[K];
  } &
  {
    [K in PropertyKeysOf<T>]: T[K];
  };

const UserValidator: SomeInterface = {
  validate: () => true,
};

async function someFunction() {
  return [UserValidator];
}

describe('run test', () => {
  let mockedValidators: Array<MockedObject<SomeInterface>>;
  beforeAll(async () => {
    mockedValidators = (await someFunction()).map((v: SomeInterface) => mocked(v));
  });
});

As you can see, I want to add an Array<MockedObject<SomeInterface>> type for the mockedValidators variable. But ts-jest/utils does not export MockedObject type and related types,source code. So I have to copy and paste them into my test file.

@mrdulin mrdulin added the 馃殌 Feature Request new suggested feature label Oct 29, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Oct 29, 2020

mocked function can accept generic type https://github.com/kulshekhar/ts-jest/blob/master/src/utils/testing.ts#L33

You can provide type like

mocked<MyInterface>()

Does it suit your need ?

@mrdulin
Copy link
Author

mrdulin commented Oct 29, 2020

@ahnpnl mocked<MyInterface>() is a js function call, NOT a ts type. I think it can't be used as a type of mockedValidators variable.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Oct 29, 2020

I see, so you want to use type directly via directive reference or import type

@mrdulin
Copy link
Author

mrdulin commented Oct 29, 2020

@ahnpnl Yes. I want to import MockedObject type like this: import { mocked, MockedObject } from 'ts-jest/utils';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
馃殌 Feature Request new suggested feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants