Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualize snapshot results #96

Merged
merged 10 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### 🔧 Internal changes

- Added better typing.
- Added a page for viewing Jest SVG snapshots.
- Added a plugin for prettifying Jest SVG outputs.

## [0.3.2] - 2024-09-14

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/docs/99-api/images/jest-svg-viewer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions memory-viz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ To run the test suite, execute the following command:
```console
$ npm run test --workspace=memory-viz
```

#### Viewing Jest SVG snapshots

To easily view the Jest SVG snapshots, run `open memory-viz/src/tests/__snapshots__/snapshot.html` and select the snapshot outputs with the `*.snap` extension:
leowrites marked this conversation as resolved.
Show resolved Hide resolved

![Jest SVG Viewer File Select](../docs/docs/99-api/images/jest-svg-viewer-file-select.png)
leowrites marked this conversation as resolved.
Show resolved Hide resolved

![Jest SVG Viewer](../docs/docs/99-api/images/jest-svg-viewer.png)
2 changes: 1 addition & 1 deletion memory-viz/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const config: Config = {
// slowTestThreshold: 5,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
snapshotSerializers: ["./src/tests/plugins/pretty_svg.ts"],

// The test environment that will be used for testing
testEnvironment: "node",
Expand Down
2 changes: 2 additions & 0 deletions memory-viz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"csstype": "^3.1.3",
"husky": "^9.1.5",
"jest": "^29.7.0",
"node-html-parser": "^6.1.13",
"pretty": "^2.0.0",
"tmp": "^0.2.3",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
Expand Down
973 changes: 608 additions & 365 deletions memory-viz/src/tests/__snapshots__/cli.spec.tsx.snap

Large diffs are not rendered by default.

4,113 changes: 2,694 additions & 1,419 deletions memory-viz/src/tests/__snapshots__/draw.spec.tsx.snap

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions memory-viz/src/tests/__snapshots__/snapshot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>SVG Snapshot Viewer</title>
</head>
<body>
<label>
Load a Jest Snapshot Output
<input
leowrites marked this conversation as resolved.
Show resolved Hide resolved
type="file"
name="file"
id="file"
onchange="renderSVGs(event)"
/>
</label>
<div id="output"></div>
<script>
async function renderSVGs(event) {
try {
clearOutput();
await loadScript(event);

const outputDiv = document.getElementById("output");
const fragment = document.createDocumentFragment();
const parser = new DOMParser();

for (const [key, value] of Object.entries(window.exports)) {
const testName = document.createElement("div");
testName.textContent = key;
fragment.appendChild(testName);

const svgContent = value.trim().replace(/(^"|"$)/g, "");
const doc = parser.parseFromString(
svgContent,
"text/html"
);
fragment.appendChild(doc.firstChild);
}

outputDiv.appendChild(fragment);
} catch (error) {
console.error("Error rendering SVGs:", error);
}
}

function clearOutput() {
window.exports = {};
const outputDiv = document.getElementById("output");
outputDiv.textContent = "";
}

async function loadScript(event) {
return new Promise((resolve, reject) => {
const file = event.target.files.item(0);
if (file) {
const scriptTag = document.createElement("script");
scriptTag.setAttribute(
"src",
URL.createObjectURL(file)
);
scriptTag.setAttribute("type", "text/javascript");
scriptTag.addEventListener("load", resolve);
scriptTag.addEventListener("error", reject);
document.head.appendChild(scriptTag);
} else {
console.log("No file selected.");
resolve();
}
});
}
</script>
</body>
</html>
22 changes: 22 additions & 0 deletions memory-viz/src/tests/plugins/pretty_svg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Custom pretty-format plugin for serializing SVG elements
// See https://github.com/jestjs/jest/tree/main/packages/pretty-format#writing-plugins for more information

import { NewPlugin } from "pretty-format";
import pretty from "pretty";
import { parse, HTMLElement } from "node-html-parser";

export const serialize: NewPlugin["serialize"] = (val: HTMLElement) => {
return pretty(val);
};

export const test: NewPlugin["test"] = (val: any) => {
const root = parse(val);
return root.firstChild.rawTagName === "svg";
};

export const plugin: NewPlugin = {
test,
serialize,
};

export default plugin;
Loading
Loading