Last active
July 12, 2022 10:02
-
-
Save Kamilius/33e8af345d84756935ccc118fe1e4e33 to your computer and use it in GitHub Desktop.
Stub window properties for Jasmine unit tests
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
describe('', () => { | |
let originalPluginReference; | |
let globalPluginStub; | |
beforeAll(() => { | |
// Backing up original property reference, so we could restore it | |
// after all tests being performed | |
originalPluginReference = window['GlobalPlugin']; | |
}); | |
// We're initializing spy for each test, to avoid each following unit test | |
// being dependent on previous one as this makes tests flaky | |
beforeEach(() => { | |
// Use this property to specify stubbed methods return | |
// values, as you usually do for Spy objects | |
globalPluginStub = jasmine.createSpyObj('GlobalPlugin', ['someTestableMethod']); | |
Object.defineProperty(window, 'GlobalPlugin', { | |
value: globalPluginStub | |
}); | |
}); | |
afterAll(() => { | |
// Restoring original property value after performing all unit tests | |
Object.defineProperty(window, 'GlobalPlugin', { | |
value: originalPluginReference | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment