diff --git a/etc/test-helpers.api.md b/etc/test-helpers.api.md index 56fc653..d9e4a02 100644 --- a/etc/test-helpers.api.md +++ b/etc/test-helpers.api.md @@ -4,10 +4,8 @@ ```ts -/// - import { IConfigComponent } from '@well-known-components/interfaces'; -import { IFetchComponent } from '@well-known-components/http-server'; +import { IFetchComponent } from '@well-known-components/interfaces'; import { Lifecycle } from '@well-known-components/interfaces'; import { default as sinon_2 } from 'sinon'; diff --git a/jest.config.js b/jest.config.js index 578c501..819a485 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,8 @@ module.exports = { - globals: { - 'ts-jest': { - tsconfig: 'tsconfig.json' - } - }, - moduleFileExtensions: ['ts', 'js'], transform: { - '^.+\\.(ts|tsx)$': 'ts-jest' + "^.+\\.(ts|tsx)$": ["ts-jest", {tsconfig: "test/tsconfig.json"}] }, + moduleFileExtensions: ['ts', 'js'], testMatch: ['**/*.spec.(ts)'], testEnvironment: 'node' -} \ No newline at end of file +} diff --git a/package.json b/package.json index b61bccc..adc9d17 100644 --- a/package.json +++ b/package.json @@ -17,20 +17,25 @@ }, "prettier": { "printWidth": 120, - "semi": false + "semi": false, + "singleQuote": true, + "trailingComma": "none", + "tabWidth": 2 }, "homepage": "https://github.com/well-known-components/test-helpers#readme", "devDependencies": { "@microsoft/api-extractor": "^7.36.0", - "@well-known-components/http-server": "^2.0.0", + "@well-known-components/http-server": "^2.1.0", "@well-known-components/interfaces": "^1.4.2", "ts-node": "^10.9.1", - "typescript": "^5.1.5" + "typescript": "^5.6.2" }, "dependencies": { "@types/jest": "^29.5.2", + "@types/node": "^22.5.4", "@types/sinon": "^17.0.1", "jest": "^29.5.0", + "node-fetch": "2.x", "sinon": "^18.0.0", "ts-jest": "^29.1.0" }, diff --git a/src/localFetch.ts b/src/localFetch.ts index 6040ecd..e0bf869 100644 --- a/src/localFetch.ts +++ b/src/localFetch.ts @@ -1,12 +1,11 @@ -import nodeFetch, { RequestInfo, RequestInit } from "node-fetch" +import nodeFetch, { RequestInfo, RequestInit } from 'node-fetch' import * as http from 'http' -import { IFetchComponent } from "@well-known-components/http-server" -import { IConfigComponent } from "@well-known-components/interfaces" +import { IFetchComponent, IConfigComponent } from '@well-known-components/interfaces' // start TCP port for listeners /* istanbul ignore next */ -let lastUsedPort = 19000 + parseInt(process.env.JEST_WORKER_ID || "1") * 1000 +let lastUsedPort = 19000 + parseInt(process.env.JEST_WORKER_ID || '1') * 1000 function getPort() { lastUsedPort += 1 @@ -16,7 +15,7 @@ function getPort() { /** * Default Server config * @public -**/ + **/ export const defaultServerConfig = () => ({ HTTP_SERVER_HOST: '0.0.0.0', HTTP_SERVER_PORT: String(getPort()) @@ -25,24 +24,22 @@ export const defaultServerConfig = () => ({ /** * Local Fetch component for testing local urls * @public -**/ -export async function createLocalFetchCompoment( - configComponent: IConfigComponent -): Promise { + **/ +export async function createLocalFetchCompoment(configComponent: IConfigComponent): Promise { const protocolHostAndProtocol = `http://${await configComponent.requireString( - "HTTP_SERVER_HOST" - )}:${await configComponent.requireNumber("HTTP_SERVER_PORT")}` + 'HTTP_SERVER_HOST' + )}:${await configComponent.requireNumber('HTTP_SERVER_PORT')}` const agent = new http.Agent({ keepAlive: false }) // test fetch, to hit our local server const localFetch: IFetchComponent = { async fetch(url: RequestInfo, initRequest?: RequestInit) { - if (typeof url == "string" && url.startsWith("/")) { + if (typeof url == 'string' && url.startsWith('/')) { return nodeFetch(protocolHostAndProtocol + url, { agent, ...initRequest }) } else { - throw new Error("localFetch only works for local testing-URLs") + throw new Error('localFetch only works for local testing-URLs') } - }, + } } return localFetch } diff --git a/test/simple-smoke-test/localFetch.spec.ts b/test/simple-smoke-test/localFetch.spec.ts index 601e5a8..a06b287 100644 --- a/test/simple-smoke-test/localFetch.spec.ts +++ b/test/simple-smoke-test/localFetch.spec.ts @@ -1,12 +1,12 @@ -import { createServerComponent, IFetchComponent, Router } from "@well-known-components/http-server" -import { IHttpServerComponent } from "@well-known-components/interfaces" +import { createServerComponent, Router } from '@well-known-components/http-server' +import { IHttpServerComponent, IFetchComponent } from '@well-known-components/interfaces' -import expect from "expect" -import { createRunner } from "../../src" -import { createLocalFetchCompoment, defaultServerConfig } from "../../src/localFetch" +import expect from 'expect' +import { createRunner } from '../../src' +import { createLocalFetchCompoment, defaultServerConfig } from '../../src/localFetch' type Components = { - fetch: IFetchComponent, + fetch: IFetchComponent server: IHttpServerComponent } type GlobalContext = { @@ -14,12 +14,12 @@ type GlobalContext = { } const logs = { - getLogger: (a: string) => ({ - log: (message: string) => {}, - error: (error: string | Error) => {}, - debug: (message: string) => {}, - info: (message: string) => {}, - warn: (message: string) => {}, + getLogger: (_a: string) => ({ + log: (_message: string) => {}, + error: (_error: string | Error) => {}, + debug: (_message: string) => {}, + info: (_message: string) => {}, + warn: (_message: string) => {} }) } const configMap = defaultServerConfig() @@ -27,7 +27,7 @@ const config = { getString: async (a: string) => a, getNumber: async (a: string) => Number(a), requireNumber: async (a: keyof typeof configMap) => Number(configMap[a])!, - requireString: async (a: keyof typeof configMap) => configMap[a]!, + requireString: async (a: keyof typeof configMap) => configMap[a]! } const ROUTE_URL = '/some-route' @@ -38,7 +38,7 @@ const test = createRunner({ const router = new Router() router.get(ROUTE_URL, async (_ctx) => { - return { status: 200, body: RESPONSE } + return { status: 200, body: RESPONSE } }) components.server.use(router.allowedMethods()) @@ -53,18 +53,18 @@ const test = createRunner({ fetch: await createLocalFetchCompoment(config), server: await createServerComponent({ config, logs }, {}) } - }, + } }) -test("test local fetch component", ({ components, stubComponents }) => { - it("should return response json", async () => { +test('test local fetch component', ({ components, stubComponents }) => { + it('should return response json', async () => { const { fetch } = components const response = await (await fetch.fetch(ROUTE_URL)).json() expect(response).toStrictEqual(RESPONSE) }) - it("sould fail if its an external url", async () => { + it('sould fail if its an external url', async () => { const { fetch } = components try { await fetch.fetch('https://some-route.com') diff --git a/yarn.lock b/yarn.lock index 1ab34e7..668f958 100644 --- a/yarn.lock +++ b/yarn.lock @@ -758,9 +758,9 @@ "@types/node" "*" "@types/http-errors@^2.0.1": - version "2.0.1" - resolved "https://registry.yarnpkg.com/@types/http-errors/-/http-errors-2.0.1.tgz#20172f9578b225f6c7da63446f56d4ce108d5a65" - integrity sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ== + version "2.0.4" + resolved "https://registry.yarnpkg.com/@types/http-errors/-/http-errors-2.0.4.tgz#7eb47726c391b7345a6ec35ad7f4de469cf5ba4f" + integrity sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA== "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1": version "2.0.4" @@ -802,6 +802,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-20.3.2.tgz#fa6a90f2600e052a03c18b8cb3fd83dd4e599898" integrity sha512-vOBLVQeCQfIcF/2Y7eKFTqrMnizK5lRNQ7ykML/5RuwVXVWxYkgwS7xbt4B6fKCUPgbSL5FSsjHQpaGQP/dQmw== +"@types/node@^22.5.4": + version "22.5.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-22.5.4.tgz#83f7d1f65bc2ed223bdbf57c7884f1d5a4fa84e8" + integrity sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg== + dependencies: + undici-types "~6.19.2" + "@types/sinon@^17.0.1": version "17.0.3" resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-17.0.3.tgz#9aa7e62f0a323b9ead177ed23a36ea757141a5fa" @@ -831,10 +838,10 @@ dependencies: "@types/yargs-parser" "*" -"@well-known-components/http-server@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@well-known-components/http-server/-/http-server-2.0.0.tgz#85ec93080ae64f962782699db3eeecd597f59405" - integrity sha512-baDP0+MpgqTnKLFFXS41WBbg9QIGCXTEA7BHf6JqW1evrTW10MKEx+rgEvQQlhCA5oXxUdKOD+xNnR3GqzGw9w== +"@well-known-components/http-server@^2.1.0": + version "2.1.0" + resolved "https://registry.yarnpkg.com/@well-known-components/http-server/-/http-server-2.1.0.tgz#23a18edc82904b3a575452c2d7e618c7da37a07f" + integrity sha512-IHD7aLTA+9DYEchQubHDBwc4FmVEmQC+2TWbi8Tz+QlkiQdtndcuba8XHH+EwqlB5sna/EAJGZGXPxS7okcHKA== dependencies: "@types/http-errors" "^2.0.1" destroy "^1.2.0" @@ -2160,9 +2167,9 @@ minimatch@~3.0.3: brace-expansion "^1.1.7" mitt@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/mitt/-/mitt-3.0.0.tgz#69ef9bd5c80ff6f57473e8d89326d01c414be0bd" - integrity sha512-7dX2/10ITVyqh4aOSVI9gdape+t9l2/8QxHrFmUXu4EEUpdlxl6RudZUPZoc+zuY2hk1j7XxVroIVIan/pD/SQ== + version "3.0.1" + resolved "https://registry.yarnpkg.com/mitt/-/mitt-3.0.1.tgz#ea36cf0cc30403601ae074c8f77b7092cdab36d1" + integrity sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw== ms@2.1.2: version "2.1.2" @@ -2185,10 +2192,10 @@ nise@^6.0.0: just-extend "^6.2.0" path-to-regexp "^6.2.1" -node-fetch@^2.6.9: - version "2.6.11" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.11.tgz#cde7fc71deef3131ef80a738919f999e6edfff25" - integrity sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w== +node-fetch@2.x, node-fetch@^2.6.9: + version "2.7.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" + integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== dependencies: whatwg-url "^5.0.0" @@ -2635,11 +2642,21 @@ typed-url-params@^1.0.1: resolved "https://registry.yarnpkg.com/typed-url-params/-/typed-url-params-1.0.1.tgz#59aeb01881a6ac6cc1cbd6e9f949f306ddb5a6e7" integrity sha512-762imXO+myoSDHD9+YxUfSmfT0yGH1j+3s9UJ6uqKkOYIwHH6/gsFo67ZoST0Ey/RSoaps1zGu1N+eiuuCxfeg== -typescript@5.4.2, typescript@^5.1.5: +typescript@5.4.2: version "5.4.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372" integrity sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ== +typescript@^5.6.2: + version "5.6.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.6.2.tgz#d1de67b6bef77c41823f822df8f0b3bcff60a5a0" + integrity sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw== + +undici-types@~6.19.2: + version "6.19.8" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02" + integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw== + universalify@^0.1.0: version "0.1.2" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"