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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template object type safety in createSpyObject #607

Open
szabyg opened this issue May 15, 2023 · 2 comments
Open

Template object type safety in createSpyObject #607

szabyg opened this issue May 15, 2023 · 2 comments

Comments

@szabyg
Copy link

szabyg commented May 15, 2023

Description

createSpyObject allows creating mock objects for services providing a template object with mock implementations of the methods of your choice. The only problem is that the type definition of the template parameter is Partial<Record<keyof T, any>> allowing to define anything as values.

export function createSpyObject<T>(type: Type<T>, template?: Partial<Record<keyof T, any>>): SpyObject<T>

See /ngneat/spectator/projects/spectator/jest/src/lib/mock.ts#L13

Would the definition be Partial<T>, the compiler would also check the value types and help to keep the unit test implementation in sync with the code to be tested.

Changing this would cause now many failing tests that were "happy" so far but probably "wrong" in the sense of type safety, and - at least in some cases also - from the logic.

This change would be very helpful when refactoring code.

Proposed solution

I propose changing the type definition to

export function createSpyObject<T>(type: Type<T>, template?: Partial<T>): SpyObject<T>

I am not sure though how to deal with backward compatibility when making type safety kick in stronger than before.

Alternatives considered

Not sure. An opt-in solution would be good to allow - especially big projects - to fix their code bit by bit.

Do you want to create a pull request?

No

@szabyg
Copy link
Author

szabyg commented May 16, 2023

I could create a PR but I feel this needs a discussion first.

@NetanelBasal
Copy link
Member

We can release a new major version.

@szabyg szabyg changed the title Teamplate object type safety in createSpyObject Template object type safety in createSpyObject May 16, 2023
szabyg pushed a commit to Eurofunk/spectator that referenced this issue May 16, 2023
BREAKING CHANGE: introducing type safety for the template values in
createSpyObject and mockProvider. This might cause breaking builds
because the compiler will find issues hidden until now.
szabyg pushed a commit to Eurofunk/spectator that referenced this issue May 16, 2023
BREAKING CHANGE: introducing type safety for the template values in
createSpyObject and mockProvider. This might cause breaking builds
because the compiler will find issues hidden until now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants