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

feat(mock): support for Jest mocking #40

Merged
merged 7 commits into from
Dec 16, 2018
Merged

feat(mock): support for Jest mocking #40

merged 7 commits into from
Dec 16, 2018

Conversation

dirkluijk
Copy link
Collaborator

@dirkluijk dirkluijk commented Aug 19, 2018

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:

  • Added new createSpyObject() and mockProvider implementation which uses Jest
  • Secondary entry point (implemented with ng-packagr), exporting different versions of:
    • initialModule() (because it is part of the public API)
    • SpectatorWithHost and createHostComponentFactory()
    • SpectatorHTTP and createHTTPFactory()
    • Spectator and createTestComponentFactory()
    • SpectatorService and createService()
  • Example Jest setup in the demo app
  • Example Jest test in the demo app (auth-service.jest.ts)
  • ng-packagr improvements
  • Typescript overloading improvements

TODO?

  • Testing in other projects
  • peerDependencies? (currently, we don't have them)
  • Add docs page about Jest support

mocking

Closes #39.

src/lib/package.json Outdated Show resolved Hide resolved
@tonivj5
Copy link
Contributor

tonivj5 commented Oct 9, 2018

Any progress in this PR?

@dirkluijk dirkluijk changed the base branch from next to master November 16, 2018 00:23
@dirkluijk
Copy link
Collaborator Author

dirkluijk commented Nov 16, 2018

Updated PR description.

Is master the correct target branch?

@NetanelBasal
Copy link
Member

Wow. Looks great!!

I saw that you added types for our custom matchers to jest? Does jest support the syntax of jasmine matchers?

@NetanelBasal
Copy link
Member

@benelliott, please help with the review.

@dirkluijk
Copy link
Collaborator Author

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

@benelliott
Copy link
Collaborator

Looks really cool! Will find some time to review soon.

@jpzwarte
Copy link
Contributor

@dirkluijk wow, nice work!

@magikMaker
Copy link

Great work @dirkluijk 👌

@dirkluijk
Copy link
Collaborator Author

Credits to @LayZeeDK for the entry point idea.

@dirkluijk
Copy link
Collaborator Author

dirkluijk commented Nov 16, 2018

Added additional tests and improved the typings a little bit.

@NetanelBasal

I saw that you added types for our custom matchers to jest? Does jest support the syntax of jasmine matchers?

Yes, they are compatible. And work perfectly fine (proven in Jest tests in the demo app).

Copy link
Collaborator

@benelliott benelliott left a 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 consts, 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!

return newSpy;
}

function installProtoMethods(proto: any) {
Copy link
Collaborator

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Collaborator Author

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": {}
Copy link
Collaborator

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?

Copy link
Collaborator Author

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"
Copy link
Collaborator

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.

Copy link
Collaborator Author

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;
Copy link
Collaborator

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.

Copy link
Member

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

Copy link
Collaborator Author

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"
Copy link
Collaborator

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?

Copy link
Member

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.

Copy link
Collaborator Author

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.

return newSpy;
}

function installProtoMethods(proto: any) {
Copy link
Member

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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isFunction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

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.

Copy link
Member

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;
Copy link
Member

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"
Copy link
Member

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.

@NetanelBasal
Copy link
Member

NetanelBasal commented Nov 17, 2018

@dirkluijk After you finish with the fixes will publish a beta version to npm so we can start testing it with the following cases:

  1. Jasmine - clean project.
  2. Jest - clean project.
  3. An existing project should be able to install the new version without any change.

I'll also give you grant to our gitbook so you can update the docs about jest.

@dirkluijk
Copy link
Collaborator Author

I will check the review comments and fix them asap.

Please check my replies.

Overall it looks great. My only problem with it is that it tidies up a lot of unrelated stuff (various let vs consts, 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.

If you want, I can move these changes to another PR.

I'll also give you grant to our gitbook so you can update the docs about jest.

Awesome.

@NetanelBasal
Copy link
Member

@dirkluijk let me know when you're done, please. (also pull from master)

@dirkluijk
Copy link
Collaborator Author

@NetanelBasal I fixed some issues and pulled from master.
After a new review it might be good to squash all commits in this PR.

Please let me know how I can be of any help.

@NetanelBasal
Copy link
Member

@dirkluijk will release a new beta version first thing in the morning, and we start to test it. Thanks.

@NetanelBasal
Copy link
Member

Published v2.3.0-rc.1, you can start with the tests.

@dirkluijk
Copy link
Collaborator Author

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.

@NetanelBasal
Copy link
Member

@dirkluijk testing it in our jasmine application and everything is working as usual. I'm waiting for your side about jest.

@dirkluijk
Copy link
Collaborator Author

@NetanelBasal thanks. Have been very busy last days, but I will try to make time for it later this week.

@NetanelBasal
Copy link
Member

@dirkluijk ping

@LayZeeDK
Copy link
Contributor

LayZeeDK commented Dec 16, 2018

I ran a fresh project with Spectator 2.3.0-rc.1 through Jest with a trivial component and a trivial service with no type errors or run-time errors.

@NetanelBasal
Copy link
Member

Cool, thanks @LayZeeDK. I don't want to wait anymore with this PR. I will also run a check and publish it.

@LayZeeDK
Copy link
Contributor

We could add Jest versions of the remaining specs in this repository to get the full Jest API exercised.

@NetanelBasal NetanelBasal merged commit 7f058b3 into ngneat:master Dec 16, 2018
@dirkluijk
Copy link
Collaborator Author

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.

@dirkluijk dirkluijk deleted the spectator-with-jest-and-jest-mocking branch December 19, 2018 20:11
@LayZeeDK
Copy link
Contributor

LayZeeDK commented Dec 19, 2018

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)

@LayZeeDK LayZeeDK mentioned this pull request Dec 23, 2018
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.

7 participants