@hedia/test
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -1,4 +0,1 @@ | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/// <reference types="node" resolution-mode="require"/> | ||
import { Mock } from "node:test"; | ||
@@ -5,0 +2,0 @@ interface TestContext { |
@@ -7,2 +7,3 @@ import assert from "node:assert"; | ||
let server; | ||
let serverUrl; | ||
before(() => { | ||
@@ -13,3 +14,7 @@ server = createServer((req, res) => { | ||
}); | ||
server.listen(8080); | ||
server.listen(0); | ||
const address = server.address(); | ||
if (address === null || typeof address !== "object") | ||
throw new Error("Can't work out test server port number"); | ||
serverUrl = `http://localhost:${address.port}`; | ||
}); | ||
@@ -26,3 +31,3 @@ after(() => { | ||
.addMock({ | ||
resource: "http://localhost:8080/makeshift", | ||
resource: `${serverUrl}/makeshift`, | ||
fetchImplementation: async () => new Response("Hello, there!", { status: 200 }), | ||
@@ -34,7 +39,7 @@ }); | ||
assert.equal(await response1.text(), "Hello, world!"); | ||
const response2 = await fetch("http://localhost:8080/makeshift"); | ||
const response2 = await fetch(`${serverUrl}/makeshift`); | ||
assert(response2.ok); | ||
assert.equal(response2.status, 200); | ||
assert.equal(await response2.text(), "Hello, there!"); | ||
const response3 = await fetch("http://localhost:8080/"); | ||
const response3 = await fetch(`${serverUrl}/`); | ||
assert(!response3.ok); | ||
@@ -46,3 +51,3 @@ assert.equal(response3.status, 418); | ||
// Fetch calls in this test should not be mocked by the FetchMocker defined in the previous test | ||
const response = await fetch("http://localhost:8080/makeshift"); | ||
const response = await fetch(`${serverUrl}/makeshift`); | ||
assert(!response.ok); | ||
@@ -67,3 +72,3 @@ assert.equal(response.status, 418); | ||
new FetchMocker(testContext).addMock({ | ||
resource: "http://localhost:8080/something", | ||
resource: `${serverUrl}/something`, | ||
fetchImplementation: async () => new Response("Hello, world!", { status: 200 }), | ||
@@ -73,3 +78,3 @@ }); | ||
const initArgument = method ? { method } : undefined; | ||
const response = await fetch("http://localhost:8080/something", initArgument); | ||
const response = await fetch(`${serverUrl}/something`, initArgument); | ||
assert(response.ok); | ||
@@ -82,7 +87,7 @@ assert.equal(response.status, 200); | ||
new FetchMocker(testContext).addMock({ | ||
resource: "http://localhost:8080/something", | ||
resource: `${serverUrl}/something`, | ||
method: "POST", | ||
fetchImplementation: async () => new Response("Hello, post!", { status: 201 }), | ||
}); | ||
const response = await fetch("http://localhost:8080/something", { method: "POST" }); | ||
const response = await fetch(`${serverUrl}/something`, { method: "POST" }); | ||
assert(response.ok); | ||
@@ -93,3 +98,3 @@ assert.equal(response.status, 201); | ||
const initArgument = method ? { method } : undefined; | ||
const response = await fetch("http://localhost:8080/something", initArgument); | ||
const response = await fetch(`${serverUrl}/something`, initArgument); | ||
assert(!response.ok); | ||
@@ -104,14 +109,14 @@ assert.equal(response.status, 418); | ||
new FetchMocker(testContext).addMock({ | ||
resource: "http://localhost:8080/words", | ||
resource: `${serverUrl}/words`, | ||
fetchImplementation: async () => new Response("Hello, purple!", { status: 200 }), | ||
}); | ||
const response1 = await fetch("http://localhost:8080/words"); | ||
const response1 = await fetch(`${serverUrl}/words`); | ||
assert(response1.ok); | ||
assert.equal(response1.status, 200); | ||
assert.equal(await response1.text(), "Hello, purple!"); | ||
const response2 = await fetch("http://localhost:8080/word"); | ||
const response2 = await fetch(`${serverUrl}/word`); | ||
assert(!response2.ok); | ||
assert.equal(response2.status, 418); | ||
assert.equal(await response2.text(), "I'm a teapot"); | ||
const response3 = await fetch("http://localhost:8080/wordsmith"); | ||
const response3 = await fetch(`${serverUrl}/wordsmith`); | ||
assert(!response3.ok); | ||
@@ -123,10 +128,6 @@ assert.equal(response3.status, 418); | ||
new FetchMocker(testContext).addMock({ | ||
resource: /local(host|teapot):\d{4}\/[a-z]{5}$/, | ||
resource: /local(host|teapot):\d+\/[a-z]{5}$/, | ||
fetchImplementation: async () => new Response("Hello, RegExp!", { status: 200 }), | ||
}); | ||
for (const url of [ | ||
"http://localhost:8080/words", | ||
"http://localhost:1234/house", | ||
"localteapot:0123/sword", | ||
]) { | ||
for (const url of [`${serverUrl}/words`, "http://localhost:1234/house", "localteapot:0123/sword"]) { | ||
const response = await fetch(url); | ||
@@ -137,7 +138,3 @@ assert(response.ok, `response not ok for ${url}`); | ||
} | ||
for (const url of [ | ||
"http://localhost:8080/word", | ||
"http://localhost:8080/12345", | ||
new Request("http://localhost:8080/wordsmith"), | ||
]) { | ||
for (const url of [`${serverUrl}/word`, `${serverUrl}/12345`, new Request(`${serverUrl}/wordsmith`)]) { | ||
const response = await fetch(url); | ||
@@ -151,14 +148,14 @@ assert(!response.ok); | ||
new FetchMocker(testContext).addMock({ | ||
resource: new URL("http://localhost:8080/words"), | ||
resource: new URL(`${serverUrl}/words`), | ||
fetchImplementation: async () => new Response("Hello, URL!", { status: 200 }), | ||
}); | ||
const response1 = await fetch("http://localhost:8080/words"); | ||
const response1 = await fetch(`${serverUrl}/words`); | ||
assert(response1.ok); | ||
assert.equal(response1.status, 200); | ||
assert.equal(await response1.text(), "Hello, URL!"); | ||
const response2 = await fetch("http://localhost:8080/word"); | ||
const response2 = await fetch(`${serverUrl}/word`); | ||
assert(!response2.ok); | ||
assert.equal(response2.status, 418); | ||
assert.equal(await response2.text(), "I'm a teapot"); | ||
const response3 = await fetch(new Request("http://localhost:8080/wordsmith")); | ||
const response3 = await fetch(new Request(`${serverUrl}/wordsmith`)); | ||
assert(!response3.ok); | ||
@@ -171,3 +168,3 @@ assert.equal(response3.status, 418); | ||
new FetchMocker().addMock({ | ||
resource: new URL("http://localhost:8080/words?color=purple&number=5"), | ||
resource: new URL(`${serverUrl}/words?color=purple&number=5`), | ||
fetchImplementation: async () => new Response("Hello, purple!", { status: 200 }), | ||
@@ -180,3 +177,3 @@ }); | ||
it("matches if all the query arguments are in the same order", async () => { | ||
const response1 = await fetch("http://localhost:8080/words?color=purple&number=5"); | ||
const response1 = await fetch(`${serverUrl}/words?color=purple&number=5`); | ||
assert(response1.ok); | ||
@@ -187,3 +184,3 @@ assert.equal(response1.status, 200); | ||
it("matches if the query arguments are in a different order", async () => { | ||
const response2 = await fetch("http://localhost:8080/words?number=5&color=purple"); | ||
const response2 = await fetch(`${serverUrl}/words?number=5&color=purple`); | ||
assert(response2.ok); | ||
@@ -194,3 +191,3 @@ assert.equal(response2.status, 200); | ||
it("doesn't match if a given query argument has a wrong value", async () => { | ||
const response3 = await fetch("http://localhost:8080/words?color=blue&number=5"); | ||
const response3 = await fetch(`${serverUrl}/words?color=blue&number=5`); | ||
assert(!response3.ok); | ||
@@ -201,3 +198,3 @@ assert.equal(response3.status, 418); | ||
it("doesn't match if a given query argument is missing", async () => { | ||
const response4 = await fetch("http://localhost:8080/words?number=5"); | ||
const response4 = await fetch(`${serverUrl}/words?number=5`); | ||
assert(!response4.ok); | ||
@@ -208,3 +205,3 @@ assert.equal(response4.status, 418); | ||
it("matches if extra query arguments are present", async () => { | ||
const response5 = await fetch("http://localhost:8080/words?color=purple&number=5&extra=stuff"); | ||
const response5 = await fetch(`${serverUrl}/words?color=purple&number=5&extra=stuff`); | ||
assert(response5.ok); | ||
@@ -220,9 +217,9 @@ assert.equal(response5.status, 200); | ||
new FetchMocker(testContext, true).addMock({ | ||
resource: "http://localhost:8080/verbose", | ||
resource: `${serverUrl}/verbose`, | ||
fetchImplementation: async () => new Response("Hello, verbose!", { status: 200 }), | ||
}); | ||
await fetch("http://localhost:8080/verbose"); | ||
await fetch("http://localhost:8080/other"); | ||
assert.equal(consoleLogMock.mock.calls[0].arguments[0], "using mock to handle GET http://localhost:8080/verbose"); | ||
assert.equal(consoleLogMock.mock.calls[1].arguments[0], "using original fetch to handle GET http://localhost:8080/other"); | ||
await fetch(`${serverUrl}/verbose`); | ||
await fetch(`${serverUrl}/other`); | ||
assert.equal(consoleLogMock.mock.calls[0].arguments[0], `using mock to handle GET ${serverUrl}/verbose`); | ||
assert.equal(consoleLogMock.mock.calls[1].arguments[0], `using original fetch to handle GET ${serverUrl}/other`); | ||
}); | ||
@@ -232,7 +229,7 @@ it("is off by default", async (testContext) => { | ||
new FetchMocker(testContext).addMock({ | ||
resource: "http://localhost:8080/nonverbose", | ||
resource: `${serverUrl}/nonverbose`, | ||
fetchImplementation: async () => new Response("Hello, normal!", { status: 200 }), | ||
}); | ||
await fetch("http://localhost:8080/verbose"); | ||
await fetch("http://localhost:8080/other"); | ||
await fetch(`${serverUrl}/verbose`); | ||
await fetch(`${serverUrl}/other`); | ||
assert.equal(consoleLogMock.mock.calls.length, 0); | ||
@@ -239,0 +236,0 @@ }); |
{ | ||
"name": "@hedia/test", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Tools for testing and reporting", | ||
"type": "module", | ||
"homepage": "https://github.com/hedia-team/test#readme", | ||
"bugs": { | ||
"url": "https://github.com/hedia-team/test/issues" | ||
}, | ||
"license": "UNLICENSED", | ||
"author": "Mathieu Veber <mathieu@hedia.com>", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"exports": { | ||
".": "./dist/src/index.js", | ||
"./fetchMocker": "./dist/src/fetchMocker.js", | ||
"./reporters/json": "./dist/src/reporters/json.js", | ||
"./reporters/sonarcloud": "./dist/src/reporters/sonarcloud.js", | ||
"./reporters/test": "./dist/src/reporters/test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hedia-team/test.git" | ||
}, | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc", | ||
"build:watch": "tsc --watch", | ||
"build": "tsc", | ||
"dev": "concurrently \"npm run build:watch\" \"npm run start:watch\"", | ||
"eslint": "eslint .", | ||
"eslint:fix": "eslint . --fix", | ||
"eslint": "eslint .", | ||
"package:lint": "npx npm-package-json-lint .", | ||
"prettier:check": "prettier --check .", | ||
"prettier:write": "prettier --write .", | ||
"start": "node --enable-source-maps .", | ||
"start:watch": "node --enable-source-maps --watch .", | ||
"start": "node --enable-source-maps .", | ||
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter spec --test-reporter @hedia/test/reporters/sonarcloud --test-reporter-destination stdout --test-reporter-destination coverage.xml --test-concurrency 1", | ||
"test": "node --enable-source-maps --test --experimental-test-coverage --test-reporter spec --test-reporter @hedia/test/reporters/json --test-reporter @hedia/test/reporters/sonarcloud --test-reporter-destination stdout --test-reporter-destination test.json --test-reporter-destination coverage.xml --test-concurrency 1", | ||
"tsc": "tsc --noEmit" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hedia-team/test.git" | ||
"dependencies": { | ||
"@hedia/html": "^2.0.4" | ||
}, | ||
"author": "Mathieu Veber <mathieu@hedia.com>", | ||
"license": "UNLICENSED", | ||
"bugs": { | ||
"url": "https://github.com/hedia-team/test/issues" | ||
}, | ||
"homepage": "https://github.com/hedia-team/test#readme", | ||
"devDependencies": { | ||
"@hedia/eslint-config": "4.0.2", | ||
"@hedia/prettier-config": "1.0.6", | ||
"@hedia/tsconfig": "1.2.2", | ||
"@types/node": "^20.14.2", | ||
"@typescript-eslint/eslint-plugin": "7.12.0", | ||
"@typescript-eslint/parser": "7.12.0", | ||
"eslint": "8.57.0", | ||
"prettier": "^3.3.1" | ||
"@hedia/eslint-config": "4.0.3", | ||
"@hedia/package-lint": "1.1.1", | ||
"@hedia/prettier-config": "1.0.7", | ||
"@hedia/tsconfig": "2.0.1", | ||
"@types/node": "20.16.1" | ||
}, | ||
"dependencies": { | ||
"@hedia/html": "^2.0.4" | ||
}, | ||
"engines": { | ||
@@ -52,7 +55,3 @@ "node": ">=20", | ||
}, | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"prettier": "@hedia/prettier-config" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97064
5
32
1003