Browse Source

Add fetch to window

Fixes #41
pull/42/head
Tim Daubenschütz 4 years ago
parent
commit
36aa45a2ec
  1. 13
      package-lock.json
  2. 1
      package.json
  3. 5
      src/index.js
  4. 1
      test/fixtures/expectedProperties.json
  5. 5
      test/unit.js

13
package-lock.json

@ -1898,6 +1898,14 @@
"capture-stack-trace": "^1.0.0" "capture-stack-trace": "^1.0.0"
} }
}, },
"cross-fetch": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.6.tgz",
"integrity": "sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ==",
"requires": {
"node-fetch": "2.6.1"
}
},
"cross-spawn": { "cross-spawn": {
"version": "5.1.0", "version": "5.1.0",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",
@ -5277,6 +5285,11 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true "dev": true
}, },
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
},
"normalize-package-data": { "normalize-package-data": {
"version": "2.4.0", "version": "2.4.0",
"resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz",

1
package.json

@ -35,6 +35,7 @@
}, },
"homepage": "https://github.com/lukechilds/window#readme", "homepage": "https://github.com/lukechilds/window#readme",
"dependencies": { "dependencies": {
"cross-fetch": "3.0.6",
"jsdom": "13.2.0" "jsdom": "13.2.0"
}, },
"devDependencies": { "devDependencies": {

5
src/index.js

@ -1,4 +1,5 @@
const { JSDOM, ResourceLoader } = require('jsdom'); const { JSDOM, ResourceLoader } = require('jsdom');
const fetch = require('cross-fetch');
// Class to return a window instance. // Class to return a window instance.
// Accepts a jsdom config object. // Accepts a jsdom config object.
@ -10,8 +11,10 @@ module.exports = class Window {
strictSSL, strictSSL,
userAgent userAgent
}); });
return (new JSDOM('', Object.assign(jsdomConfig, { const win = (new JSDOM('', Object.assign(jsdomConfig, {
resources resources
}))).window; }))).window;
win.fetch = fetch;
return win;
} }
}; };

1
test/fixtures/expectedProperties.json

@ -216,6 +216,7 @@
"document", "document",
"external", "external",
"focus", "focus",
"fetch",
"frameElement", "frameElement",
"frames", "frames",
"getComputedStyle", "getComputedStyle",

5
test/unit.js

@ -21,3 +21,8 @@ test('properties haven\'t changed', t => {
t.deepEqual(expectedProperties.sort(), properties.sort()); t.deepEqual(expectedProperties.sort(), properties.sort());
}); });
test('if fetch is available', t => {
const window = new Window();
t.true(typeof window.fetch === 'function');
});

Loading…
Cancel
Save