Skip to content

Commit

Permalink
refactor: snapshots should end in .snap, not .snap.cjs
Browse files Browse the repository at this point in the history
Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery committed Feb 8, 2024
1 parent b3c4fc8 commit f6f9c3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TUI Test Snapshot v1

exports[`take a screenshot 1`] = String.raw`
╭────────────────────────────────────────╮
│> foo │
Expand Down
2 changes: 2 additions & 0 deletions src/runner/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ const copyFilesToCache = async (directory: string, destination: string) => {
fileHash,
transformedPath
);
} else if (fileExtension == ".snap") {
await fsAsync.copyFile(resolvedPath, `${destinationPath}.cjs`);
} else {
await fsAsync.copyFile(resolvedPath, destinationPath);
}
Expand Down
24 changes: 14 additions & 10 deletions src/test/matchers/toMatchSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ import path from "node:path";
import process from "node:process";
import fs from "node:fs";
import fsAsync from "node:fs/promises";
import module from "node:module";
import workpool from "workerpool";

import { Terminal } from "../../terminal/term.js";

export type SnapshotStatus = "passed" | "failed" | "written" | "updated";

const require = module.createRequire(import.meta.url);

const snapshots = new Map<string, string>();
const snapshotsIdx = new Map<string, number>();
const snapshotPath = (testPath: string): string =>
path.join(
process.cwd(),
path.dirname(testPath),
"__snapshots__",
`${path.basename(testPath)}.snap.cjs`
`${path.basename(testPath)}.snap`
);

const loadSnapshot = async (
Expand All @@ -35,7 +38,7 @@ const loadSnapshot = async (
if (!fs.existsSync(snapPath)) {
return;
}
snaps = (await import(`file://${snapPath}`)).default;
snaps = require(snapPath);
snapshots.set(testPath, snaps);
}

Expand All @@ -53,17 +56,18 @@ const updateSnapshot = async (
}

const fh = await fsAsync.open(snapPath, "w+");
const snapshots = (await import(`file://${snapPath}`)).default;
const snapshots = require(snapPath);
snapshots[testName] = snapshot;

await fh.writeFile(
Object.keys(snapshots)
.sort()
.map(
(snapshotName) =>
`exports[\`${snapshotName}\`] = String.raw\`\n${snapshots[snapshotName].trim()}\n\`;\n\n`
)
.join("")
"// TUI Test Snapshot v1\n\n" +
Object.keys(snapshots)
.sort()
.map(
(snapshotName) =>
`exports[\`${snapshotName}\`] = String.raw\`\n${snapshots[snapshotName].trim()}\n\`;\n\n`
)
.join("")
);
await fh.close();
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
exports[`cursor position 1`] = String.raw`
╭────────────────────────────────────────╮
│> echo foo │
│foo │
│> echo bar │
│bar │
│> │
│ │
│ │
│ │
│ │
│ │
╰────────────────────────────────────────╯
`;
// TUI Test Snapshot v1

exports[`resize 1`] = String.raw`
╭────────────────────────────────────────────────────────────────────────────────╮
Expand Down

0 comments on commit f6f9c3a

Please sign in to comment.