': 'application/json' }) }).to.throw()
635 | expect(function (): void { new Headers({ 'Accept:': 'application/json' }) }).to.throw()
636 | })
637 |
638 | it('should not init an invalid header', () => {
639 | expect(function (): void { new Headers({ Héy: 'ok' }) }).to.throw()
640 | })
641 |
642 | it('should not set an invalid header', () => {
643 | const headers = new Headers()
644 | expect(function (): void { headers.set('Héy', 'ok') }).to.throw()
645 | })
646 |
647 | it('should not append an invalid header', () => {
648 | const headers = new Headers()
649 | expect(function (): void { headers.append('Héy', 'ok') }).to.throw()
650 | })
651 |
652 | it('should not get an invalid header', () => {
653 | const headers = new Headers()
654 | expect(function (): void { headers.get('Héy') }).to.throw()
655 | })
656 |
657 | it('should copy headers', () => {
658 | const original = new Headers()
659 | original.append('Accept', 'application/json')
660 | original.append('Accept', 'text/plain')
661 | original.append('Content-Type', 'text/html')
662 |
663 | const headers = new Headers(original)
664 | expect(headers.get('Accept')).to.equal('application/json, text/plain')
665 | expect(headers.get('Content-type')).to.equal('text/html')
666 | })
667 |
668 | it('should detect if a header exists', () => {
669 | const headers = new Headers({ Accept: 'application/json' })
670 | expect(headers.has('Content-Type')).to.equal(false)
671 |
672 | headers.append('Content-Type', 'application/json')
673 | expect(headers.has('Content-Type')).to.equal(true)
674 | })
675 |
676 | it('should have headers that are set', () => {
677 | const headers = new Headers()
678 | headers.set('Content-Type', 'application/json')
679 | expect(headers.has('Content-Type')).to.equal(true)
680 | })
681 |
682 | it('should delete header', () => {
683 | const headers = new Headers({ Accept: 'application/json' })
684 | expect(headers.has('Accept')).to.equal(true)
685 |
686 | headers.delete('Accept')
687 | expect(headers.has('Accept')).to.equal(false)
688 | expect(headers.get('Content-Type')).to.equal(null)
689 | })
690 |
691 | it('should convert field name to string on set and get', () => {
692 | const headers = new Headers()
693 | headers.set(1 as any, 'application/json')
694 | expect(headers.has('1')).to.equal(true)
695 | expect(headers.get(1 as any)).to.equal('application/json')
696 | })
697 |
698 | it('should convert field value to string on set and get', () => {
699 | const headers = new Headers()
700 | headers.set('Content-Type', 1 as any)
701 | headers.set('X-CSRF-Token', undefined as any)
702 | expect(headers.get('Content-Type')).to.equal('1')
703 | expect(headers.get('X-CSRF-Token')).to.equal('undefined')
704 | })
705 |
706 | it('should be iterable with forEach', () => {
707 | const headers = new Headers()
708 | headers.append('Accept', 'application/json')
709 | headers.append('Accept', 'text/plain')
710 | headers.append('Content-Type', 'text/html')
711 |
712 | const results: {value: string, key: string, object: object}[] = []
713 | headers.forEach(function (value, key, object) {
714 | results.push({ value: value, key: key, object: object })
715 | })
716 |
717 | expect(results.length).to.equal(2)
718 | expect({ key: 'accept', value: 'application/json, text/plain', object: headers }).to.deep.equal(results[0])
719 | expect({ key: 'content-type', value: 'text/html', object: headers }).to.deep.equal(results[1])
720 | })
721 |
722 | it('should accept second thisArg argument for forEach', () => {
723 | const headers = new Headers({ Accept: 'application/json' })
724 | const thisArg = {}
725 | headers.forEach(function (this: void): void {
726 | expect(this).to.equal(thisArg)
727 | }, thisArg)
728 | })
729 |
730 | it('should be iterable with keys', () => {
731 | const headers = new Headers({
732 | Accept: 'application/json, text/plain',
733 | 'Content-Type': 'text/html'
734 | })
735 |
736 | const iterator = headers.keys()
737 | expect({ done: false, value: 'accept' }).to.deep.equal(iterator.next())
738 | expect({ done: false, value: 'content-type' }).to.deep.equal(iterator.next())
739 | expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
740 | })
741 |
742 | it('should be iterable with values', () => {
743 | const headers = new Headers({
744 | Accept: 'application/json, text/plain',
745 | 'Content-Type': 'text/html'
746 | })
747 |
748 | const iterator = headers.values()
749 | expect({ done: false, value: 'application/json, text/plain' }).to.deep.equal(iterator.next())
750 | expect({ done: false, value: 'text/html' }).to.deep.equal(iterator.next())
751 | expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
752 | })
753 |
754 | it('should be iterable with entries', () => {
755 | const headers = new Headers({
756 | Accept: 'application/json, text/plain',
757 | 'Content-Type': 'text/html'
758 | })
759 |
760 | const iterator = headers.entries()
761 | expect({ done: false, value: ['accept', 'application/json, text/plain'] }).to.deep.equal(iterator.next())
762 | expect({ done: false, value: ['content-type', 'text/html'] }).to.deep.equal(iterator.next())
763 | expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
764 | })
765 | })
766 | }
767 |
768 | if (typeof module === 'object' && module.exports) {
769 | module.exports = addFetchSuite;
770 | }
771 |
--------------------------------------------------------------------------------
/test/fetch-api/browser/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/fetch-api/browser/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | . test/setup/server.sh
4 |
5 | npx mocha-headless-chrome -f http://127.0.0.1:8000/$(dirname $0)/index.html?globals=on -a no-sandbox -a disable-setuid-sandbox
6 |
--------------------------------------------------------------------------------
/test/fetch-api/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
25 |
26 |
27 |
28 |
29 |
Fetch API Test Suites
30 |
31 |
32 |
Implementations
33 |
34 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/test/fetch-api/node-fetch/index.js:
--------------------------------------------------------------------------------
1 | require('../../setup/node.env')
2 | require('../../../dist/node-polyfill')
3 |
4 | const addFetchSuite = require('../api.spec')
5 | const { addPolyfillSuite } = require('../../module-system/module.spec')
6 |
7 | describe('Node:Fetch:node-fetch', () => {
8 | addFetchSuite()
9 | addPolyfillSuite({ fetch })
10 | })
11 |
--------------------------------------------------------------------------------
/test/fetch-api/node-fetch/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | . test/setup/server.sh
4 |
5 | npx nyc mocha $(dirname $0)/index.js
6 |
--------------------------------------------------------------------------------
/test/fetch-api/node/index.js:
--------------------------------------------------------------------------------
1 | if (global.fetch) {
2 | const addFetchSuite = require('../api.spec')
3 |
4 | describe('Node:Fetch:Native', () => {
5 | addFetchSuite()
6 | })
7 | } else {
8 | console.log('Skipping tests as this Node version does not have a native fetch.')
9 | }
10 |
--------------------------------------------------------------------------------
/test/fetch-api/node/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | . test/setup/server.sh
4 |
5 | npx nyc mocha $(dirname $0)/index.js
6 |
--------------------------------------------------------------------------------
/test/fetch-api/service-worker/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
Please check console!
12 |
13 |
14 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/test/fetch-api/service-worker/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | . test/setup/server.sh
4 |
5 | npx webpack --config $(dirname "$0")/webpack.config.js &&
6 | npx mocha-headless-chrome -f http://127.0.0.1:8000/$(dirname $0)/index.html -a no-sandbox -a disable-setuid-sandbox
7 |
8 |
--------------------------------------------------------------------------------
/test/fetch-api/service-worker/sw.js:
--------------------------------------------------------------------------------
1 | /* eslint-disable no-undef */
2 | import fetch, { Request, Response, Headers } from '../../..'
3 |
4 | const logChannel = new BroadcastChannel('sw-logger')
5 |
6 | // Redirect all logs to top window
7 | console.log = (...args) => logChannel.postMessage(args)
8 |
9 | importScripts('../../../node_modules/mocha/mocha.js')
10 | importScripts('../../../node_modules/chai/chai.js')
11 | importScripts('./sw.reporter.js')
12 |
13 | importScripts('../api.spec.js')
14 | importScripts('../../module-system/module.spec.js')
15 |
16 | globalThis.expect = chai.expect
17 | globalThis.fetch = fetch
18 | globalThis.Request = Request
19 | globalThis.Response = Response
20 | globalThis.Headers = Headers
21 |
22 | mocha.setup({
23 | ui: 'bdd',
24 | reporter: SWReporter
25 | })
26 |
27 | describe('Browser:Fetch:ServiceWorker', () => {
28 | addFetchSuite()
29 | addNativeSuite({ fetch })
30 | })
31 |
32 | mocha.run()
33 |
--------------------------------------------------------------------------------
/test/fetch-api/service-worker/sw.reporter.js:
--------------------------------------------------------------------------------
1 | /**
2 | * A SW Reporter created to be compatible with mocha-headless-chrome's repoter
3 | * See: https://github.com/direct-adv-interfaces/mocha-headless-chrome/blob/273d9b8bc7445ea1196b10ad0eaf0a8bce6cbd5f/lib/runner.js#L68
4 | */
5 |
6 | const { Spec } = Mocha.reporters
7 | const {
8 | EVENT_RUN_END,
9 | EVENT_TEST_FAIL,
10 | EVENT_TEST_PASS,
11 | EVENT_TEST_PENDING
12 | } = Mocha.Runner.constants
13 |
14 | function SWReporter (runner, options) {
15 | Spec.call(this, runner, options)
16 |
17 | const all = []
18 | const passes = []
19 | const failures = []
20 | const pending = []
21 |
22 | const error = (err) => {
23 | if (!err) return {}
24 |
25 | const res = {}
26 | Object.getOwnPropertyNames(err).forEach((key) => (res[key] = err[key]))
27 | return res
28 | }
29 |
30 | const clean = (test) => ({
31 | title: test.title,
32 | fullTitle: test.fullTitle(),
33 | duration: test.duration,
34 | err: error(test.err)
35 | })
36 |
37 | const getResult = (stats) => ({
38 | result: {
39 | stats: {
40 | tests: all.length,
41 | passes: passes.length,
42 | pending: pending.length,
43 | failures: failures.length,
44 | start: stats.start.toISOString(),
45 | end: stats.end.toISOString(),
46 | duration: stats.duration
47 | },
48 | tests: all.map(clean),
49 | pending: pending.map(clean),
50 | failures: failures.map(clean),
51 | passes: passes.map(clean)
52 | }
53 | })
54 |
55 | runner
56 | .on(EVENT_TEST_PASS, (test) => {
57 | passes.push(test)
58 | all.push(test)
59 | })
60 | .on(EVENT_TEST_FAIL, (test) => {
61 | failures.push(test)
62 | all.push(test)
63 | })
64 | .on(EVENT_TEST_PENDING, (test) => {
65 | pending.push(test)
66 | all.push(test)
67 | })
68 | .once(EVENT_RUN_END, () => {
69 | const result = getResult(runner.stats)
70 | const channel = new BroadcastChannel('sw-result')
71 | channel.postMessage(JSON.stringify(result))
72 | })
73 | }
74 |
75 | Mocha.utils.inherits(SWReporter, Spec)
76 |
--------------------------------------------------------------------------------
/test/fetch-api/service-worker/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = {
4 | target: 'webworker',
5 | mode: 'none',
6 | entry: path.join(__dirname, 'sw.js'),
7 | output: {
8 | path: __dirname,
9 | filename: 'sw.bundle.js'
10 | },
11 | stats: 'none'
12 | }
13 |
--------------------------------------------------------------------------------
/test/fetch-api/whatwg/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/fetch-api/whatwg/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | . test/setup/server.sh
4 |
5 | npx mocha-headless-chrome -f http://127.0.0.1:8000/$(dirname $0)/index.html?globals=off -a no-sandbox -a disable-setuid-sandbox
6 |
7 |
--------------------------------------------------------------------------------
/test/module-system/module.spec.js:
--------------------------------------------------------------------------------
1 | /**
2 | * This file has all the tests needed to ensure cross-fetch is properly and equally
3 | * imported/required in webpack bundle for node and browser environments.
4 | */
5 |
6 | function addModuleSuite ({ fetch, Request, Response, Headers }) {
7 | it('should have the fetch function exposed', () => {
8 | expect(fetch).to.be.a('function')
9 | })
10 |
11 | it('should have the Request constructor exposed', () => {
12 | expect(Request).to.be.a('function')
13 | })
14 |
15 | it('should have the Response constructor exposed', () => {
16 | expect(Response).to.be.a('function')
17 | })
18 |
19 | it('should have Headers constructor exposed', () => {
20 | expect(Headers).to.be.a('function')
21 | })
22 | }
23 |
24 | function addPolyfillSuite ({ fetch }) {
25 | it('should use the polyfill fetch function', () => {
26 | expect(fetch.polyfill).to.equal(true)
27 | expect(fetch.ponyfill).to.equal(undefined)
28 | })
29 | }
30 |
31 | function addPonyfillSuite ({ fetch, defaultExport }) {
32 | it('should use the ponyfill fetch function', () => {
33 | expect(fetch.polyfill).to.equal(undefined)
34 | expect(fetch.ponyfill).to.equal(true)
35 | })
36 |
37 | it('should import the fetch function as the default', () => {
38 | expect(defaultExport).to.equal(fetch)
39 | })
40 | }
41 |
42 | function addNativeSuite ({ fetch }) {
43 | it('should use the native fetch function', () => {
44 | expect(fetch.polyfill).to.equal(undefined)
45 | expect(fetch.ponyfill).to.equal(undefined)
46 | })
47 | }
48 |
49 | // Since this test suite needs to run on different environments,
50 | // we used a simplified UMD pattern here.
51 | if (typeof module === 'object' && module.exports) {
52 | module.exports = {
53 | addModuleSuite,
54 | addNativeSuite,
55 | addPolyfillSuite,
56 | addPonyfillSuite
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/test/module-system/node.cjs/polyfill.js:
--------------------------------------------------------------------------------
1 | // Delete node's native fetch api to force the polyfill installation for testing purposes
2 | delete global.fetch
3 | delete global.Request
4 | delete global.Response
5 | delete global.Headers
6 |
7 | require('../../../polyfill')
8 |
9 | const { addModuleSuite, addPolyfillSuite } = require('../module.spec')
10 |
11 | describe('Node:Polyfill:Require:Webpack', () => {
12 | addModuleSuite({ fetch, Request, Response, Headers })
13 | addPolyfillSuite({ fetch })
14 | })
15 |
--------------------------------------------------------------------------------
/test/module-system/node.cjs/ponyfill.js:
--------------------------------------------------------------------------------
1 | const defaultExport = require('../../..')
2 | const namedExports = require('../../..')
3 | const { addModuleSuite, addPonyfillSuite } = require('../module.spec')
4 |
5 | describe('Node:Ponyfill:Require:Webpack', () => {
6 | addModuleSuite(namedExports)
7 | addPonyfillSuite({ ...namedExports, defaultExport })
8 | })
9 |
--------------------------------------------------------------------------------
/test/module-system/node.cjs/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx webpack --config $(dirname "$0")/webpack.config.js &&
3 | npx mocha $(dirname "$0")/*.bundle.js
4 |
--------------------------------------------------------------------------------
/test/module-system/node.cjs/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = ['polyfill', 'ponyfill'].map(method => ({
4 | target: 'node',
5 | mode: 'none',
6 | entry: [path.join(__dirname, '..', '..', 'setup', 'node.env.js'), path.join(__dirname, `${method}.js`)],
7 | output: {
8 | path: __dirname,
9 | filename: `${method}.bundle.js`
10 | },
11 | stats: 'none'
12 | }))
13 |
--------------------------------------------------------------------------------
/test/module-system/node.esm/polyfill.js:
--------------------------------------------------------------------------------
1 | import '../../../polyfill'
2 | import { addModuleSuite, addPolyfillSuite } from '../module.spec'
3 |
4 | describe('Node:Polyfill:Import:Webpack', () => {
5 | addModuleSuite({ fetch, Request, Response, Headers })
6 | addPolyfillSuite({ fetch })
7 | })
8 |
--------------------------------------------------------------------------------
/test/module-system/node.esm/ponyfill.js:
--------------------------------------------------------------------------------
1 | import defaultExport, * as namedExports from '../../..'
2 | import { addModuleSuite, addPonyfillSuite } from '../module.spec'
3 |
4 | describe('Node:Ponyfill:Import:Webpack', () => {
5 | addModuleSuite(namedExports)
6 | addPonyfillSuite({ ...namedExports, defaultExport })
7 | })
8 |
--------------------------------------------------------------------------------
/test/module-system/node.esm/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx webpack --config $(dirname "$0")/webpack.config.js &&
3 | npx mocha $(dirname "$0")/*.bundle.js
4 |
--------------------------------------------------------------------------------
/test/module-system/node.esm/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = ['polyfill', 'ponyfill'].map(method => ({
4 | target: 'node',
5 | mode: 'none',
6 | entry: [path.join(__dirname, '..', '..', 'setup', 'node.env.js'), path.join(__dirname, `${method}.js`)],
7 | output: {
8 | path: __dirname,
9 | filename: `${method}.bundle.js`
10 | },
11 | stats: 'none'
12 | }))
13 |
--------------------------------------------------------------------------------
/test/module-system/react-native/index.js:
--------------------------------------------------------------------------------
1 | describe('ReactNative', () => {
2 | it('re-exports the global functions', () => {
3 | const globalFetch = global.fetch = function globalFetch () {}
4 | const globalHeaders = global.Headers = function globalHeaders () {}
5 | const globalRequest = global.Request = function globalRequest () {}
6 | const globalResponse = global.Response = function globalResponse () {}
7 |
8 | const { fetch, Request, Response, Headers } = require('../../../dist/react-native-ponyfill')
9 |
10 | expect(fetch).to.equal(globalFetch)
11 | expect(Headers).to.equal(globalHeaders)
12 | expect(Request).to.equal(globalRequest)
13 | expect(Response).to.equal(globalResponse)
14 |
15 | delete global.fetch
16 | delete global.Headers
17 | delete global.Request
18 | delete global.Response
19 | })
20 |
21 | it('does not touch the global functions when polyfilling', () => {
22 | const globalFetch = global.fetch = function globalFetch () {}
23 | const globalHeaders = global.Headers = function globalHeaders () {}
24 | const globalRequest = global.Request = function globalRequest () {}
25 | const globalResponse = global.Response = function globalResponse () {}
26 |
27 | require('../../../dist/react-native-polyfill')
28 |
29 | expect(fetch).to.equal(globalFetch)
30 | expect(Headers).to.equal(globalHeaders)
31 | expect(Request).to.equal(globalRequest)
32 | expect(Response).to.equal(globalResponse)
33 |
34 | delete global.fetch
35 | delete global.Headers
36 | delete global.Request
37 | delete global.Response
38 | })
39 | })
40 |
--------------------------------------------------------------------------------
/test/module-system/react-native/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx mocha $(dirname "$0")/index.js
3 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/polyfill.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/polyfill.js:
--------------------------------------------------------------------------------
1 | require('../../../polyfill')
2 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/ponyfill.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/ponyfill.js:
--------------------------------------------------------------------------------
1 | window.defaultExport = require('../../..')
2 | window.namedExports = require('../../..')
3 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx webpack --config $(dirname "$0")/webpack.config.js &&
3 | npx mocha-headless-chrome -f $(dirname "$0")/polyfill.html?globals=off -a no-sandbox -a disable-setuid-sandbox &&
4 | npx mocha-headless-chrome -f $(dirname "$0")/polyfill.html?globals=on -a no-sandbox -a disable-setuid-sandbox &&
5 | npx mocha-headless-chrome -f $(dirname "$0")/ponyfill.html?globals=off -a no-sandbox -a disable-setuid-sandbox &&
6 | npx mocha-headless-chrome -f $(dirname "$0")/ponyfill.html?globals=on -a no-sandbox -a disable-setuid-sandbox
7 |
--------------------------------------------------------------------------------
/test/module-system/web.cjs/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = ['polyfill', 'ponyfill'].map(method => ({
4 | target: 'web',
5 | mode: 'none',
6 | entry: path.join(__dirname, `${method}.js`),
7 | output: {
8 | path: __dirname,
9 | filename: `${method}.bundle.js`
10 | },
11 | stats: 'none'
12 | }))
13 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/polyfill.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/polyfill.js:
--------------------------------------------------------------------------------
1 | import '../../../polyfill'
2 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/ponyfill.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/ponyfill.js:
--------------------------------------------------------------------------------
1 | import defaultExport, * as namedExports from '../../..'
2 |
3 | window.defaultExport = defaultExport
4 | window.namedExports = namedExports
5 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx webpack --config $(dirname "$0")/webpack.config.js &&
3 | npx mocha-headless-chrome -f $(dirname "$0")/polyfill.html?globals=off -a no-sandbox -a disable-setuid-sandbox &&
4 | npx mocha-headless-chrome -f $(dirname "$0")/polyfill.html?globals=on -a no-sandbox -a disable-setuid-sandbox &&
5 | npx mocha-headless-chrome -f $(dirname "$0")/ponyfill.html?globals=off -a no-sandbox -a disable-setuid-sandbox &&
6 | npx mocha-headless-chrome -f $(dirname "$0")/ponyfill.html?globals=on -a no-sandbox -a disable-setuid-sandbox
7 |
8 |
--------------------------------------------------------------------------------
/test/module-system/web.esm/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path')
2 |
3 | module.exports = ['polyfill', 'ponyfill'].map(method => ({
4 | target: 'web',
5 | mode: 'none',
6 | entry: path.join(__dirname, `${method}.js`),
7 | output: {
8 | path: __dirname,
9 | filename: `${method}.bundle.js`
10 | },
11 | stats: 'none'
12 | }))
13 |
--------------------------------------------------------------------------------
/test/setup/browser.env.js:
--------------------------------------------------------------------------------
1 | // Enable mocha's bdd style
2 | mocha.setup('bdd')
3 |
4 | // Add chai's expect to the global scope
5 | window.expect = chai.expect
6 | window.assert = chai.assert
7 |
8 | if (/globals=off/.test(location.search)) {
9 | // Delete native fetch api to force the polyfill installation for test purposes
10 | delete window.fetch
11 | delete window.Request
12 | delete window.Response
13 | delete window.Headers
14 | }
15 |
--------------------------------------------------------------------------------
/test/setup/node.env.js:
--------------------------------------------------------------------------------
1 | // Delete node's native fetch api to force the polyfill installation for testing purposes
2 | delete global.fetch
3 | delete global.Request
4 | delete global.Response
5 | delete global.Headers
6 |
--------------------------------------------------------------------------------
/test/setup/server.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | start_server () {
4 | ./bin/server &
5 | pid=$!
6 | }
7 |
8 | kill_server() {
9 | kill $pid
10 | wait $pid 2> /dev/null
11 | }
12 |
13 | is_server_ready() {
14 | lsof -i :8000 > /dev/null
15 | }
16 |
17 | wait_server() {
18 | while ! is_server_ready; do sleep 0.1; done
19 | }
20 |
21 | trap kill_server EXIT
22 |
23 | start_server
24 |
25 | wait_server
26 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | /* Visit https://aka.ms/tsconfig.json to read more about this file */
4 |
5 | /* Basic Options */
6 | // "incremental": true, /* Enable incremental compilation */
7 | "target": "ES6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8 | "module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9 | "lib": ["es2015"], /* Specify library files to be included in the compilation. */
10 | // "allowJs": true, /* Allow javascript files to be compiled. */
11 | // "checkJs": true, /* Report errors in .js files. */
12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13 | // "declaration": true, /* Generates corresponding '.d.ts' file. */
14 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15 | // "sourceMap": true, /* Generates corresponding '.map' file. */
16 | // "outFile": "./", /* Concatenate and emit output to single file. */
17 | // "outDir": "./", /* Redirect output structure to the directory. */
18 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19 | // "composite": true, /* Enable project compilation */
20 | // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21 | // "removeComments": true, /* Do not emit comments to output. */
22 | // "noEmit": true, /* Do not emit outputs. */
23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */
24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26 |
27 | /* Strict Type-Checking Options */
28 | "strict": true, /* Enable all strict type-checking options. */
29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30 | // "strictNullChecks": true, /* Enable strict null checks. */
31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36 |
37 | /* Additional Checks */
38 | // "noUnusedLocals": true, /* Report errors on unused locals. */
39 | // "noUnusedParameters": true, /* Report errors on unused parameters. */
40 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42 | // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
43 | // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
44 |
45 | /* Module Resolution Options */
46 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
47 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
48 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
49 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
50 | "typeRoots": ["./node_modules/@types"], /* List of folders to include type definitions from. */
51 | "types": ["mocha", "chai", "node"], /* Type declaration files to be included in compilation. */
52 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
53 | "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
54 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
55 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
56 |
57 | /* Source Map Options */
58 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
59 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
60 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
61 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
62 |
63 | /* Experimental Options */
64 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
65 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
66 |
67 | /* Advanced Options */
68 | "skipLibCheck": true, /* Skip type checking of declaration files. */
69 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
70 | },
71 | "files": [
72 | "./index.d.ts",
73 | "test/fetch-api/api.spec.ts"
74 | ]
75 | }
76 |
--------------------------------------------------------------------------------