Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
with-dom-overwrites
Advanced tools
Readme
with-dom-overwrites
is a utility for easily stubbing / overwriting properties of the jsdom instance (at any nesting level) in jest tests. In other words, it allows for the ability to do this:
import withDomOverwrites from 'with-dom-overwrites'
describe('some document context', () => {
it('should work', () => {
withDomOverwrites({
overwrites: {
'window.top': {},
'window.custom.prop': 'custom prop',
'document.referrer': 'foo',
'document.location.href': 'https://www.google.com',
'document.documentElement.outerHTML': `
<html data-foo="foo..">
<body>
<h1>Hello World</h1>
</body>
</html>
`,
},
run: () => {
expect(window.top).toEqual({})
expect(window.custom.prop).toBe('custom prop')
expect(document.referrer).toBe('foo')
expect(document.location.href).toBe('https://www.google.com')
expect(document.documentElement.getAttribute('data-foo')).toBe('foo..')
}
})
})
})
Instead of having to do this:
import jsdom from 'jsdom';
describe('some scenario that happens in the browser', () => {
it('should work', () => {
const url = 'https://www.google.com'
const windowTop = {}
const referrer = 'foo'
const html = `
<html data-foo="foo..">
<body>
<h1>Hello World</h1>
</body>
</html>
`
const dom = new jsdom.JSDOM(html);
const originalDocument = global.document;
Object.defineProperty(global, 'window', {
value: dom.window,
writable: true
});
Object.defineProperty(global, 'document', {
value: dom.window.document,
writable: true
});
Object.defineProperty(window.document, 'referrer', {
value: referrer,
writable: true
});
window.custom = { prop: 'custom prop' }
dom.reconfigure({ url, windowTop });
expect(window.top).toBe(windowTop)
expect(window.custom.prop).toBe('custom prop)
expect(document.referrer).toBe(referrer)
expect(document.location.href).toBe(url)
expect(document.documentElement.getAttribute('data-foo')).toBe('foo..')
global.document = originalDocument;
})
})
npm install with-dom-overwrites --save-dev
Though it is easy to access the jsdom window
/ document
object within a jest test, it's not always easy to manipulate them. For instance, trying to set the document URL by supplying a value to the document.location.href
property will not work. Rather, to change the URL, one will have to use the jsdom.reconfigure
method, which requires a custom jsdom environment in order to be used within a jest test. The same goes for setting the window.top
property, and there are one or two other scenarios that require a custom jsdom environment. This is not ideal as not only is it a bit cumbersome to setup each time, all the steps required make it something that is easily forgotten. But this is not the only issue. Even with the things that can be changed directly on the global window object, changing them is not always straight forward as a lot of things require Object.defineProperty
to be changed, which does not lead to the most concise code.
with-dom-overwrites
aims to provide a single utility withDomOverwrites({...})
function that can be used to easily and uniformly overwrite values on the jsdom instance at any nesting level, while ensuring that the original jsdom instance is restored afterwards.
FAQs
A utility for easily stubbing / overwriting properties of the jsdom instance (at any nesting level) in jest tests
The npm package with-dom-overwrites receives a total of 8 weekly downloads. As such, with-dom-overwrites popularity was classified as not popular.
We found that with-dom-overwrites demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.