diff --git a/test/lib/next-modes/base.ts b/test/lib/next-modes/base.ts index 955e0f3bbe55b..1eba7c5f1f2d3 100644 --- a/test/lib/next-modes/base.ts +++ b/test/lib/next-modes/base.ts @@ -154,6 +154,18 @@ export class NextInstance { } const tempNodeModulesFolder = 'node_modules_bak' + // Merge customized node_modules and installed node_modules. + // Move files[node_modules] to a temporary folder node_modules_back, + // then copy the modules back to node_modules after installation. + const tempNodeModulesPath = path.join(this.testDir, tempNodeModulesFolder) + const nodeModulesDir = path.join(this.testDir, 'node_modules') + const nodeModulesStats = await fs.stat(nodeModulesDir) + const hasNodeModulesDir = nodeModulesStats.isDirectory() + if (hasNodeModulesDir) { + // Move node_modules to temp folder + await fs.move(nodeModulesDir, tempNodeModulesPath) + } + if (this.files instanceof FileRef) { // if a FileRef is passed directly to `files` we copy the // entire folder to the test directory @@ -165,35 +177,11 @@ export class NextInstance { ) } - // Merge customized node_modules and installed node_modules. - // Move files[node_modules] to a temporary folder node_modules_back, - // then copy the modules back to node_modules after installation. - const tempNodeModulesPath = path.join(this.testDir, tempNodeModulesFolder) - const nodeModulesDir = path.join(this.testDir, 'node_modules') - const nodeModulesStats = await fs.stat(this.files.fsPath) - const hasNodeModulesDir = nodeModulesStats.isDirectory() - if (hasNodeModulesDir) { - // Move node_modules to temp folder - await fs.move(nodeModulesDir, tempNodeModulesPath) - } await fs.copy(this.files.fsPath, this.testDir) - if (hasNodeModulesDir) { - // Move node_modules from temp back to origin - await fs.copy(tempNodeModulesPath, nodeModulesDir) - await fs.remove(tempNodeModulesPath) - } } else { - let nodeModulesFolder: string | undefined for (const filename of Object.keys(this.files)) { const item = this.files[filename] let outputFilename = path.join(this.testDir, filename) - if (filename === 'node_modules') { - nodeModulesFolder = path.join( - this.testDir, - typeof item === 'string' ? item : item.fsPath - ) - outputFilename = path.join(this.testDir, tempNodeModulesFolder) - } if (typeof item === 'string') { await fs.ensureDir(path.dirname(outputFilename)) @@ -202,14 +190,12 @@ export class NextInstance { await fs.copy(item.fsPath, outputFilename) } } + } - if (nodeModulesFolder) { - await fs.copy( - nodeModulesFolder, - path.join(this.testDir, 'node_modules') - ) - await fs.remove(nodeModulesFolder) - } + if (hasNodeModulesDir) { + // Move node_modules from temp back to origin + await fs.copy(tempNodeModulesPath, nodeModulesDir) + await fs.remove(tempNodeModulesPath) } let nextConfigFile = Object.keys(this.files).find((file) =>