Last active
July 25, 2023 21:02
-
-
Save erikologic/90d70f8e890c46f6699ab7b6043b6f5e to your computer and use it in GitHub Desktop.
Jest: mocking externally dependencies
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
// TestSubject.js | |
const ExternalDependency = require('./ExternalDependency') | |
module.exports = class TestSubject { | |
constructor () { | |
this.externalDependency = new ExternalDependency() | |
} | |
isExternalDependencyMocked () { | |
return this.externalDependency.isMocked | |
} | |
isSpied() { | |
this.externalDependency.isSpied() | |
} | |
} | |
// TestSubject.spec.js | |
const spy = jest.fn() | |
const mock = (value) => { | |
return () => { | |
return class ExternalDependency { | |
constructor () { | |
this.isMocked = value | |
} | |
isSpied () { | |
spy() | |
} | |
} | |
} | |
} | |
jest.resetModules() | |
jest.doMock('./ExternalDependency', mock('MyMock')) | |
const ExternalDependencyMocked = require('./ExternalDependency') | |
const TestSubjectMocked = require('./TestSubject') | |
describe ('TestSubject', () => { | |
it ('tested injected', () => { | |
const testSubject = new TestSubjectMocked() | |
expect(testSubject.isExternalDependencyMocked()).toEqual('MyMock') | |
testSubject.isSpied() | |
expect(spy).toHaveBeenCalled() | |
}) | |
}) |
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
// __mock__/ExternalDependency.js | |
module.exports = class ExternalDependency { | |
constructor () { | |
this.isMocked = true | |
} | |
} | |
// ExternalDependency.js | |
module.exports = class ExternalDependency { | |
constructor () { | |
this.isMocked = false | |
} | |
} | |
// TestSubject.js | |
const ExternalDependency = require('./ExternalDependency') | |
module.exports = class TestSubject { | |
constructor () { | |
this.externalDependency = new ExternalDependency() | |
} | |
isExternalDependencyMocked () { | |
return this.externalDependency.isMocked | |
} | |
} | |
// TestSubject.spec.js | |
const TestSubject = require('./TestSubject') | |
const mock = () => { | |
return class ExternalDependency { | |
constructor () { | |
this.isMocked = true | |
} | |
} | |
} | |
describe ('TestSubject', () => { | |
it ('will require and instantiate the real ExternalDependency', () => { | |
const testSubject = new TestSubject() | |
expect(testSubject.isExternalDependencyMocked()).toEqual(false) | |
}) | |
it ('or the mocked one here', () => { | |
jest.resetModules() | |
jest.mock('./ExternalDependency') | |
// This works fine too: | |
// jest.doMock('./ExternalDependency', mock) // you really need doMock here! | |
const TestSubjectMocked = require('./TestSubject') | |
const testSubject = new TestSubjectMocked() | |
expect(testSubject.isExternalDependencyMocked()).toEqual(true) | |
}) | |
it ('and back to the real', () => { | |
const testSubject = new TestSubject() | |
expect(testSubject.isExternalDependencyMocked()).toEqual(false) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment