chore: more readable snapshots

This commit is contained in:
2024-03-24 18:04:47 +01:00
parent 4cb8daf908
commit 93f99c732f
37 changed files with 1258 additions and 1096 deletions

View File

@@ -49,7 +49,7 @@ export async function runBrowserTest(
...test,
log: test.log ?? console.log,
onResult: (output)=>{
testOutput = {...testOutput, ...output};
Object.assign(testOutput, output);
}
})]: [])
]

View File

@@ -1,5 +1,7 @@
// TODO: this should be the main module used, other should be imported manually if exceptions are needed?
export * from "./browser-test.ts";
export {defaultOutput} from "./default-output.ts";
export {serializer} from "./test-serializer.ts";
export * from "./code-output.ts";
export * from "./print-code-output.ts";

View File

@@ -133,6 +133,10 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
let server : Server;
let bundle : OutputBundle = {};
const closeServer = async ()=>new Promise((resolve, reject)=>{
server.close((err)=>err?reject(err):resolve(undefined));
server = null as unknown as Server; // unset
});
const logTest = (msg: string, mode: 'info'|'warn' = 'info')=>{
if(isInDebugMode()){
@@ -217,9 +221,9 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
function closeServerOnTermination () {
const terminationSignals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP']
terminationSignals.forEach(signal => {
process.on(signal, () => {
process.on(signal, async () => {
if (server) {
server.close()
await closeServer();
process.exit()
}
})
@@ -229,7 +233,7 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
// release previous server instance if rollup is reloading configuration in watch mode
// @ts-ignore
if (server) {
server.close()
closeServer()
} else {
closeServerOnTermination()
}
@@ -279,8 +283,9 @@ export default function serveTest (options: RollupServeTestOptions ): Plugin {
}
}
},
closeBundle (){
async closeBundle(){
// Done with the bundle
await closeServer();
}
}
}

View File

@@ -0,0 +1,79 @@
import type {runBrowserTest} from "./browser-test.ts";
import type {expect} from "@jest/globals";
import chalk from "chalk";
type TestOutput = Awaited<ReturnType<typeof runBrowserTest>>;
type Serializer = Parameters<typeof expect.addSnapshotSerializer>[0];
function headerFor(name: string): string[]{
const hr = name.split('').map(()=>`#`).join('')
return [
`##${hr}##`,
`# ${name} #`,
`##${hr}##`,
];
}
export const serializer: Serializer = {
test: (val: TestOutput)=> !!(
(val?.code && Array.isArray(val?.code))
|| (val?.html && typeof(val?.html)==='string')
),
serialize(val: TestOutput,
config,
indentation,
depth,
refs,
printer): string{
const indent = (config.indent||'')+(indentation+'');
let linesOut: string[] = [];
if(val.code){
const fileLines: string[][] = val.code.slice().sort(((a,b)=>{
const sortPropsA = [!a.fileName.endsWith('html'), a.fileName];
const sortPropsB = [!b.fileName.endsWith('html'), b.fileName];
for(let i = 0; i< 2;++i){
if(sortPropsA[i]<sortPropsB[i]) return -1;
else if(sortPropsA[i]>sortPropsB[i]) return 1;
}
return 0;
})).map(({fileName, code, source})=>{
return [
...headerFor(fileName),
...((code||source).split('\n'))
]
});
linesOut = linesOut.concat(...fileLines);
}
if(val.html){
linesOut = linesOut.concat([
...headerFor("RENDERED HTML"),
...(val.html.split('\n')),
]);
}
if(val.errors?.length){
linesOut = linesOut.concat([
...headerFor("ERRORS"),
], ...val.errors.map(x=>x.split("\n")));
}
if(val.console?.length){
linesOut = linesOut.concat([
...headerFor("CONSOLE"),
], ...val.console.map(x=>x.split("\n")));
}
if(val.requestsFailed?.length){
linesOut = linesOut.concat([
...headerFor("FAILED REQUESTS"),
], ...val.requestsFailed.map(x=>x.split("\n")));
}
if(val.responses?.length){
linesOut = linesOut.concat([
...headerFor("RESPONSES"),
], ...val.responses.map(x=>x.split("\n")));
}
return linesOut.map(x=>`${indent}${x}`).join('\n');
},
}