| 1 |
import cp from "child_process"; |
| 2 |
import fs from "fs"; |
| 3 |
import path from "path"; |
| 4 |
import stripAnsi from "strip-ansi"; |
| 5 |
|
| 6 |
function execP(cmd, opts) { |
| 7 |
const normalize = buff => stripAnsi(buff + "").trim(); |
| 8 |
|
| 9 |
return new Promise(resolve => { |
| 10 |
cp.exec(cmd, opts, (error, stdout, stderr) => { |
| 11 |
resolve({ |
| 12 |
stdout: normalize(stdout), |
| 13 |
stderr: normalize(stderr || error), |
| 14 |
exitCode: error ? error.code : 0, |
| 15 |
}); |
| 16 |
}); |
| 17 |
}); |
| 18 |
} |
| 19 |
|
| 20 |
describe("missingDependencies", () => { |
| 21 |
it("logs with @babel/cli", async () => { |
| 22 |
const { stdout, stderr, exitCode } = await execP("yarn babel in -d out", { |
| 23 |
cwd: __dirname + "/spawn-fixtures/cli", |
| 24 |
}); |
| 25 |
|
| 26 |
expect(exitCode).not.toBe(0); |
| 27 |
|
| 28 |
expect(stdout.replace(/\(\d+ms\)/, "XXXms")).toMatchInlineSnapshot( |
| 29 |
`"Successfully compiled 2 files with Babel XXXms."`, |
| 30 |
); |
| 31 |
expect(stderr).toMatchInlineSnapshot(` |
| 32 |
"Some polyfills have been added but are not present in your dependencies. |
| 33 |
Please run one of the following commands: |
| 34 |
npm install --save ___a___not_a_real_pkg___ ___b___not_a_real_pkg___ ___c___not_a_real_pkg___ |
| 35 |
yarn add ___a___not_a_real_pkg___ ___b___not_a_real_pkg___ ___c___not_a_real_pkg___" |
| 36 |
`); |
| 37 |
}); |
| 38 |
|
| 39 |
it("logs with webpack", async () => { |
| 40 |
const cwd = path.join(__dirname, "spawn-fixtures/webpack/"); |
| 41 |
const { stdout, stderr, exitCode } = await execP("yarn webpack", { cwd }); |
| 42 |
|
| 43 |
expect(exitCode).not.toBe(0); |
| 44 |
|
| 45 |
const out = stdout |
| 46 |
|
| 47 |
|
| 48 |
.replace(/\x1b\[.*?m/g, "") |
| 49 |
|
| 50 |
|
| 51 |
.replace(/^.*\[built]\s*/s, "") |
| 52 |
|
| 53 |
.replace(new RegExp(cwd.replace(/\\/g, "\\\\"), "g"), "<CWD>/"); |
| 54 |
|
| 55 |
expect(out).toMatchInlineSnapshot(` |
| 56 |
"ERROR in ./src/dep.js |
| 57 |
Module not found: Error: Can't resolve '___a___not_a_real_pkg___' in '<CWD>/src' |
| 58 |
@ ./src/dep.js 1:0-34 |
| 59 |
@ ./src/main.js |
| 60 |
|
| 61 |
ERROR in ./src/main.js |
| 62 |
Module not found: Error: Can't resolve '___b___not_a_real_pkg___' in '<CWD>/src' |
| 63 |
@ ./src/main.js 1:0-34" |
| 64 |
`); |
| 65 |
|
| 66 |
expect(stderr).toMatchInlineSnapshot(` |
| 67 |
"Some polyfills have been added but are not present in your dependencies. |
| 68 |
Please run one of the following commands: |
| 69 |
npm install --save ___a___not_a_real_pkg___ ___b___not_a_real_pkg___ |
| 70 |
yarn add ___a___not_a_real_pkg___ ___b___not_a_real_pkg___" |
| 71 |
`); |
| 72 |
|
| 73 |
const output = await fs.promises.readFile(cwd + "/output.js", "utf8"); |
| 74 |
|
| 75 |
expect(output).toMatch(/Cannot find module '___b___not_a_real_pkg___/); |
| 76 |
expect(output).toMatch(/Cannot find module '___a___not_a_real_pkg___/); |
| 77 |
}); |
| 78 |
|
| 79 |
it("logs with rollup", async () => { |
| 80 |
const { stdout, stderr, exitCode } = await execP("yarn rollup -c", { |
| 81 |
cwd: __dirname + "/spawn-fixtures/rollup", |
| 82 |
}); |
| 83 |
|
| 84 |
expect(exitCode).not.toBe(0); |
| 85 |
|
| 86 |
expect(stdout).toBe(""); |
| 87 |
|
| 88 |
|
| 89 |
const err = stderr |
| 90 |
.replace(/(?<=created output.js in )\d+/, "XXX") |
| 91 |
.replace(/\\/g, "/"); |
| 92 |
|
| 93 |
expect(err).toMatchInlineSnapshot(` |
| 94 |
"src/main.js → output.js... |
| 95 |
(!) Unresolved dependencies |
| 96 |
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency |
| 97 |
___b___not_a_real_pkg___ (imported by src/main.js) |
| 98 |
___a___not_a_real_pkg___ (imported by src/dep.js) |
| 99 |
created output.js in XXXms |
| 100 |
|
| 101 |
Some polyfills have been added but are not present in your dependencies. |
| 102 |
Please run one of the following commands: |
| 103 |
npm install --save ___a___not_a_real_pkg___ ___b___not_a_real_pkg___ |
| 104 |
yarn add ___a___not_a_real_pkg___ ___b___not_a_real_pkg___" |
| 105 |
`); |
| 106 |
}); |
| 107 |
}); |
| 108 |
|
| 109 |
describe("debug", () => { |
| 110 |
it("logs correct reasons for including a polyfill", async () => { |
| 111 |
const { stdout, exitCode } = await execP("yarn babel input.mjs", { |
| 112 |
cwd: __dirname + "/spawn-fixtures/debug", |
| 113 |
}); |
| 114 |
|
| 115 |
const out = stdout |
| 116 |
.replace(new RegExp(__dirname.replace(/\\/g, "\\\\"), "g"), "<CWD>") |
| 117 |
.replace(/\\/g, "/"); |
| 118 |
|
| 119 |
expect(out).toMatchInlineSnapshot(` |
| 120 |
"test: \`DEBUG\` option |
| 121 |
|
| 122 |
Using targets: { |
| 123 |
\\"firefox\\": \\"67\\", |
| 124 |
\\"ie\\": \\"11\\" |
| 125 |
} |
| 126 |
|
| 127 |
Using polyfills with \`entry-global\` method: |
| 128 |
|
| 129 |
[<CWD>/spawn-fixtures/debug/input.mjs] |
| 130 |
The test polyfill entry has been replaced with the following polyfills: |
| 131 |
a { \\"ie\\":\\"11\\" } |
| 132 |
import \\"core-js\\";" |
| 133 |
`); |
| 134 |
|
| 135 |
expect(exitCode).toBe(0); |
| 136 |
}); |
| 137 |
}); |