Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: port webidl tests to node test runner #2554

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"test": "node scripts/generate-pem && npm run test:tap && npm run test:node-fetch && npm run test:fetch && npm run test:cookies && npm run test:wpt && npm run test:websocket && npm run test:jest && npm run test:typescript",
"test:cookies": "tap test/cookie/*.js",
"test:node-fetch": "mocha --exit test/node-fetch",
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && tap test/webidl/*.js",
"test:fetch": "npm run build:node && tap --expose-gc test/fetch/*.js && borp --coverage -p \"test/webidl/*.js\"",
"test:jest": "jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",
Expand All @@ -100,6 +100,7 @@
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
"atomic-sleep": "^1.0.0",
"borp": "^0.4.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"chai-iterator": "^3.0.2",
Expand Down
93 changes: 38 additions & 55 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
'use strict'

const { test } = require('tap')
const { describe, test } = require('node:test')
const assert = require('node:assert')
const { webidl } = require('../../lib/fetch/webidl')

test('sequence', (t) => {
test('sequence', () => {
const converter = webidl.sequenceConverter(
webidl.converters.DOMString
)

t.same(converter([1, 2, 3]), ['1', '2', '3'])
assert.deepStrictEqual(converter([1, 2, 3]), ['1', '2', '3'])

t.throws(() => {
assert.throws(() => {
converter(3)
}, TypeError, 'disallows non-objects')

t.throws(() => {
assert.throws(() => {
converter(null)
}, TypeError)

t.throws(() => {
assert.throws(() => {
converter(undefined)
}, TypeError)

t.throws(() => {
assert.throws(() => {
converter({})
}, TypeError, 'no Symbol.iterator')

t.throws(() => {
assert.throws(() => {
converter({
[Symbol.iterator]: 42
})
}, TypeError, 'invalid Symbol.iterator')

t.throws(() => {
assert.throws(() => {
converter(webidl.converters.sequence({
[Symbol.iterator] () {
return {
Expand All @@ -41,28 +42,24 @@ test('sequence', (t) => {
}
}))
}, TypeError, 'invalid generator')

t.end()
})

test('webidl.dictionaryConverter', (t) => {
t.test('arguments', (t) => {
describe('webidl.dictionaryConverter', () => {
test('arguments', () => {
const converter = webidl.dictionaryConverter([])

t.throws(() => {
assert.throws(() => {
converter(true)
}, TypeError)

for (const value of [{}, undefined, null]) {
t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter(value)
})
}

t.end()
})

t.test('required key', (t) => {
test('required key', () => {
const converter = webidl.dictionaryConverter([
{
converter: () => true,
Expand All @@ -71,57 +68,51 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.throws(() => {
assert.throws(() => {
converter({ wrongKey: 'key' })
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter({ Key: 'this key was required!' })
})

t.end()
})

t.end()
})

test('ArrayBuffer', (t) => {
t.throws(() => {
test('ArrayBuffer', () => {
assert.throws(() => {
webidl.converters.ArrayBuffer(true)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.ArrayBuffer({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const sab = new SharedArrayBuffer(1024)
webidl.converters.ArrayBuffer(sab, { allowShared: false })
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
const sab = new SharedArrayBuffer(1024)
webidl.converters.ArrayBuffer(sab)
})

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
const ab = new ArrayBuffer(8)
webidl.converters.ArrayBuffer(ab)
})

t.end()
})

test('TypedArray', (t) => {
t.throws(() => {
test('TypedArray', () => {
assert.throws(() => {
webidl.converters.TypedArray(3)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.TypedArray({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const uint8 = new Uint8Array([1, 2, 3])
Object.defineProperty(uint8, 'buffer', {
get () {
Expand All @@ -133,20 +124,18 @@ test('TypedArray', (t) => {
allowShared: false
})
}, TypeError)

t.end()
})

test('DataView', (t) => {
t.throws(() => {
test('DataView', () => {
assert.throws(() => {
webidl.converters.DataView(3)
}, TypeError)

t.throws(() => {
assert.throws(() => {
webidl.converters.DataView({})
}, TypeError)

t.throws(() => {
assert.throws(() => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

Expand All @@ -164,39 +153,33 @@ test('DataView', (t) => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

t.equal(webidl.converters.DataView(view), view)

t.end()
assert.equal(webidl.converters.DataView(view), view)
})

test('BufferSource', (t) => {
t.doesNotThrow(() => {
test('BufferSource', () => {
assert.doesNotThrow(() => {
const buffer = new ArrayBuffer(16)
const view = new DataView(buffer, 0)

webidl.converters.BufferSource(view)
})

t.throws(() => {
assert.throws(() => {
webidl.converters.BufferSource(3)
}, TypeError)

t.end()
})

test('ByteString', (t) => {
t.doesNotThrow(() => {
test('ByteString', () => {
assert.doesNotThrow(() => {
webidl.converters.ByteString('')
})

// https://github.com/nodejs/undici/issues/1590
t.throws(() => {
assert.throws(() => {
const char = String.fromCharCode(256)
webidl.converters.ByteString(`invalid${char}char`)
}, {
message: 'Cannot convert argument to a ByteString because the character at ' +
'index 7 has a value of 256 which is greater than 255.'
})

t.end()
})
31 changes: 12 additions & 19 deletions test/webidl/helpers.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
'use strict'

const { test } = require('tap')
const { describe, test } = require('node:test')
const assert = require('node:assert')
const { webidl } = require('../../lib/fetch/webidl')

test('webidl.interfaceConverter', (t) => {
test('webidl.interfaceConverter', () => {
class A {}
class B {}

const converter = webidl.interfaceConverter(A)

t.throws(() => {
assert.throws(() => {
converter(new B())
}, TypeError)

t.doesNotThrow(() => {
assert.doesNotThrow(() => {
converter(new A())
})

t.end()
})

test('webidl.dictionaryConverter', (t) => {
t.test('extraneous keys are provided', (t) => {
describe('webidl.dictionaryConverter', () => {
test('extraneous keys are provided', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
Expand All @@ -31,7 +30,7 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.same(
assert.deepStrictEqual(
converter({
a: 'b',
key: 'string',
Expand All @@ -42,11 +41,9 @@ test('webidl.dictionaryConverter', (t) => {
}),
{ key: 'string' }
)

t.end()
})

t.test('defaultValue with key = null', (t) => {
test('defaultValue with key = null', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
Expand All @@ -55,21 +52,17 @@ test('webidl.dictionaryConverter', (t) => {
}
])

t.same(converter({ key: null }), { key: 0 })
t.end()
assert.deepStrictEqual(converter({ key: null }), { key: 0 })
})

t.test('no defaultValue and optional', (t) => {
test('no defaultValue and optional', () => {
const converter = webidl.dictionaryConverter([
{
key: 'key',
converter: webidl.converters.ByteString
}
])

t.same(converter({ a: 'b', c: 'd' }), {})
t.end()
assert.deepStrictEqual(converter({ a: 'b', c: 'd' }), {})
})

t.end()
})