mirror of
https://github.com/pnpm/pnpm.git
synced 2026-06-28 01:45:30 -04:00
The 'exec should merge node options with PnP require option' test (added in #12430) hardcoded an unquoted '--require=<path>' expectation. On Windows the .pnp.cjs path contains backslashes, which makeNodeRequireOption quotes and escapes for Node's NODE_OPTIONS tokenizer, so the assertion mismatched and the test failed on Windows runners. Derive the expected NODE_OPTIONS from makeNodeRequireOption itself so the expectation matches the implementation's quoting on every platform.
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
import { beforeEach, expect, jest, test } from '@jest/globals'
|
|
import { makeNodeRequireOption } from '@pnpm/exec.lifecycle'
|
|
import { prepareEmpty } from '@pnpm/prepare'
|
|
|
|
import { DEFAULT_OPTS } from './utils/index.js'
|
|
|
|
jest.unstable_mockModule('execa', () => ({
|
|
safeExeca: jest.fn(),
|
|
sync: jest.fn(),
|
|
}))
|
|
|
|
const { safeExeca: execa } = await import('execa')
|
|
const { exec } = await import('@pnpm/exec.commands')
|
|
|
|
beforeEach(() => {
|
|
jest.mocked(execa).mockClear()
|
|
})
|
|
|
|
test('exec should set npm_config_user_agent', async () => {
|
|
prepareEmpty()
|
|
const userAgent = 'pnpm/0.0.0'
|
|
|
|
await exec.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
selectedProjectsGraph: {},
|
|
userAgent,
|
|
}, ['eslint'])
|
|
|
|
expect(execa).toHaveBeenCalledWith('eslint', [], expect.objectContaining({
|
|
env: expect.objectContaining({
|
|
npm_config_user_agent: userAgent,
|
|
}),
|
|
}))
|
|
})
|
|
|
|
test('exec should set the NODE_OPTIONS env var', async () => {
|
|
prepareEmpty()
|
|
|
|
await exec.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
selectedProjectsGraph: {},
|
|
nodeOptions: '--max-old-space-size=4096',
|
|
}, ['eslint'])
|
|
|
|
expect(execa).toHaveBeenCalledWith('eslint', [], expect.objectContaining({
|
|
env: expect.objectContaining({
|
|
NODE_OPTIONS: '--max-old-space-size=4096',
|
|
}),
|
|
}))
|
|
})
|
|
|
|
test('exec should merge node options with PnP require option', async () => {
|
|
prepareEmpty()
|
|
const pnpPath = path.join(process.cwd(), '.pnp.cjs')
|
|
fs.writeFileSync(pnpPath, '')
|
|
|
|
await exec.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
selectedProjectsGraph: {},
|
|
workspaceDir: undefined,
|
|
nodeOptions: '--max-old-space-size=4096',
|
|
}, ['eslint'])
|
|
|
|
// The require path is quoted and backslash-escaped on Windows, so derive the
|
|
// expected value from the same helper exec uses instead of hardcoding it.
|
|
const { NODE_OPTIONS } = makeNodeRequireOption(pnpPath, { NODE_OPTIONS: '--max-old-space-size=4096' })
|
|
expect(execa).toHaveBeenCalledWith('eslint', [], expect.objectContaining({
|
|
env: expect.objectContaining({ NODE_OPTIONS }),
|
|
}))
|
|
})
|
|
|
|
test('exec should specify the command', async () => {
|
|
prepareEmpty()
|
|
|
|
await expect(exec.handler({
|
|
...DEFAULT_OPTS,
|
|
dir: process.cwd(),
|
|
selectedProjectsGraph: {},
|
|
}, [])
|
|
).rejects.toThrow("'pnpm exec' requires a command to run")
|
|
})
|