-
-
Notifications
You must be signed in to change notification settings - Fork 177
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
feat(mock): support for Jest mocking #40
feat(mock): support for Jest mocking #40
Conversation
Any progress in this PR? |
Updated PR description. Is |
Wow. Looks great!! I saw that you added types for our custom matchers to jest? Does jest support the syntax of jasmine matchers? |
@benelliott, please help with the review. |
I think that in the future, we might even move away from default mocking and have it more like this: import { createSpectator } from '@netbasal/spectator'; // no mocks at all
import { createSpectator } from '@netbasal/spectator/jest'; // with Jest mocks
import { createSpectator } from '@netbasal/spectator/jasmine'; // with Jasmine spies |
Looks really cool! Will find some time to review soon. |
@dirkluijk wow, nice work! |
Great work @dirkluijk 👌 |
Credits to @LayZeeDK for the entry point idea. |
Added additional tests and improved the typings a little bit.
Yes, they are compatible. And work perfectly fine (proven in Jest tests in the demo app). |
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.
Overall it looks great. My only problem with it is that it tidies up a lot of unrelated stuff (various let
vs const
s, internal imports etc) which IMO clutter the PR a bit and make it harder to see what is actually being changed. I would prefer to see this stuff fixed in a separate PR.
I will mark it as approved and leave it up to @NetanelBasal to decide whether that needs addressing before it can be merged.
In general though - thanks for the hard work!
projects/spectator/jest/src/mock.ts
Outdated
return newSpy; | ||
} | ||
|
||
function installProtoMethods(proto: any) { |
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.
Code reuse could potentially be improved here by (internally) exporting this function in the base library. Wouldn't want it to be part of the public API though.
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.
Agree
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.
Will do
@@ -0,0 +1,3 @@ | |||
{ | |||
"ngPackage": {} |
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.
Is this just a mandatory file that exposes this part of the project as a submodule?
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.
@@ -26,11 +25,5 @@ | |||
"dependencies": { | |||
"dom-testing-library": "2.1.0", | |||
"jquery": "^3.3.1" |
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.
Any particular reason for moving these in this PR? Was something broken?
I generally agree that they should be tidied up but don't think it needs to be included here unless necessary.
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.
In combination with the ng-package.json (to support an additional entry point) I had to move this configuration to the ng-package.json.
|
||
toHaveStyle(style: { [styleKey: string]: any }): boolean; | ||
|
||
toHaveData({ data, val }: { data: string; val: string }): boolean; |
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.
Narrowing these jasmine matcher types could potentially be a breaking change (?). I see why you have done it though.
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.
You can use overload and not introduce a breaking change
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.
👍
@@ -12,7 +12,7 @@ | |||
"experimentalDecorators": true, | |||
"importHelpers": true, | |||
"types": [ | |||
"@types/jasmine" |
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.
Kind of surprised this doesn't break anything! Are jest types a superset of jasmine's or something?
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.
@dirkluijk in the past it works for me only because I had both jest and jasmine installed. We should test it on a clean project.
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 cannot load both @types/jasmine
and @types/jest
at the same time. That is because @types/jest
redefines Jasmine namespace and typings. See https:/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/v16/index.d.ts#L178.
projects/spectator/jest/src/mock.ts
Outdated
return newSpy; | ||
} | ||
|
||
function installProtoMethods(proto: any) { |
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.
Agree
@@ -0,0 +1,5 @@ | |||
import { Type } from '@angular/core'; | |||
|
|||
export function isType(v: any): v is Type<any> { |
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.
isFunction
?
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 actually copied this type guard from Angular: https:/angular/angular/blob/4c2ce4e8ba4c5ac5ce8754d67bc6603eaad4564a/packages/core/src/type.ts
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.
Not sure what is better. Let me know if you insist of isFunction
. "Is function" is what the implementation does, but it's intention is to check whether you pass a Type (Component). Also, it is a type guard for Type
.
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.
Ok, let's leave it.
|
||
toHaveStyle(style: { [styleKey: string]: any }): boolean; | ||
|
||
toHaveData({ data, val }: { data: string; val: string }): boolean; |
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.
You can use overload and not introduce a breaking change
@@ -12,7 +12,7 @@ | |||
"experimentalDecorators": true, | |||
"importHelpers": true, | |||
"types": [ | |||
"@types/jasmine" |
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.
@dirkluijk in the past it works for me only because I had both jest and jasmine installed. We should test it on a clean project.
@dirkluijk After you finish with the fixes will publish a beta version to npm so we can start testing it with the following cases:
I'll also give you grant to our gitbook so you can update the docs about jest. |
I will check the review comments and fix them asap. Please check my replies.
If you want, I can move these changes to another PR.
Awesome. |
@dirkluijk let me know when you're done, please. (also pull from master) |
@NetanelBasal I fixed some issues and pulled from master. Please let me know how I can be of any help. |
@dirkluijk will release a new beta version first thing in the morning, and we start to test it. Thanks. |
Published v2.3.0-rc.1, you can start with the tests. |
Awesome. Will test it in a blank project tonight or tomorrow. I will share the repo and its Circle CI build status when I've finished it. |
@dirkluijk testing it in our jasmine application and everything is working as usual. I'm waiting for your side about jest. |
@NetanelBasal thanks. Have been very busy last days, but I will try to make time for it later this week. |
@dirkluijk ping |
I ran a fresh project with Spectator |
Cool, thanks @LayZeeDK. I don't want to wait anymore with this PR. I will also run a check and publish it. |
We could add Jest versions of the remaining specs in this repository to get the full Jest API exercised. |
Sorry for this late reply. Always hard to leave some spare time for open-source commitment. ;) For now, I assume everything is working correctly I guess. If not, let me know. |
Thanks, Dirk! I use Jest on all my personal projects so I'm very grateful 😊 (Even though I use Sinon for test doubles and Chai for assertions) |
This PR adds support for Jest mocks. It adds an additional entry point
@netbasal/spectator/jest
to import symbols from. When you do, it will automatically use jest for mocking.In this PR:
createSpyObject()
andmockProvider
implementation which uses JestinitialModule()
(because it is part of the public API)SpectatorWithHost
andcreateHostComponentFactory()
SpectatorHTTP
andcreateHTTPFactory()
Spectator
andcreateTestComponentFactory()
SpectatorService
andcreateService()
auth-service.jest.ts
)TODO?
Closes #39.