Skip to content

Commit

Permalink
chore: remove mocha and chai (nodejs#2696)
Browse files Browse the repository at this point in the history
* chore: remove mocha and chai, fix bug in fetch request

* fix test

* uncomment test cases

* remove p-timeout

* fix

* add back removed line
  • Loading branch information
Uzlopak authored and crysmags committed Feb 27, 2024
1 parent 7cd3d82 commit 89448eb
Show file tree
Hide file tree
Showing 7 changed files with 739 additions and 755 deletions.
20 changes: 7 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"lint:fix": "standard --fix | snazzy",
"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:eventsource && npm run test:wpt && npm run test:websocket && npm run test:jest && npm run test:typescript && npm run test:node-test",
"test:cookies": "borp --coverage -p \"test/cookie/*.js\"",
"test:node-fetch": "mocha --exit test/node-fetch",
"test:node-fetch": "borp --coverage -p \"test/node-fetch/**/*.js\"",
"test:eventsource": "npm run build:node && borp --expose-gc --coverage -p \"test/eventsource/*.js\"",
"test:fetch": "npm run build:node && borp --expose-gc --coverage -p \"test/fetch/*.js\" && borp --coverage -p \"test/webidl/*.js\"",
"test:jest": "jest",
Expand All @@ -104,48 +104,42 @@
"@sinonjs/fake-timers": "^11.1.0",
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
"axios": "^1.6.5",
"borp": "^0.9.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"chai-iterator": "^3.0.2",
"chai-string": "^1.5.0",
"concurrently": "^8.0.1",
"cronometro": "^2.0.2",
"dns-packet": "^5.4.0",
"docsify-cli": "^4.4.3",
"form-data": "^4.0.0",
"formdata-node": "^6.0.3",
"got": "^14.0.0",
"https-pem": "^3.0.0",
"husky": "^9.0.7",
"import-fresh": "^3.3.0",
"jest": "^29.0.2",
"jsdom": "^24.0.0",
"jsfuzz": "^1.0.15",
"mitata": "^0.1.8",
"mocha": "^10.0.0",
"p-timeout": "^3.2.0",
"node-fetch": "^3.3.2",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"proxyquire": "^2.1.3",
"request": "^2.88.2",
"sinon": "^17.0.1",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.1.0",
"tsd": "^0.30.1",
"typescript": "^5.0.2",
"wait-on": "^7.0.1",
"ws": "^8.11.0",
"axios": "^1.6.5",
"got": "^14.0.0",
"node-fetch": "^3.3.2",
"request": "^2.88.2"
"ws": "^8.11.0"
},
"engines": {
"node": ">=18.0"
},
"standard": {
"env": [
"mocha"
"jest"
],
"ignore": [
"lib/llhttp/constants.js",
Expand Down
185 changes: 106 additions & 79 deletions test/node-fetch/headers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/* eslint no-unused-expressions: "off" */
'use strict'

const assert = require('node:assert')
const { describe, it } = require('node:test')
const { format } = require('node:util')
const chai = require('chai')
const chaiIterator = require('chai-iterator')
const { Headers } = require('../../lib/fetch/headers.js')

chai.use(chaiIterator)

const { expect } = chai
const { Headers } = require('../../index.js')

describe('Headers', () => {
it('should have attributes conforming to Web IDL', () => {
const headers = new Headers()
expect(Object.getOwnPropertyNames(headers)).to.be.empty
assert.strictEqual(Object.getOwnPropertyNames(headers).length, 0)
const enumerableProperties = []

for (const property in headers) {
Expand All @@ -30,7 +26,7 @@ describe('Headers', () => {
'set',
'values'
]) {
expect(enumerableProperties).to.contain(toCheck)
assert.strictEqual(enumerableProperties.includes(toCheck), true)
}
})

Expand All @@ -41,14 +37,14 @@ describe('Headers', () => {
['b', '3'],
['a', '1']
])
expect(headers).to.have.property('forEach')
assert.strictEqual(typeof headers.forEach, 'function')

const result = []
for (const [key, value] of headers.entries()) {
result.push([key, value])
}

expect(result).to.deep.equal([
assert.deepStrictEqual(result, [
['a', '1'],
['b', '2, 3'],
['c', '4']
Expand All @@ -66,23 +62,23 @@ describe('Headers', () => {
results.push({ value, key, object })
})

expect(results.length).to.equal(2)
expect({ key: 'accept', value: 'application/json, text/plain', object: headers }).to.deep.equal(results[0])
expect({ key: 'content-type', value: 'text/html', object: headers }).to.deep.equal(results[1])
assert.strictEqual(results.length, 2)
assert.deepStrictEqual(results[0], { key: 'accept', value: 'application/json, text/plain', object: headers })
assert.deepStrictEqual(results[1], { key: 'content-type', value: 'text/html', object: headers })
})

xit('should set "this" to undefined by default on forEach', () => {
it.skip('should set "this" to undefined by default on forEach', () => {
const headers = new Headers({ Accept: 'application/json' })
headers.forEach(function () {
expect(this).to.be.undefined
assert.strictEqual(this, undefined)
})
})

it('should accept thisArg as a second argument for forEach', () => {
const headers = new Headers({ Accept: 'application/json' })
const thisArg = {}
headers.forEach(function () {
expect(this).to.equal(thisArg)
assert.strictEqual(this, thisArg)
}, thisArg)
})

Expand All @@ -93,14 +89,14 @@ describe('Headers', () => {
['a', '1']
])
headers.append('b', '3')
expect(headers).to.be.iterable
assert.strictEqual(typeof headers[Symbol.iterator], 'function')

const result = []
for (const pair of headers) {
result.push(pair)
}

expect(result).to.deep.equal([
assert.deepStrictEqual(result, [
['a', '1'],
['b', '2, 3'],
['c', '4']
Expand All @@ -115,12 +111,22 @@ describe('Headers', () => {
])
headers.append('b', '3')

expect(headers.entries()).to.be.iterable
.and.to.deep.iterate.over([
['a', '1'],
['b', '2, 3'],
['c', '4']
])
assert.strictEqual(typeof headers.entries, 'function')
assert.strictEqual(typeof headers.entries()[Symbol.iterator], 'function')

const entries = headers.entries()
assert.strictEqual(typeof entries.next, 'function')
assert.deepStrictEqual(entries.next().value, ['a', '1'])
assert.strictEqual(typeof entries.next, 'function')
assert.deepStrictEqual(entries.next().value, ['b', '2, 3'])
assert.strictEqual(typeof entries.next, 'function')
assert.deepStrictEqual(entries.next().value, ['c', '4'])

assert.deepStrictEqual([...headers.entries()], [
['a', '1'],
['b', '2, 3'],
['c', '4']
])
})

it('should allow iterating through all headers with keys()', () => {
Expand All @@ -131,8 +137,18 @@ describe('Headers', () => {
])
headers.append('b', '3')

expect(headers.keys()).to.be.iterable
.and.to.iterate.over(['a', 'b', 'c'])
assert.strictEqual(typeof headers.keys, 'function')
assert.strictEqual(typeof headers.keys()[Symbol.iterator], 'function')

const keys = headers.keys()
assert.strictEqual(typeof keys.next, 'function')
assert.strictEqual(keys.next().value, 'a')
assert.strictEqual(typeof keys.next, 'function')
assert.strictEqual(keys.next().value, 'b')
assert.strictEqual(typeof keys.next, 'function')
assert.strictEqual(keys.next().value, 'c')

assert.deepStrictEqual([...headers.keys()], ['a', 'b', 'c'])
})

it('should allow iterating through all headers with values()', () => {
Expand All @@ -143,26 +159,37 @@ describe('Headers', () => {
])
headers.append('b', '3')

expect(headers.values()).to.be.iterable
.and.to.iterate.over(['1', '2, 3', '4'])
assert.strictEqual(typeof headers.values, 'function')
assert.strictEqual(typeof headers.values()[Symbol.iterator], 'function')

const values = headers.values()
assert.strictEqual(typeof values.next, 'function')
assert.strictEqual(values.next().value, '1')
assert.strictEqual(typeof values.next, 'function')
assert.strictEqual(values.next().value, '2, 3')
assert.strictEqual(typeof values.next, 'function')
assert.strictEqual(values.next().value, '4')

assert.deepStrictEqual([...headers.values()], ['1', '2, 3', '4'])
})

it('should reject illegal header', () => {
const headers = new Headers()
expect(() => new Headers({ 'He y': 'ok' })).to.throw(TypeError)
expect(() => new Headers({ 'Hé-y': 'ok' })).to.throw(TypeError)
expect(() => new Headers({ 'He-y': 'ăk' })).to.throw(TypeError)
expect(() => headers.append('Hé-y', 'ok')).to.throw(TypeError)
expect(() => headers.delete('Hé-y')).to.throw(TypeError)
expect(() => headers.get('Hé-y')).to.throw(TypeError)
expect(() => headers.has('Hé-y')).to.throw(TypeError)
expect(() => headers.set('Hé-y', 'ok')).to.throw(TypeError)

assert.throws(() => new Headers({ 'He y': 'ok' }), TypeError)
assert.throws(() => new Headers({ 'Hé-y': 'ok' }), TypeError)
assert.throws(() => new Headers({ 'He-y': 'ăk' }), TypeError)
assert.throws(() => headers.append('Hé-y', 'ok'), TypeError)
assert.throws(() => headers.delete('Hé-y'), TypeError)
assert.throws(() => headers.get('Hé-y'), TypeError)
assert.throws(() => headers.has('Hé-y'), TypeError)
assert.throws(() => headers.set('Hé-y', 'ok'), TypeError)
// Should reject empty header
expect(() => headers.append('', 'ok')).to.throw(TypeError)
assert.throws(() => headers.append('', 'ok'), TypeError)
})

xit('should ignore unsupported attributes while reading headers', () => {
const FakeHeader = function () {}
it.skip('should ignore unsupported attributes while reading headers', () => {
const FakeHeader = function () { }
// Prototypes are currently ignored
// This might change in the future: #181
FakeHeader.prototype.z = 'fake'
Expand All @@ -188,26 +215,26 @@ describe('Headers', () => {

const h1Raw = h1.raw()

expect(h1Raw.a).to.include('string')
expect(h1Raw.b).to.include('1,2')
expect(h1Raw.c).to.include('')
expect(h1Raw.d).to.include('')
expect(h1Raw.e).to.include('1')
expect(h1Raw.f).to.include('1,2')
expect(h1Raw.g).to.include('[object Object]')
expect(h1Raw.h).to.include('undefined')
expect(h1Raw.i).to.include('null')
expect(h1Raw.j).to.include('NaN')
expect(h1Raw.k).to.include('true')
expect(h1Raw.l).to.include('false')
expect(h1Raw.m).to.include('test')
expect(h1Raw.n).to.include('1,2')
expect(h1Raw.n).to.include('3,4')

expect(h1Raw.z).to.be.undefined
assert.strictEqual(h1Raw.a.includes('string'), true)
assert.strictEqual(h1Raw.b.includes('1,2'), true)
assert.strictEqual(h1Raw.c.includes(''), true)
assert.strictEqual(h1Raw.d.includes(''), true)
assert.strictEqual(h1Raw.e.includes('1'), true)
assert.strictEqual(h1Raw.f.includes('1,2'), true)
assert.strictEqual(h1Raw.g.includes('[object Object]'), true)
assert.strictEqual(h1Raw.h.includes('undefined'), true)
assert.strictEqual(h1Raw.i.includes('null'), true)
assert.strictEqual(h1Raw.j.includes('NaN'), true)
assert.strictEqual(h1Raw.k.includes('true'), true)
assert.strictEqual(h1Raw.l.includes('false'), true)
assert.strictEqual(h1Raw.m.includes('test'), true)
assert.strictEqual(h1Raw.n.includes('1,2'), true)
assert.strictEqual(h1Raw.n.includes('3,4'), true)

assert.strictEqual(h1Raw.z, undefined)
})

xit('should wrap headers', () => {
it.skip('should wrap headers', () => {
const h1 = new Headers({
a: '1'
})
Expand All @@ -221,16 +248,16 @@ describe('Headers', () => {
h3.append('a', '2')
const h3Raw = h3.raw()

expect(h1Raw.a).to.include('1')
expect(h1Raw.a).to.not.include('2')
assert.strictEqual(h1Raw.a.includes('1'), true)
assert.strictEqual(h1Raw.a.includes('2'), false)

expect(h2Raw.a).to.include('1')
expect(h2Raw.a).to.not.include('2')
expect(h2Raw.b).to.include('1')
assert.strictEqual(h2Raw.a.includes('1'), true)
assert.strictEqual(h2Raw.a.includes('2'), false)
assert.strictEqual(h2Raw.b.includes('1'), true)

expect(h3Raw.a).to.include('1')
expect(h3Raw.a).to.include('2')
expect(h3Raw.b).to.include('1')
assert.strictEqual(h3Raw.a.includes('1'), true)
assert.strictEqual(h3Raw.a.includes('2'), true)
assert.strictEqual(h3Raw.b.includes('1'), true)
})

it('should accept headers as an iterable of tuples', () => {
Expand All @@ -241,33 +268,33 @@ describe('Headers', () => {
['b', '2'],
['a', '3']
])
expect(headers.get('a')).to.equal('1, 3')
expect(headers.get('b')).to.equal('2')
assert.strictEqual(headers.get('a'), '1, 3')
assert.strictEqual(headers.get('b'), '2')

headers = new Headers([
new Set(['a', '1']),
['b', '2'],
new Map([['a', null], ['3', null]]).keys()
])
expect(headers.get('a')).to.equal('1, 3')
expect(headers.get('b')).to.equal('2')
assert.strictEqual(headers.get('a'), '1, 3')
assert.strictEqual(headers.get('b'), '2')

headers = new Headers(new Map([
['a', '1'],
['b', '2']
]))
expect(headers.get('a')).to.equal('1')
expect(headers.get('b')).to.equal('2')
assert.strictEqual(headers.get('a'), '1')
assert.strictEqual(headers.get('b'), '2')
})

it('should throw a TypeError if non-tuple exists in a headers initializer', () => {
expect(() => new Headers([['b', '2', 'huh?']])).to.throw(TypeError)
expect(() => new Headers(['b2'])).to.throw(TypeError)
expect(() => new Headers('b2')).to.throw(TypeError)
expect(() => new Headers({ [Symbol.iterator]: 42 })).to.throw(TypeError)
assert.throws(() => new Headers([['b', '2', 'huh?']]), TypeError)
assert.throws(() => new Headers(['b2']), TypeError)
assert.throws(() => new Headers('b2'), TypeError)
assert.throws(() => new Headers({ [Symbol.iterator]: 42 }), TypeError)
})

xit('should use a custom inspect function', () => {
it.skip('should use a custom inspect function', () => {
const headers = new Headers([
['Host', 'thehost'],
['Host', 'notthehost'],
Expand All @@ -277,6 +304,6 @@ describe('Headers', () => {
])

// eslint-disable-next-line quotes
expect(format(headers)).to.equal("{ a: [ '1', '3' ], b: '2', host: 'thehost' }")
assert.strictEqual(format(headers), "{ a: [ '1', '3' ], b: '2', host: 'thehost' }")
})
})
Loading

0 comments on commit 89448eb

Please sign in to comment.