Skip to content

Commit

Permalink
chore(smoke-tests): add expected lazy require failure (npm#7358)
Browse files Browse the repository at this point in the history
With the performance optimizations that have been landing recently, I
thought it would be helpful to create a test case where we expect a lazy
require to trigger an error while npm is updating itself.

We can use this test to model similar tests in the future if we want to
ensure a specific lazy require is not hit during this update path.
  • Loading branch information
lukekarrys authored Apr 11, 2024
1 parent c16dd4e commit 4736b0d
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions smoke-tests/test/npm-replace-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const setupNpmGlobal = async (t, opts) => {

return {
npmRoot: await mock.npmPath('help').then(setup.getNpmRoot),
pathNpm: await which('npm', { path: mock.getPath(), nothrow: true }),
globalNpm: await which('npm', { nothrow: true }),
pathNpx: await which('npx', { path: mock.getPath(), nothrow: true }),
globalNpx: await which('npx', { nothrow: true }),
pathNpm: await which('npm', { path: mock.getPath() }),
globalNpm: await which('npm'),
pathNpx: await which('npx', { path: mock.getPath() }),
globalNpx: await which('npx'),
binContents,
nodeModulesContents,
}
Expand All @@ -52,7 +52,6 @@ t.test('pack and replace global self', async t => {
})

const tarball = await npmLocalTarball()

await npm('install', tarball, '--global')

t.equal(
Expand Down Expand Up @@ -162,3 +161,43 @@ t.test('publish and replace global self', async t => {

t.strictSame(await npmInstall(npmPath), paths)
})

t.test('fail when updating with lazy require', async t => {
const {
npm,
npmLocalTarball,
npmPath,
paths,
} = await setupNpmGlobal(t, {
testdir: {
project: {
'package.json': {
name: 'npm',
version: '999.999.999',
bin: {
npm: './my-new-npm-bin.js',
},
},
'my-new-npm-bin.js': `#!/usr/bin/env node\nconsole.log('This worked!')`,
},
},
})

const tarball = await npmLocalTarball()
await npm('install', tarball, '--global')
await npmPath('pack')

// exit-handler is the last thing called in the code
// so an uncached lazy require within the exit handler will always throw
await fs.writeFile(
join(paths.globalNodeModules, 'npm/lib/utils/exit-handler.js'),
`module.exports = () => require('./LAZY_REQUIRE_CANARY');module.exports.setNpm = () => {}`,
'utf-8'
)

await t.rejects(npmPath('install', 'npm-999.999.999.tgz', '--global'), {
stderr: `Error: Cannot find module './LAZY_REQUIRE_CANARY'`,
}, 'install command fails with lazy require error')

await t.resolveMatch(npmPath(), { stdout: 'This worked!' }, 'bin placement still works')
})

0 comments on commit 4736b0d

Please sign in to comment.