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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/module_mock/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
16 changes: 16 additions & 0 deletions examples/module_mock/__tests__/full_mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2004-present Facebook. All Rights Reserved.

import DefaultExport, {apple, strawberry} from '../fruit';

/**
* This file illustrates a full mock of a module.
*/
jest.mock('../fruit');

describe('test', () => {
it('calls', () => {
expect(DefaultExport()).toBe(undefined);
expect(apple).toBe('apple');
expect(strawberry()).toBe(undefined);
});
});
30 changes: 30 additions & 0 deletions examples/module_mock/__tests__/partial_mock.js
Original file line number Diff line number Diff line change
@@ -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!


/**
* This file illustrates how to do a partial mock where a subset
* of a module's exports have been mocked and the rest
* keep their actual implementation.
*/
import DefaultExport, {apple, strawberry} from '../fruit';

jest.mock('../fruit', () => {
const originalModule = require.requireActual('../fruit');

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!

expect(apple).toBe('mocked apple');
expect(strawberry()).toBe('strawberry');
expect(DefaultExport).toHaveBeenCalled();
});
});
7 changes: 7 additions & 0 deletions examples/module_mock/fruit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2004-present Facebook. All Rights Reserved.

export const apple = 'apple';

export const strawberry = () => 'strawberry';

export default () => 'banana';
9 changes: 9 additions & 0 deletions examples/module_mock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"devDependencies": {
"babel-preset-env": "*",
"jest": "*"
},
"scripts": {
"test": "jest"
}
}