-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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.
- Loading branch information
1 parent
98c1d6a
commit e3052b4
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// 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'); | ||
|
||
it('does a full mock', () => { | ||
expect(defaultExport()).toBe(undefined); | ||
expect(apple).toBe('apple'); | ||
expect(strawberry()).toBe(undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
/** | ||
* This file illustrates how to define a custom mock per test. | ||
* | ||
* The file contains two test cases: | ||
* - One where the fruit module is mocked. | ||
* - One where the fruit module is not mocked. | ||
*/ | ||
describe('define mock per test', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
it('uses mocked module', () => { | ||
jest.doMock('../fruit', () => ({ | ||
apple: 'mocked apple', | ||
default: jest.fn(() => 'mocked fruit'), | ||
strawberry: jest.fn(() => 'mocked strawberry'), | ||
})); | ||
const {apple, strawberry, default: defaultExport} = require('../fruit'); | ||
|
||
const defaultExportResult = defaultExport(); | ||
expect(defaultExportResult).toBe('mocked fruit'); | ||
expect(defaultExport).toHaveBeenCalled(); | ||
|
||
expect(apple).toBe('mocked apple'); | ||
expect(strawberry()).toBe('mocked strawberry'); | ||
}); | ||
|
||
it('uses actual module', () => { | ||
jest.dontMock('../fruit'); | ||
const {apple, strawberry, default: defaultExport} = require('../fruit'); | ||
|
||
const defaultExportResult = defaultExport(); | ||
expect(defaultExportResult).toBe('banana'); | ||
|
||
expect(apple).toBe('apple'); | ||
expect(strawberry()).toBe('strawberry'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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'); | ||
|
||
//Mock the default export and named export 'apple'. | ||
return Object.assign({}, mockedModule, originalModule, { | ||
apple: 'mocked apple', | ||
default: jest.fn(() => 'mocked fruit'), | ||
}); | ||
}); | ||
|
||
it('does a partial mock', () => { | ||
const defaultExportResult = defaultExport(); | ||
expect(defaultExportResult).toBe('mocked fruit'); | ||
expect(defaultExport).toHaveBeenCalled(); | ||
|
||
expect(apple).toBe('mocked apple'); | ||
expect(strawberry()).toBe('strawberry'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"devDependencies": { | ||
"babel-preset-env": "*", | ||
"jest": "*" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
} | ||
} |