Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: well-known-components/test-helpers
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.5.4
Choose a base ref
...
head repository: well-known-components/test-helpers
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 18 commits
  • 8 files changed
  • 3 contributors

Commits on Dec 12, 2023

  1. chore: bump @types/sinon from 17.0.1 to 17.0.2 (#42)

    Bumps [@types/sinon](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/sinon) from 17.0.1 to 17.0.2.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/sinon)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/sinon"
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Dec 12, 2023
    Copy the full SHA
    b8a08b6 View commit details
  2. chore: bump @types/jest from 29.5.8 to 29.5.11 (#45)

    Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 29.5.8 to 29.5.11.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/jest"
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Dec 12, 2023
    Copy the full SHA
    4144398 View commit details
  3. chore: bump @microsoft/api-extractor from 7.36.3 to 7.38.5 (#44)

    Bumps [@microsoft/api-extractor](https://github.com/microsoft/rushstack/tree/HEAD/apps/api-extractor) from 7.36.3 to 7.38.5.
    - [Changelog](https://github.com/microsoft/rushstack/blob/main/apps/api-extractor/CHANGELOG.md)
    - [Commits](https://github.com/microsoft/rushstack/commits/@microsoft/api-extractor_v7.38.5/apps/api-extractor)
    
    ---
    updated-dependencies:
    - dependency-name: "@microsoft/api-extractor"
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Dec 12, 2023
    Copy the full SHA
    4e5931e View commit details
  4. chore: bump ts-node from 10.9.1 to 10.9.2 (#47)

    Bumps [ts-node](https://github.com/TypeStrong/ts-node) from 10.9.1 to 10.9.2.
    - [Release notes](https://github.com/TypeStrong/ts-node/releases)
    - [Changelog](https://github.com/TypeStrong/ts-node/blob/main/development-docs/release-template.md)
    - [Commits](TypeStrong/ts-node@v10.9.1...v10.9.2)
    
    ---
    updated-dependencies:
    - dependency-name: ts-node
      dependency-type: direct:development
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Dec 12, 2023
    Copy the full SHA
    e62fd1b View commit details

Commits on Jan 9, 2024

  1. fix: edge case in node 20

    menduz committed Jan 9, 2024
    Copy the full SHA
    25c5df4 View commit details

Commits on Feb 8, 2024

  1. fix: disable keep alive for local fetch requests (#54)

    hugoArregui authored Feb 8, 2024
    Copy the full SHA
    de171dd View commit details
  2. chore: bump ts-jest from 29.1.1 to 29.1.2 (#51)

    Bumps [ts-jest](https://github.com/kulshekhar/ts-jest) from 29.1.1 to 29.1.2.
    - [Release notes](https://github.com/kulshekhar/ts-jest/releases)
    - [Changelog](https://github.com/kulshekhar/ts-jest/blob/main/CHANGELOG.md)
    - [Commits](kulshekhar/ts-jest@v29.1.1...v29.1.2)
    
    ---
    updated-dependencies:
    - dependency-name: ts-jest
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 8, 2024
    Copy the full SHA
    4fc0444 View commit details
  3. chore: bump @types/jest from 29.5.11 to 29.5.12 (#53)

    Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 29.5.11 to 29.5.12.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/jest"
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 8, 2024
    Copy the full SHA
    93c8b1e View commit details
  4. chore: bump @types/sinon from 17.0.2 to 17.0.3 (#50)

    Bumps [@types/sinon](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/sinon) from 17.0.2 to 17.0.3.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/sinon)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/sinon"
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 8, 2024
    Copy the full SHA
    df2899b View commit details
  5. chore: bump @microsoft/api-extractor from 7.38.5 to 7.39.4 (#52)

    Bumps [@microsoft/api-extractor](https://github.com/microsoft/rushstack/tree/HEAD/apps/api-extractor) from 7.38.5 to 7.39.4.
    - [Changelog](https://github.com/microsoft/rushstack/blob/main/apps/api-extractor/CHANGELOG.md)
    - [Commits](https://github.com/microsoft/rushstack/commits/@microsoft/api-extractor_v7.39.4/apps/api-extractor)
    
    ---
    updated-dependencies:
    - dependency-name: "@microsoft/api-extractor"
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 8, 2024
    Copy the full SHA
    c7d0db6 View commit details

Commits on Sep 10, 2024

  1. chore: bump @microsoft/api-extractor from 7.39.4 to 7.43.1 (#65)

    Bumps [@microsoft/api-extractor](https://github.com/microsoft/rushstack/tree/HEAD/apps/api-extractor) from 7.39.4 to 7.43.1.
    - [Changelog](https://github.com/microsoft/rushstack/blob/main/apps/api-extractor/CHANGELOG.md)
    - [Commits](https://github.com/microsoft/rushstack/commits/@microsoft/api-extractor_v7.43.1/apps/api-extractor)
    
    ---
    updated-dependencies:
    - dependency-name: "@microsoft/api-extractor"
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 10, 2024
    Copy the full SHA
    89e32b3 View commit details
  2. chore: bump @well-known-components/interfaces from 1.4.2 to 1.4.3 (#61)

    Bumps [@well-known-components/interfaces](https://github.com/well-known-components/interfaces) from 1.4.2 to 1.4.3.
    - [Release notes](https://github.com/well-known-components/interfaces/releases)
    - [Commits](well-known-components/interfaces@1.4.2...1.4.3)
    
    ---
    updated-dependencies:
    - dependency-name: "@well-known-components/interfaces"
      dependency-type: direct:development
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 10, 2024
    Copy the full SHA
    e8aaba8 View commit details
  3. chore: bump sinon from 17.0.1 to 18.0.0 (#67)

    Bumps [sinon](https://github.com/sinonjs/sinon) from 17.0.1 to 18.0.0.
    - [Release notes](https://github.com/sinonjs/sinon/releases)
    - [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md)
    - [Commits](sinonjs/sinon@v17.0.1...v18.0.0)
    
    ---
    updated-dependencies:
    - dependency-name: sinon
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 10, 2024
    Copy the full SHA
    cf84b23 View commit details
  4. chore: bump ts-jest from 29.1.2 to 29.2.5 (#68)

    Bumps [ts-jest](https://github.com/kulshekhar/ts-jest) from 29.1.2 to 29.2.5.
    - [Release notes](https://github.com/kulshekhar/ts-jest/releases)
    - [Changelog](https://github.com/kulshekhar/ts-jest/blob/main/CHANGELOG.md)
    - [Commits](kulshekhar/ts-jest@v29.1.2...v29.2.5)
    
    ---
    updated-dependencies:
    - dependency-name: ts-jest
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 10, 2024
    Copy the full SHA
    257de81 View commit details
  5. chore: update dependencies, http-server is now a dev dependency (#71)

    hugoArregui authored Sep 10, 2024
    Copy the full SHA
    8b09740 View commit details

Commits on Sep 11, 2024

  1. chore: bump @microsoft/api-extractor from 7.43.1 to 7.47.7 (#70)

    Bumps [@microsoft/api-extractor](https://github.com/microsoft/rushstack/tree/HEAD/apps/api-extractor) from 7.43.1 to 7.47.7.
    - [Changelog](https://github.com/microsoft/rushstack/blob/main/apps/api-extractor/CHANGELOG.md)
    - [Commits](https://github.com/microsoft/rushstack/commits/@microsoft/api-extractor_v7.47.7/apps/api-extractor)
    
    ---
    updated-dependencies:
    - dependency-name: "@microsoft/api-extractor"
      dependency-type: direct:development
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 11, 2024
    Copy the full SHA
    3b4e298 View commit details
  2. chore: bump sinon from 18.0.0 to 18.0.1 (#73)

    Bumps [sinon](https://github.com/sinonjs/sinon) from 18.0.0 to 18.0.1.
    - [Release notes](https://github.com/sinonjs/sinon/releases)
    - [Changelog](https://github.com/sinonjs/sinon/blob/main/docs/changelog.md)
    - [Commits](sinonjs/sinon@v18.0.0...v18.0.1)
    
    ---
    updated-dependencies:
    - dependency-name: sinon
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 11, 2024
    Copy the full SHA
    7769c7e View commit details

Commits on Feb 18, 2025

  1. chore: bump @types/jest from 29.5.12 to 29.5.14 (#83)

    Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 29.5.12 to 29.5.14.
    - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
    - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)
    
    ---
    updated-dependencies:
    - dependency-name: "@types/jest"
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <[email protected]>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Feb 18, 2025
    Copy the full SHA
    5fd5f39 View commit details
Showing with 350 additions and 288 deletions.
  1. +7 −0 .devcontainer/devcontainer.json
  2. +1 −3 etc/test-helpers.api.md
  3. +3 −8 jest.config.js
  4. +9 −4 package.json
  5. +4 −4 src/index.ts
  6. +15 −15 src/localFetch.ts
  7. +18 −18 test/simple-smoke-test/localFetch.spec.ts
  8. +293 −236 yarn.lock
7 changes: 7 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "docker",
"image": "mcr.microsoft.com/devcontainers/typescript-node:0-20",
"containerEnv": {
"SHELL": "/bin/zsh"
}
}
4 changes: 1 addition & 3 deletions etc/test-helpers.api.md
Original file line number Diff line number Diff line change
@@ -4,10 +4,8 @@
```ts

/// <reference types="jest" />

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';

11 changes: 3 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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'
}
}
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -17,21 +17,26 @@
},
"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",
"sinon": "^17.0.0",
"node-fetch": "2.x",
"sinon": "^18.0.0",
"ts-jest": "^29.1.0"
},
"files": [
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -44,8 +44,8 @@ if (!_beforeAll || !_afterAll) {
*/
export type SpiedInstance<TType extends {}> = {
[P in keyof TType]: Required<TType>[P] extends (...args: any[]) => any
? jest.SpyInstance<ReturnType<Required<TType>[P]>, jest.ArgsType<Required<TType>[P]>>
: never
? jest.SpyInstance<ReturnType<Required<TType>[P]>, jest.ArgsType<Required<TType>[P]>>
: never
}

/**
@@ -58,7 +58,7 @@ export function createRunner<TestComponents extends Record<string, any>>(
) {
return (name: string, suite: (testArgs: TestArguments<TestComponents>) => void) => {
let program: Lifecycle.ComponentBasedProgram<TestComponents>
let sandbox: sinon.SinonSandbox
let sandbox: sinon.SinonSandbox | null = null
const stubComponentInstances = new Map<keyof TestComponents, sinon.SinonStubbedInstance<any>>()
const spyComponentInstances = new Map<keyof TestComponents, SpiedInstance<any>>()

@@ -146,7 +146,7 @@ export function createRunner<TestComponents extends Record<string, any>>(
})

afterEach(() => {
sandbox.restore()
sandbox?.restore()
jest.restoreAllMocks()
})

30 changes: 15 additions & 15 deletions src/localFetch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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
@@ -15,7 +15,7 @@ function getPort() {
/**
* Default Server config
* @public
**/
**/
export const defaultServerConfig = () => ({
HTTP_SERVER_HOST: '0.0.0.0',
HTTP_SERVER_PORT: String(getPort())
@@ -24,22 +24,22 @@ export const defaultServerConfig = () => ({
/**
* Local Fetch component for testing local urls
* @public
**/
export async function createLocalFetchCompoment(
configComponent: IConfigComponent
): Promise<IFetchComponent> {
**/
export async function createLocalFetchCompoment(configComponent: IConfigComponent): Promise<IFetchComponent> {
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("/")) {
return nodeFetch(protocolHostAndProtocol + url, { ...initRequest })
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
}
36 changes: 18 additions & 18 deletions test/simple-smoke-test/localFetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
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<GlobalContext>
}
type GlobalContext = {
components: Components
}

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()
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<Components>({
const router = new Router<GlobalContext>()

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<Components>({
fetch: await createLocalFetchCompoment(config),
server: await createServerComponent<GlobalContext>({ 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')
Loading