Skip to content

Commit

Permalink
test: simplify snapshot tests (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 17, 2024
1 parent 7b7ffef commit 0c2c0d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
20 changes: 9 additions & 11 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ import.meta.env?.TEST true"
`;

exports[`fixtures > error-parse > stderr 1`] = `
"[Error: ParseError: Unexpected token
<cwd>/index.ts:4:0]"
[
"Error: ParseError: Unexpected token ",
]
`;

exports[`fixtures > error-parse > stdout 1`] = `""`;

exports[`fixtures > error-runtime > stderr 1`] = `"test error"`;
exports[`fixtures > error-runtime > stderr 1`] = `
[
"Error: test error",
]
`;

exports[`fixtures > error-runtime > stdout 1`] = `""`;

Expand All @@ -44,14 +49,7 @@ exports[`fixtures > esm > stdout 1`] = `
{
file: '<cwd>/test.js',
dir: '<cwd>',
'import.meta.url': 'file://<cwd>/test.js',
stack: [
'at getStack (<cwd>/test)',
'at test (<cwd>/test)',
'at async <cwd>/index.js',
'at async Function.import (<root>/dist/jiti.cjs)',
'at async file://<root>/lib/jiti-cli.mjs'
]
'import.meta.url': 'file://<cwd>/test.js'
}"
`;

Expand Down
12 changes: 10 additions & 2 deletions test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe("fixtures", async () => {
);
}

function extractErrors(stderr: string) {
const errors = [] as string[];
for (const m of stderr.matchAll(/\w*(Error|Warning).*:.*$/gm)) {
errors.push(m[0]);
}
return errors;
}

const { stdout, stderr } = await x("node", [jitiPath, fixtureEntry], {
nodeOptions: {
cwd,
Expand All @@ -59,9 +67,9 @@ describe("fixtures", async () => {
});

if (name.includes("error")) {
expect(cleanUpSnap(stderr)).toMatchSnapshot("stderr");
expect(extractErrors(cleanUpSnap(stderr))).toMatchSnapshot("stderr");
} else {
// expect no error
// Expect no error by default
expect(stderr).toBe("");
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/error-runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
throw "test error";
throw new Error("test error");
6 changes: 0 additions & 6 deletions test/fixtures/esm/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const getStack = () => new Error("Boo").stack;

require("./utils.mjs");

export default async function test() {
Expand All @@ -9,9 +7,5 @@ export default async function test() {
file: __filename,
dir: __dirname,
"import.meta.url": import.meta.url,
stack: getStack()
.split("\n")
.splice(1)
.map((s) => s.trim()),
};
}

0 comments on commit 0c2c0d0

Please sign in to comment.