-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Add unit tests for API user service #2223
Add unit tests for API user service #2223
Conversation
4d42c4c
to
6b725cc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for creating this PR! Left a few comments. I only added each comment once but the same comment should be considered in other places where they apply.
it('should return a user object', async () => { | ||
// GIVEN | ||
const userMock = mockUser(); | ||
jest.spyOn(service, 'getUser').mockResolvedValueOnce(userMock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So instead of mocking the method on the service
being tested, let's mock the underlying prismaService
method instead.
const user = await service.mustGetUser(userMock.id); | ||
|
||
// THEN | ||
(expect(user) as any).toBeUser(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you have implemented custom matchers. Can we add typings for the custom matchers also, instead of using any
? It would also help with discovery so other authors know it exists?
https://redd.one/blog/practical-guide-to-custom-jest-matchers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imolorhe can you give me some guidance here, please?
I defined the type extension (jest.d.ts) under the altair-api
folder. But I'm having issues with which tsconfig.json
to include.
This is what I have tried already:
- The package's (
packages/altair-api/tsconfig.json
) - The root (
tsconfig.json
) - Create a spec tsconfig under the package (
packages/altair-api/tsconfig.spec.json
) and point the jest config to it.
The test suite keeps failing on the missing type:
src/auth/user/user.service.spec.ts:60:20 - error TS2339: Property 'toBeUser' does not exist on type 'JestMatchers<User>'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what you'll want is to create it under @types/jest/index.d.ts
(similar to the one for express)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried placing it under the @types
folder both as @types/jest/index.d.ts
and @types/jest.d.ts
, but in that case jest global keywords (it
, describe
etc.) are lost for me.
I could make it work by defining the declaration file in the altair-api
root. Is it okay?
6b725cc
to
a53d570
Compare
Thanks for the changes! @horvathmarton |
2c87d42
Checks
yarn test-build
Changes proposed in this pull request:
Added unit tests for all public method of altair-api/UserService that is not a proxy call.
Along with that, I added a couple of mocks and custom matches to make the test cases more readable.