11"use strict" ;
22
3+ const os = require ( "node:os" ) ;
34const path = require ( "node:path" ) ;
45const testUtilsPkg = require ( "../utils/test-utils" ) ;
56
@@ -8,6 +9,35 @@ const nodeVersion = Number.parseInt(process.versions.node.split(".")[0], 10);
89// eslint-disable-next-line jsdoc/reject-any-type
910/** @typedef {Record<string, any> } TestOptions */
1011
12+ // The generators install real packages. Skip the requests the tests don't need, so fewer
13+ // requests can fail. `prefer-offline` isn't used: with a stale cache, npm misses new
14+ // versions that other packages already depend on.
15+ const packageManagerEnv = {
16+ npm_config_audit : "false" ,
17+ npm_config_fund : "false" ,
18+ npm_config_update_notifier : "false" ,
19+ // Yarn 1 breaks its cache when two installs fill it at the same time, as tests in parallel
20+ // Jest workers do, so each worker gets its own
21+ YARN_CACHE_FOLDER : path . join (
22+ os . tmpdir ( ) ,
23+ `webpack-cli-yarn-cache-${ process . env . JEST_WORKER_ID || 1 } ` ,
24+ ) ,
25+ } ;
26+
27+ /**
28+ * Fail with the package manager's error, rather than with the output the test misses.
29+ * @template {{ stderr: string }} T
30+ * @param {T } result result of a CLI run
31+ * @returns {T } the same result
32+ */
33+ const checkInstall = ( result ) => {
34+ if ( result . stderr && result . stderr . includes ( "Error occurred while installing packages" ) ) {
35+ throw new Error ( `Installing packages failed:\n${ result . stderr } ` ) ;
36+ }
37+
38+ return result ;
39+ } ;
40+
1141function createPathDependentUtils ( cli ) {
1242 const CLI_PATH = path . resolve ( __dirname , `../../packages/${ cli } /bin/cli.js` ) ;
1343
@@ -19,10 +49,13 @@ function createPathDependentUtils(cli) {
1949 * @returns {Promise<import("execa").Result> } child process
2050 */
2151 const run = async ( cwd , args = [ ] , options = { } ) =>
22- testUtilsPkg . run ( cwd , args , {
23- ...options ,
24- executorPath : CLI_PATH ,
25- } ) ;
52+ checkInstall (
53+ await testUtilsPkg . run ( cwd , args , {
54+ ...options ,
55+ env : { ...packageManagerEnv , ...options . env } ,
56+ executorPath : CLI_PATH ,
57+ } ) ,
58+ ) ;
2659
2760 /**
2861 * Run the webpack CLI in watch mode for a test case.
@@ -34,6 +67,7 @@ function createPathDependentUtils(cli) {
3467 const runWatch = async ( cwd , args = [ ] , options = { } ) =>
3568 testUtilsPkg . runWatch ( cwd , args , {
3669 ...options ,
70+ env : { ...packageManagerEnv , ...options . env } ,
3771 executorPath : CLI_PATH ,
3872 } ) ;
3973
@@ -46,10 +80,13 @@ function createPathDependentUtils(cli) {
4680 * @returns {Promise<{ stdout: string, stderr: string }> } result
4781 */
4882 const runPromptWithAnswers = async ( cwd , args , answers = [ ] , options = { } ) =>
49- testUtilsPkg . runPromptWithAnswers ( cwd , args , answers , {
50- ...options ,
51- executorPath : CLI_PATH ,
52- } ) ;
83+ checkInstall (
84+ await testUtilsPkg . runPromptWithAnswers ( cwd , args , answers , {
85+ ...options ,
86+ env : { ...packageManagerEnv , ...options . env } ,
87+ executorPath : CLI_PATH ,
88+ } ) ,
89+ ) ;
5390
5491 return { run, runWatch, runPromptWithAnswers } ;
5592}
0 commit comments