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 2 commits
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');

//Mock the default export and named export 'apple'.
return Object.assign({}, mockedModule, originalModule, {
apple: 'mocked apple',
default: jest.fn(() => 'mocked fruit'),
});
});

describe('test', () => {
it('calls', () => {
const defaultExportResult = DefaultExport();
expect(defaultExportResult).toBe('mocked fruit');
expect(DefaultExport).toHaveBeenCalled();

expect(apple).toBe('mocked apple');
expect(strawberry()).toBe('strawberry');
});
});
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"
}
}