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

fix(spectator): make mock template type safe (#607) #609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

szabyg
Copy link

@szabyg szabyg commented 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.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[X] Other... Please describe: Stronger type checks for test mock implementation.

What is the current behavior?

Weak type checking when mocking services, only checking the template keys, not the values types.

Issue Number: #607

What is the new behavior?

The TS compiler will show up mismatches between the service template values and the actual service methods.

Does this PR introduce a breaking change?

[X] Yes
[ ] No

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.

Other information

@stackblitz
Copy link

stackblitz bot commented May 16, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

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
Copy link
Author

szabyg commented Jun 7, 2023

@NetanelBasal Am I supposed to check all PR checklist items (even if they are IMO not relevant) or only the ones that I think are relevant? This PR fixes a bug (sort of) and helps improving code quality. I don't see the need for an additional unit test or documentation, other than the release note explanation of why this is a breaking change.
WDYT and how should I move on with this PR?

@NetanelBasal
Copy link
Member

I'd love to see basic types test. You can use the same library I'm using here

@szabyg
Copy link
Author

szabyg commented Jul 12, 2023

@NetanelBasal Interesting idea and testing library. We spent some hours now to try to find a way to add a meaningful test without testing the type of something that's part of the test itself. On the other hand, the point of the actual change is that the compiler will throw an error if the template type for the provider doesn't match the provider type.

Trying the following things:

import { mockProvider } from '@ngneat/spectator';
import { expectTypeOf } from 'expect-type';

class TestProvider {
  testMethod(a: string): string {
    return '';
  }
}

This would be nice but it doesn't work:

expectTypeOf(mockProvider).parameters.toMatchTypeOf<[TestProvider, { testMethod: (x: string) => string }]>();
expectTypeOf(mockProvider).parameters.not.toMatchTypeOf<[TestProvider, { testMethod: (x: string) => number }]>();

Or this

expectTypeOf(mockProvider).toBeCallableWith(TestProvider, {
  testMethod: (s: string): number => s.length,
});

but .toBeCallableWith never fails, no matter what parameter I give it. To be mentioned, the documentation also doesn't describe what this method is supposed to do.

Would you have a hint what else I could try? Otherwise I suggest taking it as is, the compiler does complain and that's the point, isn't it?

@szabyg
Copy link
Author

szabyg commented Sep 19, 2023

Any ideas/update on this?
I think type safety is nice so the community would benefit from this change.
@NetanelBasal WDYT?

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

Successfully merging this pull request may close these issues.

None yet

2 participants