UNPKG

1.48 kBJavaScriptView Raw
1const Window = require('window');
2
3// Default jsdom config.
4// These settings must override any custom settings to make sure we can iterate
5// over the window object.
6const defaultJsdomConfig = {
7 features: {
8 FetchExternalResources: false,
9 ProcessExternalResources: false
10 }
11};
12
13// IIFE executed on import to return an array of global Node.js properties that
14// conflict with global browser properties.
15const protectedproperties = (() => Object
16 .getOwnPropertyNames(new Window(defaultJsdomConfig))
17 .filter(prop => typeof global[prop] !== 'undefined')
18)();
19
20// Sets up global browser environment
21const browserEnv = function () {
22 // Extract options from args
23 const args = Array.from(arguments);
24 const properties = args.filter(arg => Array.isArray(arg))[0];
25 const userJsdomConfig = args.filter(arg => !Array.isArray(arg))[0];
26
27 // Create window object
28 const window = new Window(Object.assign({}, userJsdomConfig, defaultJsdomConfig));
29
30 // Get all global browser properties
31 Object.getOwnPropertyNames(window)
32
33 // Remove protected properties
34 .filter(prop => protectedproperties.indexOf(prop) === -1)
35
36 // If we're only applying specific required properties remove everything else
37 .filter(prop => !(properties && properties.indexOf(prop) === -1))
38
39 // Copy what's left to the Node.js global scope
40 .forEach(prop => {
41 global[prop] = window[prop];
42});
43
44 // Return reference to original window object
45 return window;
46};
47
48module.exports = browserEnv;