Created
March 31, 2017 20:06
-
-
Save elderfo/03317d961e624e9ec34bb44deb5356c2 to your computer and use it in GitHub Desktop.
Jest - Class Mocking
This file contains hidden or 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
import SampleClass from './SampleClass'; | |
export default function getSomethingFromSampleClass() { | |
const sampleClass = new SampleClass(); | |
return sampleClass.getSomething(); | |
} |
This file contains hidden or 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
import getSomethingFromSampleClass from './getSomethingFromSampleClass'; | |
import SampleClass from './SampleClass'; | |
jest.mock('./SampleClass'); | |
test('getSomethingFromSampleClass should return expected value', () => { | |
const expected = 'other thing'; | |
SampleClass.prototype.getSomething.mockImplementation(() => expected); | |
const actual = getSomethingFromSampleClass(); | |
expect(actual).toEqual(expected); | |
}); |
This file contains hidden or 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
export default class SampleClass { | |
getSomething() { | |
return 'something'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if getSomething() method also in getSomethingFromSampleClass.js class?