Skip to content

Commit

Permalink
test: move translator-default tests from compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
mlrawlings committed Jul 24, 2023
1 parent 957934b commit 59dd026
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 137 deletions.
137 changes: 0 additions & 137 deletions packages/compiler/test/index.test.js

This file was deleted.

132 changes: 132 additions & 0 deletions packages/translator-default/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import fs from "fs";
import path from "path";
import autotest from "mocha-autotest";
import stripAnsi from "strip-ansi";
import escapeRegExp from "escape-string-regexp";
import { compileFileSync } from "@marko/compiler";

const cwdRegExp = new RegExp(escapeRegExp(process.cwd() + "/"), "g");

describe("translator-class", () => {
autotest("fixtures", {
cjs: runTest({ output: "html", modules: "cjs" }),
html: runTest({ output: "html" }),
htmlProduction: runTest({
output: "html",
optimize: true,
}),
vdom: runTest({ output: "dom" }),
vdomProduction: runTest({
output: "dom",
optimize: true,
}),
generated: runTest({ output: "migrate" }),
hydrate: runTest({
output: "hydrate",
resolveVirtualDependency(from, { virtualPath }) {
return virtualPath;
},
}),
});
});

function runTest(config) {
return ({ mode, test, resolve, snapshot }) => {
const testConfigFile = resolve("test.js");
const testConfig = fs.existsSync(testConfigFile)
? require(testConfigFile)
: {};
const templateFile = resolve(testConfig.templateFile || "template.marko");

const compilerConfig = {
...config,
babelConfig: {
...config.babelConfig,
babelrc: false,
configFile: false,
},
writeVersionComment: false,
};

const snapshotsDir = resolve("snapshots");
const name = `snapshots${path.sep + mode}`;

if (!fs.existsSync(snapshotsDir)) {
fs.mkdirSync(snapshotsDir);
}

test(() => {
let output;
let diags;
try {
const result = compileFileSync(templateFile, compilerConfig);
output = result.code;
diags = result.meta.diagnostics;
} catch (err) {
try {
snapshot(stripCwd(stripModuleStackTrace(stripAnsi(err.stack))), {
name: `${name}-error`,
ext: ".txt",
});
return;
} catch {
throw err;
}
}

snapshot(output, {
name,
ext: mode === "generated" ? ".marko" : ".js",
});

if (mode === "generated") {
snapshot(output, {
name,
ext: ".marko",
});

if (diags && diags.length) {
snapshot(printDiags(diags), {
name,
ext: ".diagnostics.txt",
});
}
} else {
snapshot(output, {
name,
ext: ".js",
});
}
});
};
}

function stripCwd(message) {
return message.replace(cwdRegExp, "");
}

function stripModuleStackTrace(message) {
return message.replace(/\r?\n +at (?!packages[/\\])[^\n]+$/gm, "");
}

function printDiags(diags) {
let result = "";

for (const diag of diags) {
result += `${diag.type}${diag.fix ? `[fixable]` : ""}${printLoc(
diag.loc
)}: ${diag.label}\n`;
}

return result;
}

function printLoc(loc) {
if (loc) {
return `(${loc.start.line}:${loc.start.column + 1}-${loc.end.line}:${
loc.end.column + 1
})`;
}

return "";
}

0 comments on commit 59dd026

Please sign in to comment.