MCPcopy Create free account
hub / github.com/FormidableLabs/babel-plugin-transform-define / assertTransform

Function assertTransform

test/index.js:21–56  ·  view source on GitHub ↗
(initial, expected, opts)

Source from the content-addressed store, hash-verified

19 .join(EOL);
20
21const assertTransform = async (initial, expected, opts) => {
22 const transformOpts = {
23 ...opts,
24 // Specify filename to pick up local `.babelrc.js`
25 // https://babeljs.io/docs/en/options#babelrc
26 filename: initial
27 };
28
29 // Note: We trim + EOL to normalize whitespace + give readable error diffs
30 // Also normalize across OS'es.
31 const [actualCode, expectedCode] = await Promise.all([
32 readFile(initial).then((code) => babel.transform(code, transformOpts).code),
33 readFile(expected).then((buf) => buf.toString())
34 ]).then((vals) => vals.map((val) => val
35 .split(/\r?\n/)
36 .join(EOL)
37 .trim() + EOL
38 ));
39
40 const diff = jsdiff.diffLines(actualCode, expectedCode);
41 // Consider no diff or newline-only diff to be "the same".
42 const changes = diff.filter(({ added, removed, value }) => (added || removed) && value.trim());
43 if (changes.length === 0) {
44 return true;
45 }
46
47 const msg = diff
48 .map((obj) => {
49 if (obj.added) { return splitLines(obj, (line) => chalk `{green +${line}}`); }
50 if (obj.removed) { return splitLines(obj, (line) => chalk `{red -${line}}`); }
51 return splitLines(obj, (line) => chalk `{grey ${line}}`);
52 })
53 .join("");
54
55 throw new Error(chalk `{white Difference found ({green actual}, {red expected}): ${EOL}}${msg}`);
56};
57
58const getBabelOps = (pluginOps) => ({
59 presets: ["@babel/preset-env"],

Callers 1

index.jsFile · 0.85

Calls 1

splitLinesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…