From 2d3c04c2579f5e7332ebd44cedbab907aa13829e Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 16:24:48 -0400 Subject: [PATCH 1/7] 4.6.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 38e78f2..b1bd336 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@everymundo/http-client", - "version": "4.5.1", + "version": "4.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@everymundo/http-client", - "version": "4.5.1", + "version": "4.6.0", "license": "MIT", "devDependencies": { "c8": "^10.1.2", diff --git a/package.json b/package.json index 998c2eb..bbd9b72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@everymundo/http-client", - "version": "4.5.1", + "version": "4.6.0", "description": "This is a helper to perform POST requests using promises and no external dependencies", "main": "index.js", "directories": { From d1f9f966bbfb66f5835c3cef4527e0605cd424d1 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 17:56:17 -0400 Subject: [PATCH 2/7] Add .js to require calls to easy the transition to esm --- classes/Endpoint.class.js | 2 +- classes/Headers.class.js | 2 +- classes/Response.class.js | 2 +- lib/fetch.js | 4 +-- lib/parse-endpoints.js | 2 +- lib/parse-url.js | 36 +----------------------- lib/promise-data-to-refactored.js | 20 ++++++------- lib/promise-get.js | 2 +- test/classes/BasicEndpoint.class.test.js | 4 +-- test/classes/Endpoint.class.test.js | 6 ++-- test/classes/GetEndpoint.class.test.js | 2 +- test/classes/PostEndpoint.class.test.js | 2 +- test/lib/add-query-to-path.test.js | 2 +- test/lib/fetch.test.js | 4 +-- test/lib/get-data-from-xdata.test.js | 2 +- test/lib/object-polyfill.test.js | 2 +- test/lib/parse-url.test.js | 4 +-- test/lib/promise-get.test.js | 10 ++++--- test/lib/set-headers.test.js | 2 +- test/lib/url-to-endpoint.test.js | 4 +-- 20 files changed, 41 insertions(+), 73 deletions(-) diff --git a/classes/Endpoint.class.js b/classes/Endpoint.class.js index 89e6015..de49708 100644 --- a/classes/Endpoint.class.js +++ b/classes/Endpoint.class.js @@ -1,5 +1,5 @@ const BasicEndpoint = require('./BasicEndpoint.class.js') -const lib = require('../lib/promise-data-to') +const lib = require('../lib/promise-data-to.js') class Endpoint extends BasicEndpoint { post (data, options) { return this.constructor.post(this, data, options) } diff --git a/classes/Headers.class.js b/classes/Headers.class.js index 73df359..012d761 100644 --- a/classes/Headers.class.js +++ b/classes/Headers.class.js @@ -1,4 +1,4 @@ -require('../lib/object-polyfill')(Object) +require('../lib/object-polyfill.js')(Object) class Headers extends Map { constructor (entries) { diff --git a/classes/Response.class.js b/classes/Response.class.js index a8fc205..e6ff108 100644 --- a/classes/Response.class.js +++ b/classes/Response.class.js @@ -1,4 +1,4 @@ -const { decompressBuffer } = require('../lib/decompress-buffer') +const { decompressBuffer } = require('../lib/decompress-buffer.js') class Response { code = undefined diff --git a/lib/fetch.js b/lib/fetch.js index 02bac3d..396d886 100644 --- a/lib/fetch.js +++ b/lib/fetch.js @@ -1,5 +1,5 @@ -const Endpoint = require('../classes/Endpoint.class') -const httpClient = require('../') +const Endpoint = require('../classes/Endpoint.class.js') +const httpClient = require('../index.js') const fetch = (url, options = {}) => { const { diff --git a/lib/parse-endpoints.js b/lib/parse-endpoints.js index badc3a9..c76f47c 100644 --- a/lib/parse-endpoints.js +++ b/lib/parse-endpoints.js @@ -1,6 +1,6 @@ 'use strict' -const { Endpoint } = require('../classes/Endpoint.class') +const { Endpoint } = require('../classes/Endpoint.class.js') const validateEndpointProtocol = endpointKey => (urlString) => { if (/^https?:/.test(urlString)) return true diff --git a/lib/parse-url.js b/lib/parse-url.js index 5e40406..09f3aac 100644 --- a/lib/parse-url.js +++ b/lib/parse-url.js @@ -1,35 +1 @@ -/* -const parseUrlOld = (urlString) => { - const u = new url.URL(urlString) - const result = { - href: u.href, - origin: u.origin, - protocol: u.protocol, - username: u.username, - password: u.password, - host: u.host, - hostname: u.hostname, - port: u.port, - pathname: u.pathname, - search: u.search, - searchParams: u.searchParams, - hash: u.hash, - slashes: true, - - get auth () { - if (this.username) { - if (this.password) return `${this.username}:${this.password}` - - return `${this.username}` - } - - return `${this.password}` - }, - get query () { return this.search.slice(1) }, - get path () { return `${this.pathname}${this.search}` } - } - - return result -} */ - -module.exports = require('../classes/EMUrl.class') +module.exports = require('../classes/EMUrl.class.js') diff --git a/lib/promise-data-to-refactored.js b/lib/promise-data-to-refactored.js index 69ccba8..c85a973 100644 --- a/lib/promise-data-to-refactored.js +++ b/lib/promise-data-to-refactored.js @@ -1,15 +1,15 @@ 'use strict' -const Endpoint = require('../classes/Endpoint.class') -const Headers = require('../classes/Headers.class') -const RequestError = require('../classes/Headers.class') -const logr = require('../lib/logr.js') -// const { addQueryToPath } = require('../lib/add-query-to-path') -const { simulatedResponse } = require('../lib/simulate-response') -// const { setResTxt } = require('../lib/set-response-text') -const { decompressBuffer } = require('../lib/decompress-buffer') -const { getDataFromXData } = require('../lib/get-data-from-xdata') -const { setHeaders } = require('../lib/set-headers') +const Endpoint = require('../classes/Endpoint.class.js') +const Headers = require('../classes/Headers.class.js') +const RequestError = require('../classes/Headers.class.js') +const logr = require('../lib/logr.js.js') +// const { addQueryToPath } = require('../lib/add-query-to-path.js') +const { simulatedResponse } = require('../lib/simulate-response.js') +// const { setResTxt } = require('../lib/set-response-text.js') +const { decompressBuffer } = require('../lib/decompress-buffer.js') +const { getDataFromXData } = require('../lib/get-data-from-xdata.js') +const { setHeaders } = require('../lib/set-headers.js') const SIMULATE = +process.env.SIMULATE const MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3 diff --git a/lib/promise-get.js b/lib/promise-get.js index d466193..91794e5 100644 --- a/lib/promise-get.js +++ b/lib/promise-get.js @@ -1,4 +1,4 @@ -const httpClient = require('../lib/promise-data-to') +const httpClient = require('../lib/promise-data-to.js') const promiseGet = async (_endpoint) => { const endpoint = httpClient.getEndpoint(_endpoint) diff --git a/test/classes/BasicEndpoint.class.test.js b/test/classes/BasicEndpoint.class.test.js index 8f23a6d..c276030 100644 --- a/test/classes/BasicEndpoint.class.test.js +++ b/test/classes/BasicEndpoint.class.test.js @@ -8,8 +8,8 @@ const { expect } = require('chai') const http = require('http') describe('classes/Endpoint', () => { - const BasicEndpoint = require('../../classes/BasicEndpoint.class') - const EMUrl = require('../../classes/EMUrl.class') + const BasicEndpoint = require('../../classes/BasicEndpoint.class.js') + const EMUrl = require('../../classes/EMUrl.class.js') // eslint-disable-next-line one-var-declaration-per-line let box diff --git a/test/classes/Endpoint.class.test.js b/test/classes/Endpoint.class.test.js index 257c7b1..4f06845 100644 --- a/test/classes/Endpoint.class.test.js +++ b/test/classes/Endpoint.class.test.js @@ -6,9 +6,9 @@ describe('classes/Endpoint', () => { const sinon = require('sinon') const { expect } = require('chai') - const { Endpoint } = require('../../classes/Endpoint.class') - const EMUrl = require('../../classes/EMUrl.class') - const httpClient = require('../../lib/promise-data-to') + const { Endpoint } = require('../../classes/Endpoint.class.js') + const EMUrl = require('../../classes/EMUrl.class.js') + const httpClient = require('../../lib/promise-data-to.js') // eslint-disable-next-line one-var-declaration-per-line let box diff --git a/test/classes/GetEndpoint.class.test.js b/test/classes/GetEndpoint.class.test.js index d76d023..e78c0d4 100644 --- a/test/classes/GetEndpoint.class.test.js +++ b/test/classes/GetEndpoint.class.test.js @@ -9,7 +9,7 @@ describe('GetEndpoint', () => { // eslint-disable-next-line one-var-declaration-per-line // let box - const { Endpoint, GetEndpoint } = require('../../classes/Endpoint.class') + const { Endpoint, GetEndpoint } = require('../../classes/Endpoint.class.js') // beforeEach(() => { box = sinon.createSandbox() }) // afterEach(() => { box.restore() }) diff --git a/test/classes/PostEndpoint.class.test.js b/test/classes/PostEndpoint.class.test.js index 6c4def2..37d749f 100644 --- a/test/classes/PostEndpoint.class.test.js +++ b/test/classes/PostEndpoint.class.test.js @@ -9,7 +9,7 @@ describe('PostEndpoint', () => { // eslint-disable-next-line one-var-declaration-per-line // let box - const { Endpoint, PostEndpoint } = require('../../classes/Endpoint.class') + const { Endpoint, PostEndpoint } = require('../../classes/Endpoint.class.js') // beforeEach(() => { box = sinon.createSandbox() }) // afterEach(() => { box.restore() }) diff --git a/test/lib/add-query-to-path.test.js b/test/lib/add-query-to-path.test.js index 33bd47a..81c0bdb 100644 --- a/test/lib/add-query-to-path.test.js +++ b/test/lib/add-query-to-path.test.js @@ -7,7 +7,7 @@ describe('lib/addQueryToPath.js', () => { const sinon = require('sinon') const { expect } = require('chai') - const loadLib = () => require('../../lib/add-query-to-path') + const loadLib = () => require('../../lib/add-query-to-path.js') // eslint-disable-next-line one-var-declaration-per-line let box diff --git a/test/lib/fetch.test.js b/test/lib/fetch.test.js index e5693e8..9a97efa 100644 --- a/test/lib/fetch.test.js +++ b/test/lib/fetch.test.js @@ -9,8 +9,8 @@ describe('lib/fetch.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box - const { Endpoint } = require('../../classes/Endpoint.class') - const httpClient = require('../../lib/promise-data-to') + const { Endpoint } = require('../../classes/Endpoint.class.js') + const httpClient = require('../../lib/promise-data-to.js') beforeEach(() => { box = sinon.createSandbox() diff --git a/test/lib/get-data-from-xdata.test.js b/test/lib/get-data-from-xdata.test.js index 54662cc..377414e 100644 --- a/test/lib/get-data-from-xdata.test.js +++ b/test/lib/get-data-from-xdata.test.js @@ -8,7 +8,7 @@ describe('lib/get-data-from-xdata.js', () => { const sinon = require('sinon') const { expect } = require('chai') - const loadLib = () => require('../../lib/get-data-from-xdata') + const loadLib = () => require('../../lib/get-data-from-xdata.js') // eslint-disable-next-line one-var-declaration-per-line let box diff --git a/test/lib/object-polyfill.test.js b/test/lib/object-polyfill.test.js index 1578c7f..511d25d 100644 --- a/test/lib/object-polyfill.test.js +++ b/test/lib/object-polyfill.test.js @@ -6,7 +6,7 @@ describe('lib/fetch.js', () => { const { expect } = require('chai') - const objectPolyfill = require('../../lib/object-polyfill') + const objectPolyfill = require('../../lib/object-polyfill.js') describe('objectPolyfill', async () => { it('should set the property fromEntries', async () => { diff --git a/test/lib/parse-url.test.js b/test/lib/parse-url.test.js index fa1869e..f52a17c 100644 --- a/test/lib/parse-url.test.js +++ b/test/lib/parse-url.test.js @@ -8,8 +8,8 @@ describe('lib/parse-url', () => { const { expect } = require('chai') // eslint-disable-next-line one-var-declaration-per-line let box - const { parseUrl } = require('../../lib/parse-url') - const EMUrl = require('../../classes/EMUrl.class') + const { parseUrl } = require('../../lib/parse-url.js') + const EMUrl = require('../../classes/EMUrl.class.js') beforeEach(() => { box = sinon.createSandbox() diff --git a/test/lib/promise-get.test.js b/test/lib/promise-get.test.js index b27dee1..abf8d41 100644 --- a/test/lib/promise-get.test.js +++ b/test/lib/promise-get.test.js @@ -2,12 +2,14 @@ /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +const sinon = require('sinon') +const { expect } = require('chai') -describe('lib/promise-get.js', () => { - const { parseUrl } = require('../../lib/parse-url') - const sinon = require('sinon') - const { expect } = require('chai') +const { parseUrl } = require('../../lib/parse-url.js') +const promiseDataToLib = require('../../lib/promise-data-to.js') +const lib = require('../../index.js') +describe('lib/promise-get.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box diff --git a/test/lib/set-headers.test.js b/test/lib/set-headers.test.js index 39309c2..1099cb3 100644 --- a/test/lib/set-headers.test.js +++ b/test/lib/set-headers.test.js @@ -14,7 +14,7 @@ describe('lib/set-headers.js', () => { afterEach(() => { box.restore() }) describe('#setHeaders', () => { - const { setHeaders } = require('../../lib/set-headers') + const { setHeaders } = require('../../lib/set-headers.js') context('When content-type is not set', () => { it('should no longer set content-type to application/json', () => { diff --git a/test/lib/url-to-endpoint.test.js b/test/lib/url-to-endpoint.test.js index dec3fd5..dd98391 100644 --- a/test/lib/url-to-endpoint.test.js +++ b/test/lib/url-to-endpoint.test.js @@ -8,8 +8,8 @@ describe('lib/url-to-endpoint', () => { const { expect } = require('chai') // eslint-disable-next-line one-var-declaration-per-line let box - const { urlToEndpoint } = require('../../index') - const { Endpoint } = require('../../classes/Endpoint.class') + const { urlToEndpoint } = require('../../index.js') + const { Endpoint } = require('../../classes/Endpoint.class.js') beforeEach(() => { box = sinon.createSandbox() From 98503cbe1ae472aaa32a5c070c43694cc86b3a03 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 17:57:29 -0400 Subject: [PATCH 3/7] Replace context by describe on all tests to enable bun test --- test/classes/Endpoint.class.test.js | 4 ++-- test/index.test.js | 4 ++-- test/lib/add-query-to-path.test.js | 12 +++++------ test/lib/get-data-from-xdata.test.js | 20 +++++++++---------- test/lib/parse-endpoints.js.test.js | 24 +++++++++++----------- test/lib/parse-url.test.js | 10 +++++----- test/lib/promise-get.test.js | 6 +----- test/lib/set-headers.test.js | 12 +++++------ test/lib/simulate-response.test.js | 2 +- test/lib/url-to-endpoint.test.js | 30 ++++++++++++++-------------- 10 files changed, 60 insertions(+), 64 deletions(-) diff --git a/test/classes/Endpoint.class.test.js b/test/classes/Endpoint.class.test.js index 4f06845..4c7a5fa 100644 --- a/test/classes/Endpoint.class.test.js +++ b/test/classes/Endpoint.class.test.js @@ -32,7 +32,7 @@ describe('classes/Endpoint', () => { const inputUrlString = 'https://head-url' const expectedUrlString = 'https://head-url/' - context('instance httpMethods', () => { + describe('instance httpMethods', () => { beforeEach(() => { box.stub(httpClient, 'promiseDataTo') }) // read methods ;['get', 'head'].forEach((method) => { @@ -124,7 +124,7 @@ describe('classes/Endpoint', () => { }) }) - context.skip('static httpMethods', () => { + describe.skip('static httpMethods', () => { beforeEach(() => { box.stub(httpClient, 'promiseDataTo') }) describe('#head', () => { diff --git a/test/index.test.js b/test/index.test.js index ebaf96f..25389ff 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -16,7 +16,7 @@ describe('index.js', () => { beforeEach(() => { box = sinon.createSandbox() }) afterEach(() => { box.restore() }) - context('exported keys and types', () => { + describe('exported keys and types', () => { const expected = { Endpoint: Function, GetEndpoint: Function, @@ -49,7 +49,7 @@ describe('index.js', () => { } }) - context('httpMethods', () => { + describe('httpMethods', () => { beforeEach(() => { box.stub(httpClient, 'promiseDataTo') }) describe('#head', () => { diff --git a/test/lib/add-query-to-path.test.js b/test/lib/add-query-to-path.test.js index 81c0bdb..0c98b40 100644 --- a/test/lib/add-query-to-path.test.js +++ b/test/lib/add-query-to-path.test.js @@ -18,7 +18,7 @@ describe('lib/addQueryToPath.js', () => { describe('#addQueryToPath', () => { const { addQueryToPath } = loadLib() - context('when query is empty', () => { + describe('when query is empty', () => { it('should return inputPath', () => { const query = undefined const path = '/something' @@ -29,7 +29,7 @@ describe('lib/addQueryToPath.js', () => { }) }) - context('when query is an object', () => { + describe('when query is an object', () => { it('should return the correct concatenated string', () => { const query = { var1: 'value1', var2: 'v2' } const path = '/something' @@ -40,8 +40,8 @@ describe('lib/addQueryToPath.js', () => { }) }) - context('when query is a string', () => { - context('that already includes ?', () => { + describe('when query is a string', () => { + describe('that already includes ?', () => { it('should return the correct concatenated string', () => { const query = '?name=daniel&descr=awesome' const path = '/something' @@ -52,7 +52,7 @@ describe('lib/addQueryToPath.js', () => { }) }) - context('that DOES NOT include ?', () => { + describe('that DOES NOT include ?', () => { it('should return the correct concatenated string', () => { const query = 'name=daniel&descr=awesome' const path = '/something' @@ -64,7 +64,7 @@ describe('lib/addQueryToPath.js', () => { }) }) - context('when path already includes the ?', () => { + describe('when path already includes the ?', () => { it('should return the correct concatenated string', () => { const query = '?name=daniel&descr=awesome' const path = '/something?crazy=true' diff --git a/test/lib/get-data-from-xdata.test.js b/test/lib/get-data-from-xdata.test.js index 377414e..9e8affe 100644 --- a/test/lib/get-data-from-xdata.test.js +++ b/test/lib/get-data-from-xdata.test.js @@ -19,8 +19,8 @@ describe('lib/get-data-from-xdata.js', () => { describe('#getDataFromXData', () => { const { getDataFromXData } = loadLib() - context('Without Compression', () => { - context('when the input data is a String', () => { + describe('Without Compression', () => { + describe('when the input data is a String', () => { it('should return the exact same string', () => { const input = 'simple string' @@ -30,7 +30,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is an Array', () => { + describe('when the input data is an Array', () => { it('should return a json representation of it', () => { const input = [1, 2, 3, 4] const expected = JSON.stringify(input) @@ -40,7 +40,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is a plain Object', () => { + describe('when the input data is a plain Object', () => { it('should return a json representation of it', () => { const input = { a: 1, b: 2, c: 3, d: 4 } const expected = JSON.stringify(input) @@ -50,7 +50,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is a Buffer', () => { + describe('when the input data is a Buffer', () => { it('should return the very same Buffer', () => { const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }') const result = getDataFromXData(input) @@ -61,8 +61,8 @@ describe('lib/get-data-from-xdata.js', () => { }); ['gzip', 'deflate'].forEach((compress) => { - context(`WITH ${compress} Compression`, () => { - context('when the input data is a String', () => { + describe(`WITH ${compress} Compression`, () => { + describe('when the input data is a String', () => { it('should return the GZIPPED version of the given string', () => { const input = 'simple string' const expected = zlib[`${compress}Sync`](input).toString('base64') @@ -73,7 +73,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is an Array', () => { + describe('when the input data is an Array', () => { it('should return the GZIPPED version of the json representation of it', () => { const input = [1, 2, 3, 4] const expected = zlib[`${compress}Sync`](JSON.stringify(input)).toString('base64') @@ -84,7 +84,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is a plain Object', () => { + describe('when the input data is a plain Object', () => { it('should return the GZIPPED version of the json representation of it', () => { const input = { a: 1, b: 2, c: 3, d: 4 } const expected = zlib[`${compress}Sync`](JSON.stringify(input)).toString('base64') @@ -95,7 +95,7 @@ describe('lib/get-data-from-xdata.js', () => { }) }) - context('when the input data is a Buffer', () => { + describe('when the input data is a Buffer', () => { it('should return the very same Buffer', () => { const input = Buffer.from('{ a: 1, b: 2, c: 3, d: 4 }') const expected = zlib[`${compress}Sync`](input).toString('base64') diff --git a/test/lib/parse-endpoints.js.test.js b/test/lib/parse-endpoints.js.test.js index 82e964b..ffc86d0 100644 --- a/test/lib/parse-endpoints.js.test.js +++ b/test/lib/parse-endpoints.js.test.js @@ -49,11 +49,11 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('parseEndpoints', () => { + describe('parseEndpoints', () => { const { parseEndpoints } = loadLib() - context('when receiving and INVALID url =>', () => { - context('invalid protocol', () => { + describe('when receiving and INVALID url =>', () => { + describe('invalid protocol', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT') .value('tcp://test.com/some/path') @@ -67,7 +67,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('when not receiving any argument =>', () => { + describe('when not receiving any argument =>', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') }) @@ -93,7 +93,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('when receiving a regexp without grouping', () => { + describe('when receiving a regexp without grouping', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') }) @@ -119,7 +119,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http url with credentials and no headers', () => { + describe('http url with credentials and no headers', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT').value('http://Bearer:token@test.com/some/path') }) @@ -146,7 +146,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http url with credentials AND with headers', () => { + describe('http url with credentials AND with headers', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT') .value('http://Bearer:token@test.com/some/path') @@ -178,7 +178,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http url with keep-alive headers', () => { + describe('http url with keep-alive headers', () => { beforeEach(() => { if (!('KEEP_ENDPOINT' in process.env)) process.env.KEEP_ENDPOINT = undefined box.stub(process.env, 'KEEP_ENDPOINT') @@ -222,7 +222,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http url with keep-alive headers', () => { + describe('http url with keep-alive headers', () => { beforeEach(() => { if (!('KEEP_ENDPOINT' in process.env)) process.env.KEEP_ENDPOINT = undefined box.stub(process.env, 'KEEP_ENDPOINT') @@ -266,7 +266,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http simple url with headers', () => { + describe('http simple url with headers', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT') .value('http://test.com/some/path') @@ -297,7 +297,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('http simple url but INVALID headers', () => { + describe('http simple url but INVALID headers', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT') .value('http://test.com/some/path') @@ -312,7 +312,7 @@ describe('lib/parse-endpoints.js', () => { }) }) - context('Regexp too generic and matches multiple ENV Vars', () => { + describe('Regexp too generic and matches multiple ENV Vars', () => { beforeEach(() => { box.stub(process.env, 'MAIN_ENDPOINT') .value('http://test.com/some/path') diff --git a/test/lib/parse-url.test.js b/test/lib/parse-url.test.js index f52a17c..37890f3 100644 --- a/test/lib/parse-url.test.js +++ b/test/lib/parse-url.test.js @@ -17,9 +17,9 @@ describe('lib/parse-url', () => { afterEach(() => { box.restore() }) - context('parseUrl', () => { - context('when receiving and INVALID url =>', () => { - context('empty url', () => { + describe('parseUrl', () => { + describe('when receiving and INVALID url =>', () => { + describe('empty url', () => { it('show throw an error', () => { const caller = () => parseUrl() @@ -28,7 +28,7 @@ describe('lib/parse-url', () => { }) }) - context('when receiving valid url =>', () => { + describe('when receiving valid url =>', () => { it('should return an object with the expected properties', () => { const result = parseUrl('http://test.com/some/path?a=100&b=200') @@ -59,7 +59,7 @@ describe('lib/parse-url', () => { }) }) - context('#auth', () => { + describe('#auth', () => { it('should return user:pass when having both', () => { const parsed = parseUrl('http://user:pass@test.com/some/path') diff --git a/test/lib/promise-get.test.js b/test/lib/promise-get.test.js index abf8d41..db33184 100644 --- a/test/lib/promise-get.test.js +++ b/test/lib/promise-get.test.js @@ -16,11 +16,7 @@ describe('lib/promise-get.js', () => { beforeEach(() => { box = sinon.createSandbox() }) afterEach(() => { box.restore() }) - context('promiseGet', () => { - const promiseDataToLib = require('../../lib/promise-data-to') - // const { promiseGet } = require('../lib/promise-get') - const lib = require('../../index') - + describe('promiseGet', () => { beforeEach(() => { box.stub(promiseDataToLib, 'promiseDataTo').callsFake((...args) => Promise.resolve(args)) }) diff --git a/test/lib/set-headers.test.js b/test/lib/set-headers.test.js index 1099cb3..4492ced 100644 --- a/test/lib/set-headers.test.js +++ b/test/lib/set-headers.test.js @@ -16,7 +16,7 @@ describe('lib/set-headers.js', () => { describe('#setHeaders', () => { const { setHeaders } = require('../../lib/set-headers.js') - context('When content-type is not set', () => { + describe('When content-type is not set', () => { it('should no longer set content-type to application/json', () => { const headers = {} @@ -26,7 +26,7 @@ describe('lib/set-headers.js', () => { }) }) - context('When content-type is preset', () => { + describe('When content-type is preset', () => { it('should not change the content-type sent', () => { const headers = { 'content-type': 'application/something-else' } @@ -36,7 +36,7 @@ describe('lib/set-headers.js', () => { }) }) - context('When data is not passed', () => { + describe('When data is not passed', () => { it('should not set content-length', () => { const headers = { 'content-type': 'application/something-else' } @@ -46,7 +46,7 @@ describe('lib/set-headers.js', () => { }) }) - context('When data is passed', () => { + describe('When data is passed', () => { it('should set content-length with its byte length', () => { const headers = {} const data = '{"name":"Daniel San","code":"รง"}' @@ -57,7 +57,7 @@ describe('lib/set-headers.js', () => { }) }) - context('When compress is NOT present', () => { + describe('When compress is NOT present', () => { it('should not set x-compression', () => { const headers = {} @@ -67,7 +67,7 @@ describe('lib/set-headers.js', () => { }) }) - context('When compress is present', () => { + describe('When compress is present', () => { it('should set x-compression its value', () => { const headers = {} const compress = 'gzip' diff --git a/test/lib/simulate-response.test.js b/test/lib/simulate-response.test.js index 4af37f7..e3426ce 100644 --- a/test/lib/simulate-response.test.js +++ b/test/lib/simulate-response.test.js @@ -28,7 +28,7 @@ describe('lib/simulatedResponse.js', () => { describe('#simulatedResponse', () => { const { simulatedResponse } = testFile - context('when called with an array', () => { + describe('when called with an array', () => { it('should return the expected object with statusCode 222', () => { const start = new Date() const endpoint = 'http://test.com' diff --git a/test/lib/url-to-endpoint.test.js b/test/lib/url-to-endpoint.test.js index dd98391..43965e4 100644 --- a/test/lib/url-to-endpoint.test.js +++ b/test/lib/url-to-endpoint.test.js @@ -17,8 +17,8 @@ describe('lib/url-to-endpoint', () => { afterEach(() => { box.restore() }) - context('urlToEndpoint', () => { - context('when receiving and INVALID url =>', () => { + describe('urlToEndpoint', () => { + describe('when receiving and INVALID url =>', () => { it('show throw an error', () => { const caller = () => urlToEndpoint() @@ -26,8 +26,8 @@ describe('lib/url-to-endpoint', () => { }) }) - context('when receiving valid url =>', () => { - context('with no headers', () => { + describe('when receiving valid url =>', () => { + describe('with no headers', () => { it('should return an object with an object type Endpoint', () => { const result = urlToEndpoint('http://test.com/some/path?a=100&b=200') @@ -35,7 +35,7 @@ describe('lib/url-to-endpoint', () => { }) }) - context('with no headers', () => { + describe('with no headers', () => { it('should return an object with an object type Endpoint', () => { const result = urlToEndpoint('http://user:pass@test.com/some/path', { 'content-type': 'text/plain' }) @@ -47,8 +47,8 @@ describe('lib/url-to-endpoint', () => { }) }) - context('when receiving and INVALID url =>', () => { - context('empty url', () => { + describe('when receiving and INVALID url =>', () => { + describe('empty url', () => { it('show throw an error', () => { const caller = () => urlToEndpoint() @@ -56,7 +56,7 @@ describe('lib/url-to-endpoint', () => { }) }) - /* context('invalid protocol', () => { + /* describe('invalid protocol', () => { it('show throw an error', () => { const caller = () => urlToEndpoint('tcp://test.com') @@ -65,10 +65,10 @@ describe('lib/url-to-endpoint', () => { }) */ }) - context('when receiving valid url =>', () => { + describe('when receiving valid url =>', () => { const http = require('http') - context('valid simple http url and no headers', () => { + describe('valid simple http url and no headers', () => { it('should return an object with the expected properties', () => { const result = urlToEndpoint('http://test.com/some/path') @@ -88,7 +88,7 @@ describe('lib/url-to-endpoint', () => { }) }) - context('http url with credentials and no headers', () => { + describe('http url with credentials and no headers', () => { it('should return an object with the expected properties', () => { const result = urlToEndpoint('http://Bearer:token@test.com/some/path') const expectedHeaders = { @@ -117,7 +117,7 @@ describe('lib/url-to-endpoint', () => { }) }) - context('http url with credentials AND with headers', () => { + describe('http url with credentials AND with headers', () => { it('should return an object with the expected properties', () => { const headers = { 'content-type': 'application/xml' } const endpointUrl = 'http://Bearer:token@test.com/some/path' @@ -150,7 +150,7 @@ describe('lib/url-to-endpoint', () => { }) }) - context('http simple url with headers', () => { + describe('http simple url with headers', () => { it('should return an object with the expected properties', () => { const headers = { 'content-type': 'application/xml' } const endpointUrl = 'http://test.com/some/path' @@ -175,7 +175,7 @@ describe('lib/url-to-endpoint', () => { } }) - context('when content-encoding is passed as gzip', () => { + describe('when content-encoding is passed as gzip', () => { it('should return compress as "gzip"', () => { const headers = { 'x-compression': 'gzip' } const endpointUrl = 'http://test.com/some/path' @@ -202,7 +202,7 @@ describe('lib/url-to-endpoint', () => { }) }) - context('http simple url but INVALID headers', () => { + describe('http simple url but INVALID headers', () => { it('should return an object with the expected properties', () => { const headers = '' const endpoint = 'http://test.com/some/path' From 035e001c8b4514058fcfcd3eb7c793af5de9a26b Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 20:55:59 -0400 Subject: [PATCH 4/7] Convert all test/lib to mjs --- test/{index.test.js => index.test.mjs} | 11 +++----- ...ath.test.js => add-query-to-path.test.mjs} | 13 +++++----- test/lib/{fetch.test.js => fetch.test.mjs} | 12 ++++----- ...a.test.js => get-data-from-xdata.test.mjs} | 16 +++++------- test/lib/{logr.test.js => logr.test.mjs} | 6 ++--- ...yfill.test.js => object-polyfill.test.mjs} | 9 +++---- ...ts.js.test.js => parse-endpoints.test.mjs} | 25 ++++++++---------- .../{parse-url.test.js => parse-url.test.mjs} | 19 +++++++------- ...ta-to.test.js => promise-data-to.test.mjs} | 26 +++++++++---------- ...omise-get.test.js => promise-get.test.mjs} | 16 +++++------- ...t-headers.test.js => set-headers.test.mjs} | 12 ++++----- ...nse.test.js => simulate-response.test.mjs} | 16 +++++------- ...point.test.js => url-to-endpoint.test.mjs} | 14 +++++----- test/test-setup.mjs | 7 +++++ 14 files changed, 90 insertions(+), 112 deletions(-) rename test/{index.test.js => index.test.mjs} (91%) rename test/lib/{add-query-to-path.test.js => add-query-to-path.test.mjs} (90%) rename test/lib/{fetch.test.js => fetch.test.mjs} (89%) rename test/lib/{get-data-from-xdata.test.js => get-data-from-xdata.test.mjs} (92%) rename test/lib/{logr.test.js => logr.test.mjs} (84%) rename test/lib/{object-polyfill.test.js => object-polyfill.test.mjs} (87%) rename test/lib/{parse-endpoints.js.test.js => parse-endpoints.test.mjs} (95%) rename test/lib/{parse-url.test.js => parse-url.test.mjs} (80%) rename test/lib/{promise-data-to.test.js => promise-data-to.test.mjs} (97%) rename test/lib/{promise-get.test.js => promise-get.test.mjs} (70%) rename test/lib/{set-headers.test.js => set-headers.test.mjs} (93%) rename test/lib/{simulate-response.test.js => simulate-response.test.mjs} (85%) rename test/lib/{url-to-endpoint.test.js => url-to-endpoint.test.mjs} (96%) create mode 100644 test/test-setup.mjs diff --git a/test/index.test.js b/test/index.test.mjs similarity index 91% rename from test/index.test.js rename to test/index.test.mjs index 25389ff..6b67eea 100644 --- a/test/index.test.js +++ b/test/index.test.mjs @@ -1,18 +1,15 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -const sinon = require('sinon') -const { expect } = require('chai') -const lib = require('../index.js') -const httpClient = require('../lib/promise-data-to.js') +import sinon from 'sinon' +import { expect } from 'chai' +import lib from '../index.js' +import httpClient from '../lib/promise-data-to.js' describe('index.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box - // const logr = require('@everymundo/simple-logr') beforeEach(() => { box = sinon.createSandbox() }) afterEach(() => { box.restore() }) diff --git a/test/lib/add-query-to-path.test.js b/test/lib/add-query-to-path.test.mjs similarity index 90% rename from test/lib/add-query-to-path.test.js rename to test/lib/add-query-to-path.test.mjs index 0c98b40..ae38be6 100644 --- a/test/lib/add-query-to-path.test.js +++ b/test/lib/add-query-to-path.test.mjs @@ -3,20 +3,19 @@ /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -describe('lib/addQueryToPath.js', () => { - const sinon = require('sinon') - const { expect } = require('chai') - - const loadLib = () => require('../../lib/add-query-to-path.js') +import { createSandbox } from 'sinon' +import { expect } from 'chai' +import fileTotest from '../../lib/add-query-to-path.js' +describe('lib/addQueryToPath.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box - beforeEach(() => { box = sinon.createSandbox() }) + beforeEach(() => { box = createSandbox() }) afterEach(() => { box.restore() }) describe('#addQueryToPath', () => { - const { addQueryToPath } = loadLib() + const { addQueryToPath } = fileTotest describe('when query is empty', () => { it('should return inputPath', () => { diff --git a/test/lib/fetch.test.js b/test/lib/fetch.test.mjs similarity index 89% rename from test/lib/fetch.test.js rename to test/lib/fetch.test.mjs index 9a97efa..a02d43b 100644 --- a/test/lib/fetch.test.js +++ b/test/lib/fetch.test.mjs @@ -1,17 +1,15 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import sinon from 'sinon' +import { expect } from 'chai' + +import { Endpoint } from '../../classes/Endpoint.class.js' +import httpClient from '../../lib/promise-data-to.js' describe('lib/fetch.js', () => { - const sinon = require('sinon') - const { expect } = require('chai') // eslint-disable-next-line one-var-declaration-per-line let box - const { Endpoint } = require('../../classes/Endpoint.class.js') - const httpClient = require('../../lib/promise-data-to.js') - beforeEach(() => { box = sinon.createSandbox() }) diff --git a/test/lib/get-data-from-xdata.test.js b/test/lib/get-data-from-xdata.test.mjs similarity index 92% rename from test/lib/get-data-from-xdata.test.js rename to test/lib/get-data-from-xdata.test.mjs index 9e8affe..a0492b6 100644 --- a/test/lib/get-data-from-xdata.test.js +++ b/test/lib/get-data-from-xdata.test.mjs @@ -1,23 +1,19 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import zlib from 'zlib' +import sinon from 'sinon' +import { expect } from 'chai' -describe('lib/get-data-from-xdata.js', () => { - const zlib = require('zlib') - const sinon = require('sinon') - const { expect } = require('chai') - - const loadLib = () => require('../../lib/get-data-from-xdata.js') +import testFile from '../../lib/get-data-from-xdata.js' - // eslint-disable-next-line one-var-declaration-per-line +describe('lib/get-data-from-xdata.js', () => { let box beforeEach(() => { box = sinon.createSandbox() }) afterEach(() => { box.restore() }) describe('#getDataFromXData', () => { - const { getDataFromXData } = loadLib() + const { getDataFromXData } = testFile describe('Without Compression', () => { describe('when the input data is a String', () => { diff --git a/test/lib/logr.test.js b/test/lib/logr.test.mjs similarity index 84% rename from test/lib/logr.test.js rename to test/lib/logr.test.mjs index f2dbe1a..d623e5c 100644 --- a/test/lib/logr.test.js +++ b/test/lib/logr.test.mjs @@ -1,10 +1,8 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -const { expect } = require('chai') -const logr = require('../../lib/logr.js') +import { expect } from 'chai' +import logr from '../../lib/logr.js' describe('lib/logr.js', () => { describe('mkLogr', async () => { diff --git a/test/lib/object-polyfill.test.js b/test/lib/object-polyfill.test.mjs similarity index 87% rename from test/lib/object-polyfill.test.js rename to test/lib/object-polyfill.test.mjs index 511d25d..b28b9e0 100644 --- a/test/lib/object-polyfill.test.js +++ b/test/lib/object-polyfill.test.mjs @@ -1,13 +1,10 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { expect } from 'chai' -describe('lib/fetch.js', () => { - const { expect } = require('chai') - - const objectPolyfill = require('../../lib/object-polyfill.js') +import objectPolyfill from '../../lib/object-polyfill.js' +describe('lib/fetch.js', () => { describe('objectPolyfill', async () => { it('should set the property fromEntries', async () => { const O = {} diff --git a/test/lib/parse-endpoints.js.test.js b/test/lib/parse-endpoints.test.mjs similarity index 95% rename from test/lib/parse-endpoints.js.test.js rename to test/lib/parse-endpoints.test.mjs index ffc86d0..68ff2a0 100644 --- a/test/lib/parse-endpoints.js.test.js +++ b/test/lib/parse-endpoints.test.mjs @@ -1,14 +1,13 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { Agent } from 'http' +import { createSandbox } from 'sinon' +import { expect } from 'chai' -describe('lib/parse-endpoints.js', () => { - const http = require('http') - const sinon = require('sinon') - const { expect } = require('chai') +import testFile from '../../lib/parse-endpoints.js' +import { parseKeepAlive, parseKeepAliveHeader } from '../../lib/parse-keep-alive-header.js' - const loadLib = () => require('../../lib/parse-endpoints.js') +describe('lib/parse-endpoints.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box @@ -19,7 +18,7 @@ describe('lib/parse-endpoints.js', () => { if (!('MAIN_ENDPOINT_HEADERS' in process.env)) process.env.MAIN_ENDPOINT_HEADERS = '' beforeEach(() => { - box = sinon.createSandbox() + box = createSandbox() box.stub(process.env, 'MAIN_ENDPOINT').value('http://test.com/some/path') }) @@ -27,7 +26,7 @@ describe('lib/parse-endpoints.js', () => { describe('#parseKeepAlive', () => { it('should return an object with the expected properties', () => { - const { parseKeepAlive } = require('../../lib/parse-keep-alive-header.js') + // const { parseKeepAlive } = require('../../lib/parse-keep-alive-header.js') const headers = { 'keep-alive': 'max=1000, timeout=1000' } const res = parseKeepAlive(headers['keep-alive']) @@ -39,8 +38,6 @@ describe('lib/parse-endpoints.js', () => { describe('#parseKeepAliveHeader', () => { it('should return an object with the expected properties', () => { - const { parseKeepAliveHeader } = require('../../lib/parse-keep-alive-header.js') - const headers = { 'keep-alive': 'max=1000, timeout=1000' } const res = parseKeepAliveHeader(headers) const expected = { max: '1000', timeout: '1000' } @@ -50,7 +47,7 @@ describe('lib/parse-endpoints.js', () => { }) describe('parseEndpoints', () => { - const { parseEndpoints } = loadLib() + const { parseEndpoints } = testFile describe('when receiving and INVALID url =>', () => { describe('invalid protocol', () => { @@ -215,7 +212,7 @@ describe('lib/parse-endpoints.js', () => { expect(result.KEEP[0].getObject()).to.deep.equal(expected.KEEP[0]) - expect(agent).to.be.instanceof(http.Agent) + expect(agent).to.be.instanceof(Agent) expect(agent.keepAlive).to.be.true expect(agent.maxSockets).to.equal(100) expect(agent.options.timeout).to.equal(1000) @@ -259,7 +256,7 @@ describe('lib/parse-endpoints.js', () => { expect(result.KEEP[0].getObject()).to.deep.equal(expected) // expect(result.KEEP[0]).to.deep.equal(expected) - expect(agent).to.be.instanceof(http.Agent) + expect(agent).to.be.instanceof(Agent) expect(agent.keepAlive).to.be.true expect(agent.maxSockets).to.equal(50) expect(agent.options.timeout).to.be.undefined diff --git a/test/lib/parse-url.test.js b/test/lib/parse-url.test.mjs similarity index 80% rename from test/lib/parse-url.test.js rename to test/lib/parse-url.test.mjs index 37890f3..688cf71 100644 --- a/test/lib/parse-url.test.js +++ b/test/lib/parse-url.test.mjs @@ -1,15 +1,14 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import sinon from 'sinon' +import { expect } from 'chai' +import urlHelper from '../../lib/parse-url.js' +import EMUrl from '../../classes/EMUrl.class.js' + describe('lib/parse-url', () => { - const sinon = require('sinon') - const { expect } = require('chai') // eslint-disable-next-line one-var-declaration-per-line let box - const { parseUrl } = require('../../lib/parse-url.js') - const EMUrl = require('../../classes/EMUrl.class.js') beforeEach(() => { box = sinon.createSandbox() @@ -21,7 +20,7 @@ describe('lib/parse-url', () => { describe('when receiving and INVALID url =>', () => { describe('empty url', () => { it('show throw an error', () => { - const caller = () => parseUrl() + const caller = () => urlHelper.parseUrl() expect(caller).to.throw(Error) }) @@ -30,7 +29,7 @@ describe('lib/parse-url', () => { describe('when receiving valid url =>', () => { it('should return an object with the expected properties', () => { - const result = parseUrl('http://test.com/some/path?a=100&b=200') + const result = urlHelper.parseUrl('http://test.com/some/path?a=100&b=200') const expected = { href: 'http://test.com/some/path?a=100&b=200', @@ -61,7 +60,7 @@ describe('lib/parse-url', () => { describe('#auth', () => { it('should return user:pass when having both', () => { - const parsed = parseUrl('http://user:pass@test.com/some/path') + const parsed = urlHelper.parseUrl('http://user:pass@test.com/some/path') const result = parsed.auth const expected = 'user:pass' @@ -70,7 +69,7 @@ describe('lib/parse-url', () => { }) it('should return only pass when not having user when having both', () => { - const parsed = parseUrl('http://pass@test.com/some/path') + const parsed = urlHelper.parseUrl('http://pass@test.com/some/path') const result = parsed.auth const expected = 'pass' diff --git a/test/lib/promise-data-to.test.js b/test/lib/promise-data-to.test.mjs similarity index 97% rename from test/lib/promise-data-to.test.js rename to test/lib/promise-data-to.test.mjs index 4076657..bec9b4f 100644 --- a/test/lib/promise-data-to.test.js +++ b/test/lib/promise-data-to.test.mjs @@ -1,18 +1,16 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -const { EventEmitter } = require('node:events') -const http = require('node:http') -const zlib = require('node:zlib') -const sinon = require('sinon') -const { expect } = require('chai') - -const { Endpoint } = require('../../classes/Endpoint.class.js') -const libSetHeaders = require('../../lib/set-headers.js') -const simulateLib = require('../../lib/simulate-response.js') -const config = require('../../lib/config.js') -const lib = require('../../lib/promise-data-to.js') +import { EventEmitter } from 'node:events' +import http from 'node:http' +import zlib from 'node:zlib' +import sinon from 'sinon' +import { expect } from 'chai' + +import { Endpoint } from '../../classes/Endpoint.class.js' +import libSetHeaders from '../../lib/set-headers.js' +import simulateLib from '../../lib/simulate-response.js' +import config from '../../lib/config.js' +import lib from '../../lib/promise-data-to.js' describe('lib/promise-data-to', () => { const noop = () => { } @@ -192,7 +190,7 @@ describe('lib/promise-data-to', () => { const expectedData = JSON.stringify(data) const protoConfig = Endpoint.clone(endpoint) protoConfig.protocol = 'http:' - protoConfig.query = { name: 'Daniel', features: ['awesome', 'handsome'] } + // protoConfig.query = { name: 'Daniel', features: ['awesome', 'handsome'] } return lib.promiseDataTo(protoConfig, data) .then((stats) => { diff --git a/test/lib/promise-get.test.js b/test/lib/promise-get.test.mjs similarity index 70% rename from test/lib/promise-get.test.js rename to test/lib/promise-get.test.mjs index db33184..73c130f 100644 --- a/test/lib/promise-get.test.js +++ b/test/lib/promise-get.test.mjs @@ -1,13 +1,11 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -const sinon = require('sinon') -const { expect } = require('chai') +import sinon from 'sinon' +import { expect } from 'chai' -const { parseUrl } = require('../../lib/parse-url.js') -const promiseDataToLib = require('../../lib/promise-data-to.js') -const lib = require('../../index.js') +import urlHelper from '../../lib/parse-url.js' +import promiseDataToLib from '../../lib/promise-data-to.js' +import lib from '../../index.js' describe('lib/promise-get.js', () => { // eslint-disable-next-line one-var-declaration-per-line @@ -22,7 +20,7 @@ describe('lib/promise-get.js', () => { }) it('should call promiseDataTo', async () => { - const config = parseUrl('http://password@test.com/somepath') + const config = urlHelper.parseUrl('http://password@test.com/somepath') await lib.promiseGet(config) @@ -30,7 +28,7 @@ describe('lib/promise-get.js', () => { }) it('should return method GET', async () => { - const config = parseUrl('http://test.com/somepath') + const config = urlHelper.parseUrl('http://test.com/somepath') // eslint-disable-next-line one-var-declaration-per-line const result = await lib.promiseGet(config) diff --git a/test/lib/set-headers.test.js b/test/lib/set-headers.test.mjs similarity index 93% rename from test/lib/set-headers.test.js rename to test/lib/set-headers.test.mjs index 4492ced..0092844 100644 --- a/test/lib/set-headers.test.js +++ b/test/lib/set-headers.test.mjs @@ -1,12 +1,12 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -describe('lib/set-headers.js', () => { - const sinon = require('sinon') - const { expect } = require('chai') +import sinon from 'sinon' +import { expect } from 'chai' +import fileTotest from '../../lib/set-headers.js' + +describe('lib/set-headers.js', () => { // eslint-disable-next-line one-var-declaration-per-line let box @@ -14,7 +14,7 @@ describe('lib/set-headers.js', () => { afterEach(() => { box.restore() }) describe('#setHeaders', () => { - const { setHeaders } = require('../../lib/set-headers.js') + const { setHeaders } = fileTotest describe('When content-type is not set', () => { it('should no longer set content-type to application/json', () => { diff --git a/test/lib/simulate-response.test.js b/test/lib/simulate-response.test.mjs similarity index 85% rename from test/lib/simulate-response.test.js rename to test/lib/simulate-response.test.mjs index e3426ce..cc61284 100644 --- a/test/lib/simulate-response.test.js +++ b/test/lib/simulate-response.test.mjs @@ -1,25 +1,21 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { createSandbox } from 'sinon' +import { expect } from 'chai' -describe('lib/simulatedResponse.js', () => { - const sinon = require('sinon') - - const { expect } = require('chai') - - const logr = require('@everymundo/simple-logr') +import logr from '@everymundo/simple-logr' +import testFile from '../../lib/simulate-response.js' +describe('lib/simulatedResponse.js', () => { // cleanrequire = require('@everymundo/cleanrequire'), const noop = () => { } - const testFile = require('../../lib/simulate-response.js') // eslint-disable-next-line one-var-declaration-per-line let box beforeEach(() => { - box = sinon.createSandbox() + box = createSandbox() box.stub(logr, 'info').callsFake(noop) }) diff --git a/test/lib/url-to-endpoint.test.js b/test/lib/url-to-endpoint.test.mjs similarity index 96% rename from test/lib/url-to-endpoint.test.js rename to test/lib/url-to-endpoint.test.mjs index 43965e4..76f4316 100644 --- a/test/lib/url-to-endpoint.test.js +++ b/test/lib/url-to-endpoint.test.mjs @@ -1,15 +1,15 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import http from 'node:http' +import sinon from 'sinon' +import { expect } from 'chai' + +import { urlToEndpoint } from '../../index.js' +import { Endpoint } from '../../classes/Endpoint.class.js' describe('lib/url-to-endpoint', () => { - const sinon = require('sinon') - const { expect } = require('chai') // eslint-disable-next-line one-var-declaration-per-line let box - const { urlToEndpoint } = require('../../index.js') - const { Endpoint } = require('../../classes/Endpoint.class.js') beforeEach(() => { box = sinon.createSandbox() @@ -66,8 +66,6 @@ describe('lib/url-to-endpoint', () => { }) describe('when receiving valid url =>', () => { - const http = require('http') - describe('valid simple http url and no headers', () => { it('should return an object with the expected properties', () => { const result = urlToEndpoint('http://test.com/some/path') diff --git a/test/test-setup.mjs b/test/test-setup.mjs new file mode 100644 index 0000000..037542d --- /dev/null +++ b/test/test-setup.mjs @@ -0,0 +1,7 @@ +import sinon from 'sinon' + +export const box = sinon.createSandbox() + +export default { + box +} From e98a10e02bb603784cb34db2d2030b12b321aad2 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 21:05:14 -0400 Subject: [PATCH 5/7] Convert test/classes to mjs --- ...lass.test.js => BasicEndpoint.class.test.mjs} | 13 +++++-------- ...int.class.test.js => Endpoint.class.test.mjs} | 16 +++++++--------- ....class.test.js => GetEndpoint.class.test.mjs} | 15 +++------------ ...class.test.js => PostEndpoint.class.test.mjs} | 15 +++------------ ...lass.test.js => ResponseError.class.test.mjs} | 16 ++++------------ 5 files changed, 22 insertions(+), 53 deletions(-) rename test/classes/{BasicEndpoint.class.test.js => BasicEndpoint.class.test.mjs} (88%) rename test/classes/{Endpoint.class.test.js => Endpoint.class.test.mjs} (95%) rename test/classes/{GetEndpoint.class.test.js => GetEndpoint.class.test.mjs} (60%) rename test/classes/{PostEndpoint.class.test.js => PostEndpoint.class.test.mjs} (60%) rename test/classes/{ResponseError.class.test.js => ResponseError.class.test.mjs} (84%) diff --git a/test/classes/BasicEndpoint.class.test.js b/test/classes/BasicEndpoint.class.test.mjs similarity index 88% rename from test/classes/BasicEndpoint.class.test.js rename to test/classes/BasicEndpoint.class.test.mjs index c276030..70a8950 100644 --- a/test/classes/BasicEndpoint.class.test.js +++ b/test/classes/BasicEndpoint.class.test.mjs @@ -1,16 +1,13 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import sinon from 'sinon' +import { expect } from 'chai' +import http from 'http' -const sinon = require('sinon') -const { expect } = require('chai') -const http = require('http') +import BasicEndpoint from '../../classes/BasicEndpoint.class.js' +import EMUrl from '../../classes/EMUrl.class.js' describe('classes/Endpoint', () => { - const BasicEndpoint = require('../../classes/BasicEndpoint.class.js') - const EMUrl = require('../../classes/EMUrl.class.js') - // eslint-disable-next-line one-var-declaration-per-line let box beforeEach(() => { box = sinon.createSandbox() }) diff --git a/test/classes/Endpoint.class.test.js b/test/classes/Endpoint.class.test.mjs similarity index 95% rename from test/classes/Endpoint.class.test.js rename to test/classes/Endpoint.class.test.mjs index 4c7a5fa..318f540 100644 --- a/test/classes/Endpoint.class.test.js +++ b/test/classes/Endpoint.class.test.mjs @@ -1,18 +1,16 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { createSandbox } from 'sinon' +import { expect } from 'chai' +import { Endpoint } from '../../classes/Endpoint.class.js' +import EMUrl from '../../classes/EMUrl.class.js' +import httpClient from '../../lib/promise-data-to.js' describe('classes/Endpoint', () => { - const sinon = require('sinon') - const { expect } = require('chai') - const { Endpoint } = require('../../classes/Endpoint.class.js') - const EMUrl = require('../../classes/EMUrl.class.js') - const httpClient = require('../../lib/promise-data-to.js') - // eslint-disable-next-line one-var-declaration-per-line let box - beforeEach(() => { box = sinon.createSandbox() }) + + beforeEach(() => { box = createSandbox() }) afterEach(() => { box.restore() }) describe('class Endpoint', () => { diff --git a/test/classes/GetEndpoint.class.test.js b/test/classes/GetEndpoint.class.test.mjs similarity index 60% rename from test/classes/GetEndpoint.class.test.js rename to test/classes/GetEndpoint.class.test.mjs index e78c0d4..9fb4009 100644 --- a/test/classes/GetEndpoint.class.test.js +++ b/test/classes/GetEndpoint.class.test.mjs @@ -1,19 +1,10 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { expect } from 'chai' -describe('GetEndpoint', () => { - // const sinon = require('sinon') - const { expect } = require('chai') - // eslint-disable-next-line one-var-declaration-per-line - // let box - - const { Endpoint, GetEndpoint } = require('../../classes/Endpoint.class.js') - - // beforeEach(() => { box = sinon.createSandbox() }) - // afterEach(() => { box.restore() }) +import { Endpoint, GetEndpoint } from '../../classes/Endpoint.class.js' +describe('GetEndpoint', () => { describe('class GetEndpoint', () => { it('should be an instance of Endpoint', () => { const endpoint = new GetEndpoint('http://test.com') diff --git a/test/classes/PostEndpoint.class.test.js b/test/classes/PostEndpoint.class.test.mjs similarity index 60% rename from test/classes/PostEndpoint.class.test.js rename to test/classes/PostEndpoint.class.test.mjs index 37d749f..59c0cd9 100644 --- a/test/classes/PostEndpoint.class.test.js +++ b/test/classes/PostEndpoint.class.test.mjs @@ -1,19 +1,10 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ +import { expect } from 'chai' -describe('PostEndpoint', () => { - // const sinon = require('sinon') - const { expect } = require('chai') - // eslint-disable-next-line one-var-declaration-per-line - // let box - - const { Endpoint, PostEndpoint } = require('../../classes/Endpoint.class.js') - - // beforeEach(() => { box = sinon.createSandbox() }) - // afterEach(() => { box.restore() }) +import { Endpoint, PostEndpoint } from '../../classes/Endpoint.class.js' +describe('PostEndpoint', () => { describe('class PostEndpoint', () => { it('should be an instance of Endpoint', () => { const endpoint = new PostEndpoint('http://test.com') diff --git a/test/classes/ResponseError.class.test.js b/test/classes/ResponseError.class.test.mjs similarity index 84% rename from test/classes/ResponseError.class.test.js rename to test/classes/ResponseError.class.test.mjs index 1d3066a..ff66d47 100644 --- a/test/classes/ResponseError.class.test.js +++ b/test/classes/ResponseError.class.test.mjs @@ -1,19 +1,11 @@ -'require strict' - /* eslint-env mocha */ /* eslint-disable import/no-unresolved, no-unused-expressions */ -const ResponseError = require('../../classes/ResponseError.class.js') -const Response = require('../../classes/Response.class.js') - -describe('ResponseError', () => { - // const sinon = require('sinon') - const { expect } = require('chai') - // eslint-disable-next-line one-var-declaration-per-line - // let box +import { expect } from 'chai' - // beforeEach(() => { box = sinon.createSandbox() }) - // afterEach(() => { box.restore() }) +import ResponseError from '../../classes/ResponseError.class.js' +import Response from '../../classes/Response.class.js' +describe('ResponseError', () => { describe('class ResponseError', () => { it('should be an instance of Error', () => { const response = { foo: 'bar' } From 4f024cfa33edcffd0b7ec05662a9c1b95333f58b Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 21:06:47 -0400 Subject: [PATCH 6/7] Bump chai to 5.1.2 --- package-lock.json | 89 +++++++++++++++-------------------------------- package.json | 7 +++- 2 files changed, 35 insertions(+), 61 deletions(-) diff --git a/package-lock.json b/package-lock.json index b1bd336..61c3dd2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "devDependencies": { "c8": "^10.1.2", - "chai": "^4.5.0", + "chai": "^5.1.2", "flatstr": "^1.0.12", "ghooks": "^2.0.4", "mocha": "^10.7.3", @@ -649,13 +649,13 @@ } }, "node_modules/assertion-error": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", - "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", "dev": true, "license": "MIT", "engines": { - "node": "*" + "node": ">=12" } }, "node_modules/atomic-sleep": { @@ -909,45 +909,30 @@ } }, "node_modules/chai": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/chai/-/chai-4.5.0.tgz", - "integrity": "sha512-RITGBfijLkBddZvnn8jdqoTypxvqbOLYQkGGxXzeFjVHvudaPw0HNFD9x928/eUwYWd2dPCugVqspGALTZZQKw==", + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-5.1.2.tgz", + "integrity": "sha512-aGtmf24DW6MLHHG5gCx4zaI3uBq3KRtxeVs0DjFH6Z0rDNbsvTxFASFvdj79pxjxZ8/5u3PIiN3IwEIQkiiuPw==", "dev": true, "license": "MIT", "dependencies": { - "assertion-error": "^1.1.0", - "check-error": "^1.0.3", - "deep-eql": "^4.1.3", - "get-func-name": "^2.0.2", - "loupe": "^2.3.6", - "pathval": "^1.1.1", - "type-detect": "^4.1.0" + "assertion-error": "^2.0.1", + "check-error": "^2.1.1", + "deep-eql": "^5.0.1", + "loupe": "^3.1.0", + "pathval": "^2.0.0" }, "engines": { - "node": ">=4" - } - }, - "node_modules/chai/node_modules/type-detect": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.1.0.tgz", - "integrity": "sha512-Acylog8/luQ8L7il+geoSxhEkazvkslg7PSNKOX59mbB9cOveP5aq9h74Y7YU8yDpJwetzQQrfIwtf4Wp4LKcw==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=4" + "node": ">=12" } }, "node_modules/check-error": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.3.tgz", - "integrity": "sha512-iKEoDYaRmd1mxM90a2OEfWhjsjPpYPuQ+lMYsoxB126+t8fw7ySEO48nmDg5COTjxDI65/Y2OWpeEHk3ZOe8zg==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.1.tgz", + "integrity": "sha512-OAlb+T7V4Op9OwdkjmguYRqncdlx5JiofwOAUkmTF+jNdHwzTaTs4sRAGpzLF3oOz5xAyDGrPgeIDFQmDOTiJw==", "dev": true, "license": "MIT", - "dependencies": { - "get-func-name": "^2.0.2" - }, "engines": { - "node": "*" + "node": ">= 16" } }, "node_modules/chokidar": { @@ -1140,14 +1125,11 @@ "dev": true }, "node_modules/deep-eql": { - "version": "4.1.4", - "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-4.1.4.tgz", - "integrity": "sha512-SUwdGfqdKOwxCPeVYjwSyRpJ7Z+fhpwIAtmCUdZIWZ/YP5R9WAsyuSgpLVDi9bjWoN2LXHNss/dk3urXtdQxGg==", + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz", + "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==", "dev": true, "license": "MIT", - "dependencies": { - "type-detect": "^4.0.0" - }, "engines": { "node": ">=6" } @@ -2296,16 +2278,6 @@ "node": "6.* || 8.* || >= 10.*" } }, - "node_modules/get-func-name": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.2.tgz", - "integrity": "sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==", - "dev": true, - "license": "MIT", - "engines": { - "node": "*" - } - }, "node_modules/get-intrinsic": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", @@ -3389,14 +3361,11 @@ } }, "node_modules/loupe": { - "version": "2.3.7", - "resolved": "https://registry.npmjs.org/loupe/-/loupe-2.3.7.tgz", - "integrity": "sha512-zSMINGVYkdpYSOBmLi0D1Uo7JU9nVdQKrHxC8eYlV+9YKK9WePqAlL7lSlorG/U2Fw1w0hTBmaa/jrQ3UbPHtA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.2.tgz", + "integrity": "sha512-23I4pFZHmAemUnz8WZXbYRSKYj801VDaNv9ETuMh7IrMc7VuVVSo+Z9iLE3ni30+U48iDWfi30d3twAXBYmnCg==", "dev": true, - "license": "MIT", - "dependencies": { - "get-func-name": "^2.0.1" - } + "license": "MIT" }, "node_modules/lru-cache": { "version": "6.0.0", @@ -3922,13 +3891,13 @@ } }, "node_modules/pathval": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz", - "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.0.tgz", + "integrity": "sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==", "dev": true, "license": "MIT", "engines": { - "node": "*" + "node": ">= 14.16" } }, "node_modules/picomatch": { diff --git a/package.json b/package.json index bbd9b72..c31b77c 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,11 @@ "pre-push": "npm run cover && npm run check-coverage" } }, + "standard": { + "env": { + "mocha": true + } + }, "repository": { "type": "git", "url": "git+https://github.com/EveryMundo/http-client.git" @@ -38,7 +43,7 @@ "homepage": "https://github.com/EveryMundo/http-client#readme", "devDependencies": { "c8": "^10.1.2", - "chai": "^4.5.0", + "chai": "^5.1.2", "flatstr": "^1.0.12", "ghooks": "^2.0.4", "mocha": "^10.7.3", From 897a4ac3ef04fe0072d0f254ab97885be088c600 Mon Sep 17 00:00:00 2001 From: Daniel Santana Date: Fri, 25 Oct 2024 21:08:29 -0400 Subject: [PATCH 7/7] Expose promiseDataTo directly. @Jsalvadorpp --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 070348d..8c1a96c 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const { Endpoint, GetEndpoint, PostEndpoint } = require('./classes/Endpoint.clas const Headers = require('./classes/Headers.class.js') const Response = require('./classes/Response.class.js') const { parseEndpoints } = require('./lib/parse-endpoints.js') +const { promiseDataTo } = require('./lib/promise-data-to.js') const urlToEndpoint = (url, headers, agent) => new Endpoint(url, headers || null, agent) @@ -13,7 +14,7 @@ module.exports = { Response, parseEndpoints, urlToEndpoint, - promiseDataTo: Endpoint.post, + promiseDataTo, promiseGet: Endpoint.get, promisePost: Endpoint.post, fetch: Endpoint.fetch,