Files
pnpm/config/reader/test/env.test.ts
Zoltan Kochan 187049055f chore: upgrade @typescript/native-preview to 7.0.0-dev.20260421.2 (#11332)
* chore: upgrade @typescript/native-preview to 7.0.0-dev.20260421.2

- Add explicit `types: ["node"]` to the shared tsconfig because tsgo
  20260421 no longer auto-acquires `@types/*` from `node_modules`.
- Refactor test files to explicitly import jest globals (`describe`,
  `it`, `test`, `expect`, `beforeEach`, etc.) from `@jest/globals`
  instead of relying on `@types/jest` ambient declarations. Under the
  new tsgo build, `import { jest } from '@jest/globals'` shadows the
  ambient `jest` namespace, breaking `@types/jest`'s `declare var
  describe: jest.Describe;` globals.
- Add `@jest/globals` to each package's devDependencies where tests
  now import from it, and add `@types/node` to packages that need it
  but were relying on hoisted resolution.
- Replace `fail()` calls with `throw new Error(...)` since `fail` is
  no longer globally available.

* chore: fix remaining tsgo type-strictness errors

- Strip `as <PnpmType>` casts on objects passed to toMatchObject /
  toStrictEqual / toEqual; @jest/globals rejects the typed objects
  (which include AsymmetricMatchers) vs. the repo-specific type.
- Type `jest.fn<...>()` explicitly where the mock's signature matters
  for toHaveBeenCalledWith.
- Replace `beforeEach(() => X)` with `beforeEach(() => { X })` so the
  return value is void, as the stricter jest typing requires.
- Use `expect.objectContaining({...})` in one place where the full
  expected object triggered stricter type resolution.
- Cast `prompt.mock.calls` arg through `as unknown as Record<...>[]`
  for patch.test.ts's nested-array matchers.
- Fix off-by-one `<reference path>` in pnpm/test/getConfig.test.ts
  that only surfaced now.
- Move `@jest/globals` from devDependencies to dependencies in the
  two `__utils__` packages that import it from `src/`.
- Clean up unused imports from the @jest/globals migration.

* chore: address Copilot review on #11332

- Move misplaced `@jest/globals` imports to the top import block in
  checkEngine, run.ts, and workspace/root-finder tests where the
  script dropped them below executable code.
- Replace `try { await x(); throw new Error('should have thrown') } catch`
  in bins/linker, lockfile/fs, and resolving/local-resolver tests with
  `await expect(x()).rejects.toMatchObject({...})`. The old pattern
  swallowed an unrelated `throw` if the under-test call silently
  succeeded, which would fail on the catch-block assertion with a
  misleading message.
2026-04-21 22:50:40 +02:00

238 lines
7.4 KiB
TypeScript

import path from 'node:path'
import url from 'node:url'
import { expect, test } from '@jest/globals'
import { type ConfigPair, type GetSchema, parseEnvVars, type Schema } from '../src/env.js'
function assertSchemaKey (key: string): void {
const strictlyKebabCase = key
.split('-')
.every(segment => /^[a-z0-9]+$/.test(segment))
if (!strictlyKebabCase) {
throw new Error(`Key ${key} is not strictly kebab-case`)
}
}
const schemaGetter = (getSchema: GetSchema): GetSchema => key => {
assertSchemaKey(key)
return getSchema(key)
}
const schemaDict = (dict: Record<string, Schema | undefined>): GetSchema => schemaGetter(key => dict[key])
const alwaysSchema = (schema: Schema): GetSchema => schemaGetter(() => schema)
const pairsToObject = <Value> (pairs: Iterable<ConfigPair<Value>>): Record<string, Value> =>
Object.fromEntries(Array.from(pairs).map(({ key, value }) => [key, value]))
test('parseEnvVars works with strings', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(String), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_abc_def_ghi: 'value of abcDefGhi',
pnpm_config_foo: 'value of foo',
pnpm_config_bar: 'value of bar',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
abcDefGhi: 'value of abcDefGhi',
foo: 'value of foo',
bar: 'value of bar',
})
})
test('parseEnvVars works with numbers', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(Number), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_abc_def_ghi: '123',
pnpm_config_foo: '456',
pnpm_config_bar: '789',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
abcDefGhi: 123,
foo: 456,
bar: 789,
})
})
test('parseEnvVars works with booleans', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(Boolean), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_foo: 'false',
pnpm_config_bar: 'true',
pnpm_config_baz: 'not a boolean',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
foo: false,
bar: true,
baz: undefined,
})
})
test('parseEnvVars works with arrays', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(Array), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_foo: '[0, 1, 2]',
pnpm_config_bar: '["a", "b"]',
pnpm_config_baz: 'not an array',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
foo: [0, 1, 2],
bar: ['a', 'b'],
baz: undefined,
})
})
test('parseEnvVars works with paths', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(path), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_foo: 'abc/def/ghi',
pnpm_config_bar: '~/abc/def/ghi',
pnpm_config_baz: '~\\abc\\def\\ghi',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
foo: 'abc/def/ghi',
bar: path.join('/home/fake-user', 'abc/def/ghi'),
baz: path.join('/home/fake-user', 'abc\\def\\ghi'),
})
expect(pairsToObject(parseEnvVars(alwaysSchema(path), {
pnpm_config_foo: 'abc/def/ghi',
pnpm_config_bar: '~/abc/def/ghi',
pnpm_config_baz: '~\\abc\\def\\ghi',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
foo: 'abc/def/ghi',
bar: '~/abc/def/ghi',
baz: '~\\abc\\def\\ghi',
})
})
test('parseEnvVars works with URLs', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(url), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_foo: 'https://registry.npmjs.com',
pnpm_config_bar: 'http://example.org',
pnpm_config_baz: 'file:///path/to/some/local/file',
pnpm_config_undefined_somehow: undefined,
}))).toStrictEqual({
foo: 'https://registry.npmjs.com/',
bar: 'http://example.org/',
baz: 'file:///path/to/some/local/file',
})
})
test('parseEnvVars works with literals', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(['foo', 'bar', true, null]), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_a: 'foo',
pnpm_config_b: 'bar',
pnpm_config_c: 'baz',
pnpm_config_d: 'false',
pnpm_config_e: 'true',
pnpm_config_f: 'null',
}))).toStrictEqual({
a: 'foo',
b: 'bar',
c: undefined,
d: undefined,
e: true,
f: null,
})
})
test('parseEnvVars works with union', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(['foo', 'bar', Number, Array]), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_a: 'foo',
pnpm_config_b: 'bar',
pnpm_config_c: 'baz',
pnpm_config_d: '123',
pnpm_config_e: '456',
pnpm_config_f: '[0, 1, "abc"]',
}))).toStrictEqual({
a: 'foo',
b: 'bar',
c: undefined,
d: 123,
e: 456,
f: [0, 1, 'abc'],
})
})
test('parseEnvVars prioritizes parsing non-strings', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema([String, Number, Array, Boolean]), {
HOME: '/home/fake-user',
PATH: '/bin:/usr/bin:/usr/local/bin:/home/fake-user/.bin:/home/fake-user/share/local/bin',
pnpm_config_a: 'foo',
pnpm_config_b: 'bar',
pnpm_config_c: 'baz',
pnpm_config_d: '123',
pnpm_config_e: '456',
pnpm_config_f: '[0, 1, "abc"]',
}))).toStrictEqual({
a: 'foo',
b: 'bar',
c: 'baz',
d: 123,
e: 456,
f: [0, 1, 'abc'],
})
})
test('parseEnvVars skips undefined schema', () => {
expect(pairsToObject(parseEnvVars(schemaDict({
foo: String,
bar: String,
}), {
pnpm_config_foo: 'from foo',
pnpm_config_bar: 'from bar',
pnpm_config_baz: 'from baz',
}))).toStrictEqual({
foo: 'from foo',
bar: 'from bar',
})
})
test('parseEnvVars skips npm_config_*', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(String), {
npm_config_abc_def_ghi: 'value of abcDefGhi',
npm_config_foo: 'value of foo',
npm_config_bar: 'value of bar',
}))).toStrictEqual({})
})
test('parseEnvVars only reads lower snake case keys', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(String), {
PNPM_CONFIG_UPPER_SNAKE_CASE_KEY: 'whole key in upper snake case',
pnpmConfigCamelCaseKey: 'whole key in snake case',
'pnpm-config-kebab-case': 'whole key in kebab case',
pnpm_config_UPPER_SNAKE_CASE_SUFFIX: 'suffix in upper snake case',
pnpm_config_camelCaseSuffix: 'suffix in camel case',
'pnpm_config_kebab-case-suffix': 'suffix in kebab case',
pnpm_config_lower_snake_case_key: 'whole key in lower snake case',
}))).toStrictEqual({
lowerSnakeCaseKey: 'whole key in lower snake case',
})
})
test('parseEnvVars skips keys that contain multiple consecutive underscore characters', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(String), {
pnpm_config_foo__bar: 'foo bar',
pnpm_config_abc_def__ghi___jkl: 'abc def ghi jkl',
}))).toStrictEqual({})
})
test('parseEnvVars skips keys that end with underscore character', () => {
expect(pairsToObject(parseEnvVars(alwaysSchema(String), {
pnpm_config_foo_bar_: 'foo bar',
}))).toStrictEqual({})
})