@hedia/test
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "@hedia/test", | ||
"version": "1.2.1", | ||
"description": "", | ||
"version": "1.2.2", | ||
"description": "Custom Test Reporter", | ||
"type": "module", | ||
"exports": { | ||
"./reporter": "./src/reporter.js" | ||
}, "scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prettier:check": "prettier --check .", | ||
"prettier:write": "prettier --write ." | ||
}, | ||
"repository": { | ||
@@ -15,3 +18,3 @@ "type": "git", | ||
}, | ||
"author": "", | ||
"author": "Mathieu Veber <mathieu@hedia.com>", | ||
"license": "UNLICENSED", | ||
@@ -21,3 +24,6 @@ "bugs": { | ||
}, | ||
"homepage": "https://github.com/hedia-team/test#readme" | ||
"homepage": "https://github.com/hedia-team/test#readme", | ||
"devDependencies": { | ||
"prettier": "^3.0.3" | ||
} | ||
} |
@@ -1,1 +0,11 @@ | ||
# test | ||
# Custom Test Reporter | ||
Format and send test output to the `test-service`. | ||
## Usage | ||
``` | ||
node --test --experimental-test-coverage --test-reporter @hedia/test/reporter | ||
``` | ||
Tip: Save the following command as a script in `package.json` |
import { readFileSync } from "fs"; | ||
export default async function* testReporter(source) { | ||
try { | ||
let title = '' | ||
const tests = []; | ||
let nestedTitles = []; | ||
let nestingLevel = -1; | ||
try { | ||
let title = ""; | ||
const tests = []; | ||
let nestedTitles = []; | ||
let nestingLevel = -1; | ||
for await (const event of source) { | ||
switch (event.type) { | ||
case "test:start": | ||
if (nestingLevel < event.data.nesting) { | ||
nestedTitles.push(event.data.name); | ||
nestingLevel += 1; | ||
} | ||
if (nestingLevel === event.data.nesting) { | ||
nestedTitles[nestedTitles.length - 1] = event.data.name; | ||
} | ||
break; | ||
case "test:pass": | ||
if (nestingLevel > event.data.nesting) { | ||
nestedTitles = nestedTitles.slice(0, -1); | ||
nestingLevel -= 1; | ||
} | ||
if (event.data?.details?.type === "suite") { | ||
break; | ||
} | ||
for await (const event of source) { | ||
switch (event.type) { | ||
case "test:start": | ||
if (nestingLevel < event.data.nesting) { | ||
nestedTitles.push(event.data.name); | ||
nestingLevel += 1; | ||
} | ||
if (nestingLevel === event.data.nesting) { | ||
nestedTitles[nestedTitles.length - 1] = event.data.name; | ||
} | ||
break; | ||
case "test:pass": | ||
if (nestingLevel > event.data.nesting) { | ||
nestedTitles = nestedTitles.slice(0, -1); | ||
nestingLevel -= 1; | ||
} | ||
if (event.data?.details?.type === "suite") { | ||
break; | ||
} | ||
yield `test ${nestedTitles.join(" - ")}: pass (${event.data.details.duration_ms}ms)\n`; | ||
yield `test ${nestedTitles.join(" - ")}: pass (${ | ||
event.data.details.duration_ms | ||
}ms)\n`; | ||
tests.push({ | ||
name: nestedTitles.join(" - "), | ||
number: tests.length + 1, | ||
duration: event.data.details.duration_ms, | ||
status: "pass", | ||
}); | ||
tests.push({ | ||
name: nestedTitles.join(" - "), | ||
number: tests.length + 1, | ||
duration: event.data.details.duration_ms, | ||
status: "pass", | ||
}); | ||
break; | ||
case "test:fail": | ||
if (nestingLevel > event.data.nesting) { | ||
nestedTitles = nestedTitles.slice(0, -1); | ||
nestingLevel -= 1; | ||
} | ||
if (event.data?.details?.type === "suite") { | ||
break; | ||
} | ||
break; | ||
case "test:fail": | ||
if (nestingLevel > event.data.nesting) { | ||
nestedTitles = nestedTitles.slice(0, -1); | ||
nestingLevel -= 1; | ||
} | ||
if (event.data?.details?.type === "suite") { | ||
break; | ||
} | ||
yield `test ${nestedTitles.join(" - ")}: fail (${event.data.details.duration_ms}ms)\n`; | ||
tests.push({ | ||
name: nestedTitles.join(" - "), | ||
number: tests.length + 1, | ||
duration: event.data.details.duration_ms, | ||
status: "fail", | ||
}); | ||
yield `test ${nestedTitles.join(" - ")}: fail (${ | ||
event.data.details.duration_ms | ||
}ms)\n`; | ||
tests.push({ | ||
name: nestedTitles.join(" - "), | ||
number: tests.length + 1, | ||
duration: event.data.details.duration_ms, | ||
status: "fail", | ||
}); | ||
break; | ||
case "test:coverage": | ||
const location = event.data.summary.workingDirectory + "/package.json" | ||
const { name, version } = JSON.parse(readFileSync(location, 'utf8')) | ||
title = `${name}@${version}` | ||
} | ||
} | ||
break; | ||
case "test:coverage": | ||
const location = | ||
event.data.summary.workingDirectory + "/package.json"; | ||
const { name, version } = JSON.parse(readFileSync(location, "utf8")); | ||
title = `${name}@${version}`; | ||
} | ||
} | ||
const input = "https://test.hedia.dev/api/v1/reports"; | ||
const init = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify( | ||
{ | ||
title, | ||
tests, | ||
}, | ||
null, | ||
4, | ||
), | ||
}; | ||
const input = "https://test.hedia.dev/api/v1/reports"; | ||
const init = { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify( | ||
{ | ||
title, | ||
tests, | ||
}, | ||
null, | ||
4, | ||
), | ||
}; | ||
const response = await fetch(input, init); | ||
const response = await fetch(input, init); | ||
console.log(response.ok ? "OK" : "NOT OK"); | ||
yield true; | ||
} catch (err) { | ||
console.error(err.message); | ||
} | ||
console.log(response.ok ? "OK" : "NOT OK"); | ||
yield true; | ||
} catch (err) { | ||
console.error(err.message); | ||
} | ||
} |
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
3438
83
1
12
1