Skip to content

Instantly share code, notes, and snippets.

Last active July 12, 2022 10:02
What would you like to do?
Stub window properties for Jasmine unit tests
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