-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move translator-default tests from compiler
- Loading branch information
1 parent
957934b
commit 59dd026
Showing
2 changed files
with
132 additions
and
137 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} |