(initial, expected, opts)
| 19 | .join(EOL); |
| 20 | |
| 21 | const 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 | |
| 58 | const getBabelOps = (pluginOps) => ({ |
| 59 | presets: ["@babel/preset-env"], |
no test coverage detected
searching dependent graphs…