diff --git a/packages/webapi/package.json b/packages/webapi/package.json index 63b45685a..7846d7775 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -54,10 +54,12 @@ "@rollup/plugin-inject": "^4.0.4", "@rollup/plugin-node-resolve": "^13.1.3", "@rollup/plugin-typescript": "^8.3.1", + "@types/chai": "^4.3.0", "@types/mocha": "^9.1.0", "@types/node": "^14.18.12", "@ungap/structured-clone": "^0.3.4", "abort-controller": "^3.0.0", + "chai": "^4.3.6", "event-target-shim": "^6.0.2", "fetch-blob": "^3.1.5", "formdata-polyfill": "^4.0.10", diff --git a/packages/webapi/run/test.setup.js b/packages/webapi/run/test.setup.js deleted file mode 100644 index 6998a43bd..000000000 --- a/packages/webapi/run/test.setup.js +++ /dev/null @@ -1,29 +0,0 @@ -import { fileURLToPath } from 'url' - -export { strict as assert } from 'assert' - -export const pathFrom = (...args) => - fileURLToPath(args.reduce((url, bit) => new URL(bit, url), new URL('file:'))) - -export const test = async (setup) => { - console.log(`Testing Node ${process.version}:`) - console.log('') - - for (const test of setup()) { - try { - console.log(`- ${test.name}`) - - await test.test() - } catch (error) { - console.error(error) - - process.exit(1) - } - } - - console.log('') - console.log('Pass!') - console.log('') - - process.exit(0) -} diff --git a/packages/webapi/test/base64.js b/packages/webapi/test/base64.js index 695641805..8881784dc 100644 --- a/packages/webapi/test/base64.js +++ b/packages/webapi/test/base64.js @@ -1,48 +1,29 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Supports Base64 Methods', - test() { - const target = {} +describe('Base64', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal('atob' in target, true) - assert.equal('btoa' in target, true) - assert.equal(typeof target['atob'], 'function') - assert.equal(typeof target['btoa'], 'function') - }, - }, - { - name: 'Supports atob(data)', - test() { - const target = {} + it('Supports Base64 Methods', () => { + expect(target).to.have.property('atob').that.is.a('function') + expect(target).to.have.property('btoa').that.is.a('function') + }) - polyfill(target) + it('Supports atob(data)', () => { + const a = 'SGVsbG8sIHdvcmxk' + const b = target.atob(a) - const a = 'SGVsbG8sIHdvcmxk' - const b = target.atob(a) + expect(a).to.equal('SGVsbG8sIHdvcmxk') + expect(b).to.equal('Hello, world') + }) - assert.equal(a, 'SGVsbG8sIHdvcmxk') - assert.equal(b, 'Hello, world') - }, - }, - { - name: 'Supports btoa(data)', - test() { - const target = {} + it('Supports btoa(data)', () => { + const b = 'Hello, world' + const a = target.btoa(b) - polyfill(target) - - const b = 'Hello, world' - const a = target.btoa(b) - - assert.equal(a, 'SGVsbG8sIHdvcmxk') - assert.equal(b, 'Hello, world') - }, - }, - ] + expect(a).to.equal('SGVsbG8sIHdvcmxk') + expect(b).to.equal('Hello, world') + }) }) diff --git a/packages/webapi/test/basic.js b/packages/webapi/test/basic.js index 2ea4a613b..5ee48d033 100644 --- a/packages/webapi/test/basic.js +++ b/packages/webapi/test/basic.js @@ -1,214 +1,185 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - polyfill(globalThis) +describe('Basic', () => { + before(() => polyfill(globalThis)) - return [ - { - name: 'Globals exist', - test() { - const webAPIs = [ - 'AbortController', - 'AbortSignal', - 'Blob', - 'ByteLengthQueuingStrategy', - 'CSSStyleSheet', - 'CountQueuingStrategy', - 'CustomElementRegistry', - 'CustomEvent', - 'DOMException', - 'Document', - 'DocumentFragment', - 'Element', - 'Event', - 'EventTarget', - 'File', - 'FormData', - 'HTMLDocument', - 'HTMLElement', - 'HTMLDivElement', - 'HTMLHeadElement', - 'HTMLHtmlElement', - 'HTMLImageElement', - 'HTMLStyleElement', - 'HTMLTemplateElement', - 'HTMLUnknownElement', - 'Headers', - 'IntersectionObserver', - 'Image', - 'MediaQueryList', - 'MutationObserver', - 'Node', - 'ReadableByteStreamController', - 'ReadableStream', - 'ReadableStreamBYOBReader', - 'ReadableStreamBYOBRequest', - 'ReadableStreamDefaultController', - 'ReadableStreamDefaultReader', - 'Request', - 'Response', - 'ShadowRoot', - 'StyleSheet', - 'TransformStream', - 'WritableStream', - 'WritableStreamDefaultController', - 'WritableStreamDefaultWriter', - 'Window', - 'cancelAnimationFrame', - 'cancelIdleCallback', - 'clearTimeout', - 'fetch', - 'requestAnimationFrame', - 'requestIdleCallback', - 'setTimeout', - ] + it('Globals exist', () => { + const webAPIs = [ + 'AbortController', + 'AbortSignal', + 'Blob', + 'ByteLengthQueuingStrategy', + 'CSSStyleSheet', + 'CountQueuingStrategy', + 'CustomElementRegistry', + 'CustomEvent', + 'DOMException', + 'Document', + 'DocumentFragment', + 'Element', + 'Event', + 'EventTarget', + 'File', + 'FormData', + 'HTMLDocument', + 'HTMLElement', + 'HTMLDivElement', + 'HTMLHeadElement', + 'HTMLHtmlElement', + 'HTMLImageElement', + 'HTMLStyleElement', + 'HTMLTemplateElement', + 'HTMLUnknownElement', + 'Headers', + 'IntersectionObserver', + 'Image', + 'MediaQueryList', + 'MutationObserver', + 'Node', + 'ReadableByteStreamController', + 'ReadableStream', + 'ReadableStreamBYOBReader', + 'ReadableStreamBYOBRequest', + 'ReadableStreamDefaultController', + 'ReadableStreamDefaultReader', + 'Request', + 'Response', + 'ShadowRoot', + 'StyleSheet', + 'TransformStream', + 'WritableStream', + 'WritableStreamDefaultController', + 'WritableStreamDefaultWriter', + 'Window', + 'cancelAnimationFrame', + 'cancelIdleCallback', + 'clearTimeout', + 'fetch', + 'requestAnimationFrame', + 'requestIdleCallback', + 'setTimeout', + ] - for (const name of webAPIs) { - assert.equal(typeof globalThis[name], 'function') - } - }, - }, - { - name: 'Constructs an Event', - test() { - const e = new Event('test') + for (const name of webAPIs) { + expect(globalThis[name]).to.be.a('function') + } + }) - assert.equal(e.type, 'test') - }, - }, - { - name: 'Constructs an EventTarget', - test() { - const t = new EventTarget() - }, - }, - { - name: 'Dispatches an Event on an EventTarget', - test() { - const t = new EventTarget() + it('Constructs an Event', () => { + const e = new Event('test') - let pass = false + expect(e.type).to.equal('test') + }) - t.addEventListener('test', (event) => { - pass = true - }) + it('Constructs an EventTarget', () => { + const _t = new EventTarget() + }) - const e = new Event('test') + it('Dispatches an Event on an EventTarget', () => { + const t = new EventTarget() - t.dispatchEvent(e) + let pass = false - assert.equal(pass, true) - }, - }, - { - name: 'Classes extend as expected', - test() { - assert.equal(HTMLElement.prototype instanceof Element, true) - assert.equal(Element.prototype instanceof Node, true) - assert.equal(Node.prototype instanceof EventTarget, true) - }, - }, - { - name: 'DOM Methods have no effect', - test() { - const element = document.createElement('div') + t.addEventListener('test', (event) => { + pass = true + }) - assert.equal(element.innerHTML, '') - element.innerHTML = 'frozen' - assert.equal(element.innerHTML, '') + const e = new Event('test') - assert.equal(element.textContent, '') - element.textContent = 'frozen' - assert.equal(element.textContent, '') - }, - }, - { - name: 'globalThis.window === globalThis', - test() { - assert.equal(globalThis.window, globalThis) - }, - }, - { - name: 'Relative Indexing Method (String#at)', - test() { - assert.equal(typeof String.prototype.at, 'function') - assert.equal(String.prototype.at.length, 1) + t.dispatchEvent(e) - const example = 'The quick brown fox jumps over the lazy dog.' + expect(pass).to.equal(true) + }) - assert.equal(example.at(2), 'e') - assert.equal(example.at(-2), 'g') - }, - }, - { - name: 'Relative Indexing Method (Array#at)', - test() { - assert.equal(typeof Array.prototype.at, 'function') - assert.equal(Array.prototype.at.length, 1) + it('Classes extend as expected', () => { + expect(HTMLElement.prototype).to.be.an.instanceof(Element) + expect(Element.prototype).to.be.an.instanceof(Node) + expect(Node.prototype).to.be.an.instanceof(EventTarget) + }) - const example = [1, 3, 5, 7, 9] + it('DOM Methods have no effect', () => { + const element = document.createElement('div') - assert.equal(example.at(1), 3) - assert.equal(example.at(-1), 9) - }, - }, - { - name: 'Relative Indexing Method (TypedArray#at)', - test() { - assert.equal(typeof Int8Array.prototype.at, 'function') - assert.equal(Int8Array.prototype.at.length, 1) + expect(element.innerHTML).to.be.empty + element.innerHTML = 'frozen' + expect(element.innerHTML).to.be.empty - const example = new Int8Array([1, 3, 5, 7, 9]) + expect(element.textContent).to.be.empty + element.textContent = 'frozen' + expect(element.textContent).to.be.empty + }) - assert.equal(example.at(1), 3) - assert.equal(example.at(-1), 9) - }, - }, - { - name: 'Object.hasOwn', - test() { - assert.equal(typeof Object.hasOwn, 'function') - assert.equal(Object.hasOwn.length, 2) + it('globalThis.window === globalThis', () => { + expect(globalThis.window).to.equal(globalThis) + }) - const example = {} + it('Relative Indexing Method (String#at)', () => { + expect(String.prototype.at).to.be.a('function') + expect(String.prototype.at.length).to.equal(1) - assert.equal(Object.hasOwn(example, 'prop'), false) + const example = 'The quick brown fox jumps over the lazy dog.' - example.prop = 'exists' + expect(example.at(2)).to.equal('e') + expect(example.at(-2)).to.equal('g') + }) - assert.equal(Object.hasOwn(example, 'prop'), true) - }, - }, - { - name: 'Promise.any', - test() { - assert.equal(typeof Promise.any, 'function') - assert.equal(Promise.any.length, 1) + it('Relative Indexing Method (Array#at)', () => { + expect(Array.prototype.at).to.be.a('function') + expect(Array.prototype.at.length).to.equal(1) - Promise.any([ - Promise.resolve(42), - Promise.reject(-1), - Promise.reject(Infinity), - ]).then((result) => { - assert.equal(result, 42) - }) - }, - }, - { - name: 'String#replaceAll', - test() { - assert.equal(typeof String.prototype.replaceAll, 'function') - assert.equal(String.prototype.replaceAll.length, 2) + const example = [1, 3, 5, 7, 9] - const t1 = - 'Of all the sorcerers in Harry Potter, Halo is my favorite sorcerer.' - const t2 = t1.replaceAll('sorcerer', 'philosopher') - const t3 = - 'Of all the philosophers in Harry Potter, Halo is my favorite philosopher.' + expect(example.at(1)).to.equal(3) + expect(example.at(-1)).to.equal(9) + }) - assert.equal(t2, t3) - }, - }, - ] + it('Relative Indexing Method (TypedArray#at)', () => { + expect(Int8Array.prototype.at).to.be.a('function') + expect(Int8Array.prototype.at.length).to.equal(1) + + const example = new Int8Array([1, 3, 5, 7, 9]) + + expect(example.at(1)).to.equal(3) + expect(example.at(-1)).to.equal(9) + }) + + it('Object.hasOwn', () => { + expect(Object.hasOwn).to.be.a('function') + expect(Object.hasOwn.length).to.equal(2) + + const example = {} + + expect(Object.hasOwn(example, 'prop')).to.equal(false) + + example.prop = 'exists' + + expect(Object.hasOwn(example, 'prop')).to.equal(true) + }) + + it('Promise.any', () => { + expect(Promise.any).to.be.a('function') + expect(Promise.any.length).to.equal(1) + + Promise.any([ + Promise.resolve(42), + Promise.reject(-1), + Promise.reject(Infinity), + ]).then((result) => { + expect(result).to.equal(42) + }) + }) + + it('String#replaceAll', () => { + expect(String.prototype.replaceAll).to.be.a('function') + expect(String.prototype.replaceAll.length).to.equal(2) + + const t1 = + 'Of all the sorcerers in Harry Potter, Halo is my favorite sorcerer.' + const t2 = t1.replaceAll('sorcerer', 'philosopher') + const t3 = + 'Of all the philosophers in Harry Potter, Halo is my favorite philosopher.' + + expect(t2).to.equal(t3) + }) }) diff --git a/packages/webapi/test/characterdata.js b/packages/webapi/test/characterdata.js index c4700cb1d..b9973db9f 100644 --- a/packages/webapi/test/characterdata.js +++ b/packages/webapi/test/characterdata.js @@ -1,65 +1,42 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes CharacterData functionality', - test() { - const target = {} +describe('CharacterData', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'CharacterData'), true) - assert.equal(Reflect.has(target, 'Text'), true) - assert.equal(Reflect.has(target, 'Comment'), true) - }, - }, - { - name: 'Throws new CharacterData', - test() { - const target = {} + it('Includes CharacterData functionality', () => { + expect(target).to.have.property('CharacterData') + expect(target).to.have.property('Text') + expect(target).to.have.property('Comment') + }) - polyfill(target) - }, - }, - { - name: 'Supports new Comment', - test() { - const target = polyfill({}) + it('Supports new Comment', () => { + expect(() => { + new target.Comment() + }).not.to.throw() - assert.doesNotThrow(() => { - new target.Comment() - }) + expect(new target.Comment().constructor.name).to.equal('Comment') + expect(Object.prototype.toString.call(new target.Comment())).to.equal( + '[object Comment]' + ) - assert.equal(new target.Comment().constructor.name, 'Comment') - assert.equal( - Object.prototype.toString.call(new target.Comment()), - '[object Comment]' - ) + expect(new target.Comment('hello').data).to.equal('hello') + expect(new target.Comment('hello').textContent).to.equal('hello') + }) - assert.equal(new target.Comment('hello').data, 'hello') - assert.equal(new target.Comment('hello').textContent, 'hello') - }, - }, - { - name: 'Supports new Text', - test() { - const target = polyfill({}) + it('Supports new Text', () => { + expect(() => { + new target.Text() + }).not.to.throw() - assert.doesNotThrow(() => { - new target.Text() - }) + expect(new target.Text().constructor.name).to.equal('Text') + expect(Object.prototype.toString.call(new target.Text())).to.equals( + '[object Text]' + ) - assert.equal(new target.Text().constructor.name, 'Text') - assert.equal( - Object.prototype.toString.call(new target.Text()), - '[object Text]' - ) - - assert.equal(new target.Text('hello').data, 'hello') - assert.equal(new target.Text('hello').textContent, 'hello') - }, - }, - ] + expect(new target.Text('hello').data).to.equal('hello') + expect(new target.Text('hello').textContent).to.equal('hello') + }) }) diff --git a/packages/webapi/test/elements.js b/packages/webapi/test/elements.js index b48a57c53..f758559a5 100644 --- a/packages/webapi/test/elements.js +++ b/packages/webapi/test/elements.js @@ -1,96 +1,70 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes Custom Element functionality', - test() { - const target = {} +describe('Custom Elements', () => { + const target = {} - polyfill(target) + beforeEach(() => polyfill(target)) - assert.equal(Reflect.has(target, 'CustomElementRegistry'), true) - assert.equal(Reflect.has(target, 'customElements'), true) - assert.equal(Reflect.has(target, 'HTMLElement'), true) - }, - }, - { - name: 'Supports Custom Element creation', - test() { - const target = {} + it('Includes Custom Element functionality', () => { + expect(target).to.have.property('CustomElementRegistry') + expect(target).to.have.property('customElements') + expect(target).to.have.property('HTMLElement') + }) - polyfill(target) + it('Supports Custom Element creation', () => { + const CustomElement = class HTMLCustomElement extends target.HTMLElement {} - const CustomElement = class HTMLCustomElement extends target.HTMLElement {} + target.customElements.define('custom-element', CustomElement) - target.customElements.define('custom-element', CustomElement) + expect(target.customElements.get('custom-element')).to.equal(CustomElement) + expect(target.customElements.getName(CustomElement)).to.equal( + 'custom-element' + ) + }) - assert.equal(target.customElements.get('custom-element'), CustomElement) - assert.equal( - target.customElements.getName(CustomElement), - 'custom-element' - ) - }, - }, - { - name: 'Supports Custom Elements created from Document', - test() { - const target = {} + it('Supports Custom Elements created from Document', () => { + expect(target.document.body.localName).to.equal('body') + expect(target.document.body.tagName).to.equal('BODY') - polyfill(target) + expect( + target.document.createElement('custom-element').constructor.name + ).to.equal('HTMLUnknownElement') - assert.equal(target.document.body.localName, 'body') - assert.equal(target.document.body.tagName, 'BODY') + const CustomElement = class HTMLCustomElement extends target.HTMLElement {} - assert.equal( - target.document.createElement('custom-element').constructor.name, - 'HTMLUnknownElement' - ) + target.customElements.define('custom-element', CustomElement) - const CustomElement = class HTMLCustomElement extends target.HTMLElement {} + expect( + target.document.createElement('custom-element').constructor.name + ).to.equal('HTMLCustomElement') + }) - target.customElements.define('custom-element', CustomElement) + it('Supports Custom Elements with properties', () => { + const testSymbol = Symbol.for('webapi.test') - assert.equal( - target.document.createElement('custom-element').constructor.name, - 'HTMLCustomElement' - ) - }, - }, - { - name: 'Supports Custom Elements with properties', - test() { - const target = {} + const CustomElement = class HTMLCustomElement extends target.HTMLElement { + otherMethod = () => testSymbol - polyfill(target) + method() { + return this.otherMethod() + } - const testSymbol = Symbol.for('webapi.test') + static method() { + return this.otherMethod() + } - const CustomElement = class HTMLCustomElement extends target.HTMLElement { - otherMethod = () => testSymbol + static otherMethod() { + return testSymbol + } + } - method() { - return this.otherMethod() - } + target.customElements.define('custom-element', CustomElement) - static method() { - return this.otherMethod() - } + expect(CustomElement.method()).to.equal(testSymbol) - static otherMethod() { - return testSymbol - } - } + const customElement = new CustomElement() - target.customElements.define('custom-element', CustomElement) - - assert.equal(CustomElement.method(), testSymbol) - - const customElement = new CustomElement() - - assert.equal(customElement.method(), testSymbol) - }, - }, - ] + expect(customElement.method()).to.equal(testSymbol) + }) }) diff --git a/packages/webapi/test/fetch.js b/packages/webapi/test/fetch.js index 48c2f688e..ae5ae0386 100644 --- a/packages/webapi/test/fetch.js +++ b/packages/webapi/test/fetch.js @@ -1,142 +1,100 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Fetch functionality', - test() { - const target = {} +describe('Fetch', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'fetch'), true) - assert.equal(typeof target.fetch, 'function') - }, - }, - { - name: 'Fetch with https', - async test() { - const target = {} + it('Fetch functionality', () => { + expect(target).to.have.property('fetch').that.is.a('function') + }) - polyfill(target) + it('Fetch with https', async () => { + const { fetch } = target - const { fetch } = target + const response = await fetch('https://api.openbrewerydb.org/breweries') - const response = await fetch('https://api.openbrewerydb.org/breweries') + expect(response.constructor).to.equal(target.Response) - assert.equal(response.constructor, target.Response) + const json = await response.json() - const json = await response.json() + expect(json).to.be.an('array') + }) - assert.equal(Array.isArray(json), true) - }, - }, - { - name: 'Fetch with file', - async test() { - const target = {} + it('Fetch with file', async () => { + const { fetch } = target - polyfill(target) + const url = new URL('../package.json', import.meta.url) - const { fetch } = target + const response = await fetch(url) - const url = new URL('../package.json', import.meta.url) + expect(response.constructor).to.equal(target.Response) - const response = await fetch(url) + expect(response.status).to.equal(200) + expect(response.statusText).to.be.empty + expect(response.headers.has('date')).to.equal(true) + expect(response.headers.has('content-length')).to.equal(true) + expect(response.headers.has('last-modified')).to.equal(true) - assert.equal(response.constructor, target.Response) + const json = await response.json() - assert.equal(response.status, 200) - assert.equal(response.statusText, '') - assert.equal(response.headers.has('date'), true) - assert.equal(response.headers.has('content-length'), true) - assert.equal(response.headers.has('last-modified'), true) + expect(json.name).to.equal('@astrojs/webapi') + }) - const json = await response.json() + it('Fetch with missing file', async () => { + const { fetch } = target - assert.equal(json.name, '@astrojs/webapi') - }, - }, - { - name: 'Fetch with missing file', - async test() { - const target = {} + const url = new URL('../missing.json', import.meta.url) - polyfill(target) + const response = await fetch(url) - const { fetch } = target + expect(response.constructor).to.equal(target.Response) - const url = new URL('../missing.json', import.meta.url) + expect(response.status).to.equal(404) + expect(response.statusText).to.be.empty + expect(response.headers.has('date')).to.equal(true) + expect(response.headers.has('content-length')).to.equal(false) + expect(response.headers.has('last-modified')).to.equal(false) + }) - const response = await fetch(url) + it('Fetch with (file) Request', async () => { + const { Request, fetch } = target - assert.equal(response.constructor, target.Response) + const request = new Request(new URL('../package.json', import.meta.url)) - assert.equal(response.status, 404) - assert.equal(response.statusText, '') - assert.equal(response.headers.has('date'), true) - assert.equal(response.headers.has('content-length'), false) - assert.equal(response.headers.has('last-modified'), false) - }, - }, - { - name: 'Fetch with (file) Request', - async test() { - const target = {} + const response = await fetch(request) - polyfill(target) + expect(response.constructor).to.equal(target.Response) - const { Request, fetch } = target + const json = await response.json() - const request = new Request(new URL('../package.json', import.meta.url)) + expect(json.name).to.equal('@astrojs/webapi') + }) - const response = await fetch(request) + it('Fetch with relative file', async () => { + const { fetch } = target - assert.equal(response.constructor, target.Response) + const response = await fetch('package.json') - const json = await response.json() + const json = await response.json() - assert.equal(json.name, '@astrojs/webapi') - }, - }, - { - name: 'Fetch with relative file', - async test() { - const target = {} + expect(json.name).to.equal('@astrojs/webapi') + }) - polyfill(target) + it('Fetch with data', async () => { + const { fetch } = target - const { fetch } = target + const jsonURI = `data:application/json,${encodeURIComponent( + JSON.stringify({ + name: '@astrojs/webapi', + }) + )}` - const response = await fetch('package.json') + const response = await fetch(jsonURI) - const json = await response.json() + const json = await response.json() - assert.equal(json.name, '@astrojs/webapi') - }, - }, - { - name: 'Fetch with data', - async test() { - const target = {} - - polyfill(target) - - const { fetch } = target - - const jsonURI = `data:application/json,${encodeURIComponent( - JSON.stringify({ - name: '@astrojs/webapi', - }) - )}` - - const response = await fetch(jsonURI) - - const json = await response.json() - - assert.equal(json.name, '@astrojs/webapi') - }, - }, - ] + expect(json.name).to.equal('@astrojs/webapi') + }) }) diff --git a/packages/webapi/test/imagedata.js b/packages/webapi/test/imagedata.js index 7f35eddd7..b386a18d6 100644 --- a/packages/webapi/test/imagedata.js +++ b/packages/webapi/test/imagedata.js @@ -1,84 +1,54 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Supports ImageData', - test() { - const target = {} +describe('ImageData', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal('ImageData' in target, true) - assert.equal(typeof target['ImageData'], 'function') - }, - }, - { - name: 'Supports new (data: Uint8ClampedArray, width: number, height: number): ImageData', - test() { - const target = {} + it('Supports ImageData', () => { + expect(target).to.have.property('ImageData').that.is.a('function') + }) - polyfill(target) + it('Supports new (data: Uint8ClampedArray, width: number, height: number): ImageData', () => { + const w = 640 + const h = 480 + const d = new Uint8ClampedArray(w * h * 4) - const w = 640 - const h = 480 - const d = new Uint8ClampedArray(w * h * 4) + const id = new target.ImageData(d, w, h) - const id = new target.ImageData(d, w, h) + expect(id.data).to.equal(d) + expect(id.width).to.equal(w) + expect(id.height).to.equal(h) + }) - assert.equal(id.data, d) - assert.equal(id.width, w) - assert.equal(id.height, h) - }, - }, - { - name: 'Supports new (data: Uint8ClampedArray, width: number): ImageData', - test() { - const target = {} + it('Supports new (data: Uint8ClampedArray, width: number): ImageData', () => { + const w = 640 + const h = 480 + const d = new Uint8ClampedArray(w * h * 4) - polyfill(target) + const id = new target.ImageData(d, w) - const w = 640 - const h = 480 - const d = new Uint8ClampedArray(w * h * 4) + expect(id.data).to.equal(d) + expect(id.width).to.equal(w) + expect(id.height).to.equal(h) + }) - const id = new target.ImageData(d, w) + it('Supports new (width: number, height: number): ImageData', () => { + const w = 640 + const h = 480 - assert.equal(id.data, d) - assert.equal(id.width, w) - assert.equal(id.height, h) - }, - }, - { - name: 'Supports new (width: number, height: number): ImageData', - test() { - const target = {} + const id = new target.ImageData(w, h) - polyfill(target) + expect(id.data).to.have.lengthOf(w * h * 4) + expect(id.width).to.equal(w) + expect(id.height).to.equal(h) + }) - const w = 640 - const h = 480 + it('Supports Object.keys(new ImageData(640, 480))', () => { + const keys = Object.keys(new target.ImageData(640, 480)) - const id = new target.ImageData(w, h) - - assert.equal(id.data.length, w * h * 4) - assert.equal(id.width, w) - assert.equal(id.height, h) - }, - }, - { - name: 'Supports Object.keys(new ImageData(640, 480))', - test() { - const target = {} - - polyfill(target) - - const keys = Object.keys(new target.ImageData(640, 480)) - - assert.equal(keys.length, 1) - assert.equal(keys[0], 'data') - }, - }, - ] + expect(keys).to.have.lengthOf(1) + expect(keys[0]).to.equal('data') + }) }) diff --git a/packages/webapi/test/internals.js b/packages/webapi/test/internals.js index 6972f65a2..054b7e488 100644 --- a/packages/webapi/test/internals.js +++ b/packages/webapi/test/internals.js @@ -1,34 +1,26 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes polyfill.internals functionality', - test() { - const target = {} +it('Includes polyfill.internals functionality', () => { + const target = {} - polyfill(target, { exclude: 'window document' }) + polyfill(target, { exclude: 'window document' }) - const pseudo = { ...target } + const pseudo = { ...target } - assert.equal(Reflect.has(pseudo, 'document'), false) + expect(pseudo).to.not.have.property('document') - const CustomElement = class extends pseudo.HTMLElement {} + const CustomElement = class extends pseudo.HTMLElement {} - pseudo.customElements.define('custom-element', CustomElement) + pseudo.customElements.define('custom-element', CustomElement) - polyfill.internals(pseudo, 'Document') + polyfill.internals(pseudo, 'Document') - assert.equal(Reflect.has(pseudo, 'document'), true) + expect(pseudo).to.have.property('document') - assert.equal( - CustomElement.prototype.isPrototypeOf( - pseudo.document.createElement('custom-element') - ), - true - ) - }, - }, - ] + expect( + CustomElement.prototype.isPrototypeOf( + pseudo.document.createElement('custom-element') + ) + ).to.equal(true) }) diff --git a/packages/webapi/test/media.js b/packages/webapi/test/media.js index c1b4607ca..e6e7b82d4 100644 --- a/packages/webapi/test/media.js +++ b/packages/webapi/test/media.js @@ -1,31 +1,20 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes MediaQueryList functionality', - test() { - const target = {} +describe('Media', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'MediaQueryList'), true) - assert.equal(Reflect.has(target, 'matchMedia'), true) - }, - }, - { - name: 'Supports matchMedia creation', - test() { - const target = {} + it('Includes MediaQueryList functionality', () => { + expect(target).to.have.property('MediaQueryList') + expect(target).to.have.property('matchMedia') + }) - polyfill(target) + it('Supports matchMedia creation', () => { + const mql = target.matchMedia('(min-width: 640px)') - const mql = target.matchMedia('(min-width: 640px)') - - assert.equal(mql.matches, false) - assert.equal(mql.media, '(min-width: 640px)') - }, - }, - ] + expect(mql.matches).to.equal(false) + expect(mql.media).to.equal('(min-width: 640px)') + }) }) diff --git a/packages/webapi/test/offscreencanvas.js b/packages/webapi/test/offscreencanvas.js index cf5bbb6a3..6223e78e8 100644 --- a/packages/webapi/test/offscreencanvas.js +++ b/packages/webapi/test/offscreencanvas.js @@ -1,57 +1,39 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Supports OffscreenCanvas', - test() { - const target = {} +describe('OffscreenCanvas', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal('OffscreenCanvas' in target, true) - assert.equal(typeof target['OffscreenCanvas'], 'function') - }, - }, - { - name: 'Supports new (width: number, height: number): OffscreenCanvas', - test() { - const target = {} + it('Supports OffscreenCanvas', () => { + expect(target).to.have.property('OffscreenCanvas').that.is.a('function') + }) - polyfill(target) + it('Supports new (width: number, height: number): OffscreenCanvas', () => { + const w = 640 + const h = 480 - const w = 640 - const h = 480 + const canvas = new target.OffscreenCanvas(w, h) - const canvas = new target.OffscreenCanvas(w, h) + expect(canvas.width).to.equal(w) + expect(canvas.height).to.equal(h) + }) - assert.equal(canvas.width, w) - assert.equal(canvas.height, h) - }, - }, - { - name: 'Supports OffscreenCanvas#getContext', - test() { - const target = {} + it('Supports OffscreenCanvas#getContext', () => { + const w = 640 + const h = 480 - polyfill(target) + const canvas = new target.OffscreenCanvas(w, h) - const w = 640 - const h = 480 + const context = canvas.getContext('2d') - const canvas = new target.OffscreenCanvas(w, h) + expect(context.canvas).to.equal(canvas) - const context = canvas.getContext('2d') + const imageData = context.createImageData(w, h) - assert.equal(context.canvas, canvas) - - const imageData = context.createImageData(w, h) - - assert.equal(imageData.width, w) - assert.equal(imageData.height, h) - assert.equal(imageData.data.length, w * h * 4) - }, - }, - ] + expect(imageData.width).to.equal(w) + expect(imageData.height).to.equal(h) + expect(imageData.data).to.have.lengthOf(w * h * 4) + }) }) diff --git a/packages/webapi/test/options.js b/packages/webapi/test/options.js index f322d708a..0db1cbfe3 100644 --- a/packages/webapi/test/options.js +++ b/packages/webapi/test/options.js @@ -1,53 +1,44 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Can exclude HTMLElement+', - test() { - const target = {} +describe('Options', () => { + it('Can exclude HTMLElement+', () => { + const target = {} - polyfill(target, { - exclude: 'HTMLElement+', - }) + polyfill(target, { + exclude: 'HTMLElement+', + }) - assert.equal(Reflect.has(target, 'Event'), true) - assert.equal(Reflect.has(target, 'EventTarget'), true) - assert.equal(Reflect.has(target, 'Element'), true) - assert.equal(Reflect.has(target, 'HTMLElement'), false) - assert.equal(Reflect.has(target, 'HTMLDivElement'), false) - }, - }, - { - name: 'Can exclude Event+', - test() { - const target = {} + expect(target).to.have.property('Event') + expect(target).to.have.property('EventTarget') + expect(target).to.have.property('Element') + expect(target).to.not.have.property('HTMLElement') + expect(target).to.not.have.property('HTMLDivElement') + }) - polyfill(target, { - exclude: 'Event+', - }) + it('Can exclude Event+', () => { + const target = {} - assert.equal(Reflect.has(target, 'Event'), false) - assert.equal(Reflect.has(target, 'EventTarget'), false) - assert.equal(Reflect.has(target, 'Element'), false) - assert.equal(Reflect.has(target, 'HTMLElement'), false) - assert.equal(Reflect.has(target, 'HTMLDivElement'), false) - }, - }, - { - name: 'Can exclude document', - test() { - const target = {} + polyfill(target, { + exclude: 'Event+', + }) - polyfill(target, { - exclude: 'document', - }) + expect(target).to.not.have.property('Event') + expect(target).to.not.have.property('EventTarget') + expect(target).to.not.have.property('Element') + expect(target).to.not.have.property('HTMLElement') + expect(target).to.not.have.property('HTMLDivElement') + }) - assert.equal(Reflect.has(target, 'Document'), true) - assert.equal(Reflect.has(target, 'HTMLDocument'), true) - assert.equal(Reflect.has(target, 'document'), false) - }, - }, - ] + it('Can exclude document', () => { + const target = {} + + polyfill(target, { + exclude: 'document', + }) + + expect(target).to.have.property('Document') + expect(target).to.have.property('HTMLDocument') + expect(target).to.not.have.property('document') + }) }) diff --git a/packages/webapi/test/storage.js b/packages/webapi/test/storage.js index 0564b1074..9a185644a 100644 --- a/packages/webapi/test/storage.js +++ b/packages/webapi/test/storage.js @@ -1,45 +1,32 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes Storage functionality', - test() { - const target = {} +describe('Storage', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'Storage'), true) - assert.equal(Reflect.has(target, 'localStorage'), true) - assert.equal(typeof target.Storage, 'function') - assert.equal(typeof target.localStorage, 'object') - }, - }, - { - name: 'Supports Storage methods', - test() { - const target = {} + it('Includes Storage functionality', () => { + expect(target).to.have.property('Storage').that.is.a('function') + expect(target).to.have.property('localStorage').that.is.an('object') + }) - polyfill(target) - - assert.equal(target.localStorage.setItem('hello', 'world'), undefined) - assert.equal(target.localStorage.getItem('hello'), 'world') - assert.equal(target.localStorage.key(0), 'hello') - assert.equal(target.localStorage.key(1), null) - assert.equal(target.localStorage.length, 1) - assert.equal(target.localStorage.setItem('world', 'hello'), undefined) - assert.equal(target.localStorage.key(1), 'world') - assert.equal(target.localStorage.key(2), null) - assert.equal(target.localStorage.length, 2) - assert.equal(target.localStorage.removeItem('hello'), undefined) - assert.equal(target.localStorage.key(0), 'world') - assert.equal(target.localStorage.key(1), null) - assert.equal(target.localStorage.length, 1) - assert.equal(target.localStorage.clear(), undefined) - assert.equal(target.localStorage.key(0), null) - assert.equal(target.localStorage.length, 0) - }, - }, - ] + it('Supports Storage methods', () => { + expect(target.localStorage.setItem('hello', 'world')).to.equal(undefined) + expect(target.localStorage.getItem('hello')).to.equal('world') + expect(target.localStorage.key(0)).to.equal('hello') + expect(target.localStorage.key(1)).to.equal(null) + expect(target.localStorage.length).to.equal(1) + expect(target.localStorage.setItem('world', 'hello')).to.equal(undefined) + expect(target.localStorage.key(1)).to.equal('world') + expect(target.localStorage.key(2)).to.equal(null) + expect(target.localStorage.length).to.equal(2) + expect(target.localStorage.removeItem('hello')).to.equal(undefined) + expect(target.localStorage.key(0)).to.equal('world') + expect(target.localStorage.key(1)).to.equal(null) + expect(target.localStorage.length).to.equal(1) + expect(target.localStorage.clear()).to.equal(undefined) + expect(target.localStorage.key(0)).to.equal(null) + expect(target.localStorage.length).to.equal(0) + }) }) diff --git a/packages/webapi/test/structuredclone.js b/packages/webapi/test/structuredclone.js index 5e79f3194..0503cf904 100644 --- a/packages/webapi/test/structuredclone.js +++ b/packages/webapi/test/structuredclone.js @@ -1,40 +1,28 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes structuredClone', - test() { - const target = {} +describe('structuredClone', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'structuredClone'), true) - assert.equal(typeof target.structuredClone, 'function') + it('Includes structuredClone', () => { + expect(target).to.have.property('structuredClone').that.is.a('function') + }) + + it('Supports structuredClone usage', () => { + const obj = { + foo: 'bar', + baz: { + qux: 'quux', }, - }, - { - name: 'Supports structuredClone usage', - test() { - const target = {} + } - polyfill(target) + const clone = target.structuredClone(obj) - const obj = { - foo: 'bar', - baz: { - qux: 'quux', - }, - } + expect(obj).to.not.equal(clone) + expect(obj.baz).to.not.equal(clone.baz) - const clone = target.structuredClone(obj) - - assert.notEqual(obj, clone) - assert.notEqual(obj.baz, clone.baz) - - assert.equal(obj.baz.qux, clone.baz.qux) - }, - }, - ] + expect(obj.baz.qux).to.equal(clone.baz.qux) + }) }) diff --git a/packages/webapi/test/urlpattern.js b/packages/webapi/test/urlpattern.js index aac7f9d4e..b9ef6b31f 100644 --- a/packages/webapi/test/urlpattern.js +++ b/packages/webapi/test/urlpattern.js @@ -1,31 +1,19 @@ -import { assert, test } from '../run/test.setup.js' +import { expect } from 'chai' import { polyfill } from '../mod.js' -test(() => { - return [ - { - name: 'Includes URLPattern', - test() { - const target = {} +describe('URLPattern', () => { + const target = {} - polyfill(target) + before(() => polyfill(target)) - assert.equal(Reflect.has(target, 'URLPattern'), true) - assert.equal(typeof target.URLPattern, 'function') - }, - }, - { - name: 'Supports URLPattern usage', - test() { - const target = {} + it('Includes URLPattern', () => { + expect(target).to.have.property('URLPattern').that.is.a('function') + }) - polyfill(target) + it('Supports URLPattern usage', () => { + const pattern = new target.URLPattern({ pathname: '/hello/:name' }) + const match = pattern.exec('https://example.com/hello/Deno') - const pattern = new target.URLPattern({ pathname: '/hello/:name' }) - const match = pattern.exec('https://example.com/hello/Deno') - - assert.deepEqual(match.pathname.groups, { name: 'Deno' }) - }, - }, - ] + expect(match.pathname.groups).to.deep.equal({ name: 'Deno' }) + }) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b38d40c4e..1e1a382d9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1526,10 +1526,12 @@ importers: '@rollup/plugin-inject': ^4.0.4 '@rollup/plugin-node-resolve': ^13.1.3 '@rollup/plugin-typescript': ^8.3.1 + '@types/chai': ^4.3.0 '@types/mocha': ^9.1.0 '@types/node': ^14.18.12 '@ungap/structured-clone': ^0.3.4 abort-controller: ^3.0.0 + chai: ^4.3.6 event-target-shim: ^6.0.2 fetch-blob: ^3.1.5 formdata-polyfill: ^4.0.10 @@ -1546,10 +1548,12 @@ importers: '@rollup/plugin-inject': 4.0.4_rollup@2.70.1 '@rollup/plugin-node-resolve': 13.1.3_rollup@2.70.1 '@rollup/plugin-typescript': 8.3.1_8fb0118e9d69f3600cbb8f144f7d456c + '@types/chai': 4.3.0 '@types/mocha': 9.1.0 '@types/node': 14.18.12 '@ungap/structured-clone': 0.3.4 abort-controller: 3.0.0 + chai: 4.3.6 event-target-shim: 6.0.2 fetch-blob: 3.1.5 formdata-polyfill: 4.0.10