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

Adapt tool for jest #14

Open
unlight opened this issue Apr 11, 2020 · 3 comments
Open

Adapt tool for jest #14

unlight opened this issue Apr 11, 2020 · 3 comments

Comments

@unlight
Copy link

unlight commented Apr 11, 2020

Unfortunately, this helper does not work for jest.
But there are few place to fix.

        const getterSpy = spyOnProperty(this.stub, propertyName, 'get').and.callFake(() => this.spy[propertyName]._value);
        const setterSpy = spyOnProperty(this.stub, propertyName, 'set').and.callFake(value => this.spy[propertyName]._value = value);
// =>
        const getterSpy = jest
            .spyOn(this.stub, propertyName, 'get')
            .mockImplementation(() => this.spy[propertyName]._value);
        const setterSpy = jest
            .spyOn(this.stub, propertyName, 'set')
            .mockImplementation((value) => (this.spy[propertyName]._value = value));
const spy = jasmine.createSpy(propertyName);
// =>
const spy = jest.fn().mockName(propertyName);

Maybe possible detect environment in runtime and call appropriate methods? Or it is better to create separate module for jest...

@chuanqisun
Copy link
Owner

chuanqisun commented Apr 12, 2020

Could you help me understand your project goal? Do you want to keep the syntax of jasmine-mock-factory but use jest instead of jasmine for assertion and mocking?

@unlight
Copy link
Author

unlight commented Apr 12, 2020

Yes. Currently I'm using NestJS (nodejs backend framework) and architecture of NestJS is similar to Angular. I realized that using built-in jest mocking features is not very convenient in some cases, in those cases jasmine-mock-factory will more suitable.

Namely, creating mock object from class.

I cannot switch to jasmine testing framework, because jest has many other good built-in stuff (assertions, etc.)

So, I want to combine best from both worlds.

@chuanqisun
Copy link
Owner

chuanqisun commented Apr 12, 2020

I see. I agree that it's probably better for you to stick to jest.

The jasmine-mock-factory as its name suggestions, was designed to work hand-in-hand with jasmine. Jest, on the other hand, has its own opinion on how to automatically mock classes, as described in its documentation.

I noticed that the getter and setter mocks in Jest are lacking. You can track and up-vote this issue.

I feel wrapping jasmine-mock-factory around jest isn't the best solution, because

  1. It would blur the purpose of this project
  2. jasmine-mock-factory and jest both mock class methods already. It will be unclear to users which library is handling the method mocking and which library is handling the getter/setter mocking.
  3. I feel for most existing users of jest, the syntax of jasmine-mock-factory may not be what they want.

In your case, I wonder if you can write a simple test utility to just "polyfill" the getter/setter issue with jest.

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