-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
/** | ||
* 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this whitespace, pls. Also we're mutating mockedModule here :( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}); | ||
}); |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"devDependencies": { | ||
"babel-preset-env": "*", | ||
"jest": "*" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
} | ||
} |
There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
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:)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!