Skip to content

Instantly share code, notes, and snippets.

@Kamilius
Last active July 12, 2022 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kamilius/33e8af345d84756935ccc118fe1e4e33 to your computer and use it in GitHub Desktop.
Save Kamilius/33e8af345d84756935ccc118fe1e4e33 to your computer and use it in GitHub Desktop.
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