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

Added module mock example #4199

Merged
merged 3 commits into from
Aug 6, 2017
Merged

Conversation

excitement-engineer
Copy link
Contributor

I thought it would be useful to add an example outlining how to do module mocking both for a full mock and partial mock.

Partial mocks is something that I have found useful when writing tests but it is not well documented unfortunately. Hopefully this can help other people use this feature as well.

Let me know if you think this is a worthy addition.

@codecov-io
Copy link

codecov-io commented Aug 5, 2017

Codecov Report

Merging #4199 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #4199   +/-   ##
=======================================
  Coverage   60.27%   60.27%           
=======================================
  Files         198      198           
  Lines        6779     6779           
  Branches        6        6           
=======================================
  Hits         4086     4086           
  Misses       2690     2690           
  Partials        3        3

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 98c1d6a...28b4588. Read the comment docs.

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Sweet, love it! Just some minor fixes and we're good to merge it 🙂

@@ -0,0 +1,30 @@
// Copyright 2004-present Facebook. All Rights Reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the copyright header is slightly different, can you fix it? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What copyright header should I use? I got this header from the other example projects:)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, I didn't realize that. Let's keep it this way then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great!


const mockedModule = jest.genMockFromModule('../fruit');

const module = Object.assign(mockedModule, originalModule);
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this whitespace, pls. Also we're mutating mockedModule here :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, fixed it!

// Module the default export and named export 'apple'
module.default = jest.fn(() => 'mocked fruit');
module.apple = 'mocked apple';
return module;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can make it simpler. Usually what you want to do is to have original module and replace just single APIs (like default and apple here). How about:

const originalModule = require.requireActual('../fruit');

return Object.assign({}, orignalModule, {
  default: jest.fn(() => 'mocked fruit'),
  apple: 'mocked apple'
})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I also thought this was the way to do it but changing the code this way generates an error:

screen shot 2017-08-06 at 08 45 00

The default export cannot be mocked this way unfortunately:( Do you have any idea why this is happening?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure why atm (no time dig into it today), but @aaronabramov may help with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok thanks! Hopefully he knows:) If I find some time today I will also investigate further.


describe('test', () => {
it('calls', () => {
expect(DefaultExport()).toBe('mocked fruit');
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function call is easy to miss. How about extract it to a constant?

const defaultExportResult = DefaultExport(); // now it's easier to see that the function was called
expect(defaultExportResult).toBe('mocked fruit');
expect(DefaultExport).toHaveBeenCalled();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, fixed it!

@excitement-engineer
Copy link
Contributor Author

Thanks for the review @thymikee! I left some comments in response to your review.

@excitement-engineer
Copy link
Contributor Author

I was thinking that it may also be beneficial to add an example of using jest.doMock() to illustrate how to create a new mock per test case. This way the example code is more complete and highlights the various ways in which modules can be mocked. What do you think @thymikee ?

Simplified some of the test code and improved descriptions per test.
@excitement-engineer
Copy link
Contributor Author

I added a commit that illustrates how to define a custom mock per test. Hopefully it is not a problem that I extended the PR:)

@thymikee
Copy link
Collaborator

thymikee commented Aug 6, 2017

You're more than welcome to extend it! Thank you for doing this.

@cpojer cpojer merged commit e3052b4 into jestjs:master Aug 6, 2017
@cpojer
Copy link
Member

cpojer commented Aug 6, 2017

Thanks for adding an example! This is great.

@excitement-engineer
Copy link
Contributor Author

Thanks for your help and your awesome work maintaining this library guys!!! I really appreciate it

@excitement-engineer excitement-engineer deleted the mock-docs branch August 6, 2017 19:59
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Added module mock example

* Simplified the code in the partial mock example.

* Added file illustrating how to define mock behaviour per test

Simplified some of the test code and improved descriptions per test.
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants