(args)
| 53 | * @returns {Promise<{ config: CapturedConfig, exitCode: number, errors: string }>} result |
| 54 | */ |
| 55 | const run = async (args) => { |
| 56 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), "wp-cli-")); |
| 57 | const capture = path.join(dir, "config.json"); |
| 58 | process.env.WEBPACK_PACKAGE = MOCK_WEBPACK; |
| 59 | process.env.WEBPACK_CLI_TEST_CAPTURE = capture; |
| 60 | jest.resetModules(); |
| 61 | |
| 62 | /** @type {EXPECTED_ANY} */ |
| 63 | const WebpackCLI = require("webpack-cli").default; |
| 64 | |
| 65 | const exitSpy = jest.spyOn(process, "exit").mockImplementation((code) => { |
| 66 | throw new Error(`__exit__ ${code}`); |
| 67 | }); |
| 68 | /** @type {string[]} */ |
| 69 | const errors = []; |
| 70 | const errorSpy = jest |
| 71 | .spyOn(console, "error") |
| 72 | .mockImplementation((message) => errors.push(`${message}`)); |
| 73 | let exitCode = 0; |
| 74 | try { |
| 75 | await new WebpackCLI().run(["node", "webpack", "build", ...args]); |
| 76 | } catch (error) { |
| 77 | const match = /__exit__ (\d+)/.exec(/** @type {Error} */ (error).message); |
| 78 | if (!match) throw error; |
| 79 | exitCode = Number(match[1]); |
| 80 | } finally { |
| 81 | exitSpy.mockRestore(); |
| 82 | errorSpy.mockRestore(); |
| 83 | delete process.env.WEBPACK_PACKAGE; |
| 84 | delete process.env.WEBPACK_CLI_TEST_CAPTURE; |
| 85 | } |
| 86 | const config = fs.existsSync(capture) |
| 87 | ? JSON.parse(fs.readFileSync(capture, "utf8")) |
| 88 | : undefined; |
| 89 | fs.rmSync(dir, { recursive: true, force: true }); |
| 90 | return { config, exitCode, errors: errors.join("\n") }; |
| 91 | }; |
| 92 | |
| 93 | describe("WebpackCLI integration", () => { |
| 94 | // webpack-cli requires a newer Node than webpack itself; skip on older versions. |
no test coverage detected