next.js/packages/next/src/server/lib/utils.test.ts
utils.test.ts161 lines4.7 KB
import {
  getFormattedNodeOptionsWithoutInspect,
  getParsedDebugAddress,
  formatNodeOptions,
  tokenizeArgs,
  getParsedNodeOptions,
} from './utils'

const originalNodeOptions = process.env.NODE_OPTIONS

afterAll(() => {
  process.env.NODE_OPTIONS = originalNodeOptions
})

describe('tokenizeArgs', () => {
  it('splits arguments by spaces', () => {
    const result = tokenizeArgs('--spaces "thing with spaces" --normal 1234')

    expect(result).toEqual([
      '--spaces',
      'thing with spaces',
      '--normal',
      '1234',
    ])
  })

  it('supports quoted values', () => {
    const result = tokenizeArgs(
      '--spaces "thing with spaces" --spacesAndQuotes "thing with \\"spaces\\"" --normal 1234'
    )

    expect(result).toEqual([
      '--spaces',
      'thing with spaces',
      '--spacesAndQuotes',
      'thing with "spaces"',
      '--normal',
      '1234',
    ])
  })
})

describe('formatNodeOptions', () => {
  it('wraps values with spaces in quotes', () => {
    const result = formatNodeOptions({
      spaces: 'thing with spaces',
      spacesAndQuotes: 'thing with "spaces"',
      normal: '1234',
    })

    expect(result).toEqual({
      execArgv: [],
      nodeOptions:
        '--spaces="thing with spaces" --spacesAndQuotes="thing with \\"spaces\\"" --normal=1234',
    })
    expect(result.execArgv).toEqual([])
  })

  it('separates exec-argv-only options from NODE_OPTIONS', () => {
    const result = formatNodeOptions({
      'enable-source-maps': true,
      'experimental-network-inspection': true,
      'experimental-storage-inspection': true,
      'experimental-worker-inspection': true,
      'experimental-inspector-network-resource': true,
      'max-old-space-size': '4096',
    })

    expect(result).toEqual({
      nodeOptions: '--enable-source-maps --max-old-space-size=4096',
      execArgv: [
        '--experimental-network-inspection',
        '--experimental-storage-inspection',
        '--experimental-worker-inspection',
        '--experimental-inspector-network-resource',
      ],
    })
  })
})

describe('getParsedDebugAddress', () => {
  it('supports the flag with an equal sign', () => {
    process.env.NODE_OPTIONS = '--inspect=1234'
    const nodeOptions = getParsedNodeOptions()
    const result = getParsedDebugAddress(nodeOptions.inspect)
    expect(result).toEqual({ host: undefined, port: 1234 })
  })

  it('supports the flag without an equal sign', () => {
    process.env.NODE_OPTIONS = '--inspect 1234'
    const nodeOptions = getParsedNodeOptions()
    const result = getParsedDebugAddress(nodeOptions.inspect)
    expect(result).toEqual({ host: undefined, port: 1234 })
  })
})

describe('getFormattedNodeOptionsWithoutInspect', () => {
  it('removes --inspect option', () => {
    process.env.NODE_OPTIONS = '--other --inspect --additional'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other --additional')
  })

  it('removes --inspect option at end of line', () => {
    process.env.NODE_OPTIONS = '--other --inspect'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other')
  })

  it('handles options with spaces', () => {
    process.env.NODE_OPTIONS =
      '--other --inspect --additional --spaces "/some/path with spaces"'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe(
      '--other --additional --spaces="/some/path with spaces"'
    )
  })

  it('handles options with quotes', () => {
    process.env.NODE_OPTIONS =
      '--require "./file with spaces to-require-with-node-require-option.js"'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe(
      '--require="./file with spaces to-require-with-node-require-option.js"'
    )
  })

  it('removes --inspect option with parameters', () => {
    process.env.NODE_OPTIONS = '--other --inspect=0.0.0.0:1234 --additional'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other --additional')
  })

  it('removes --inspect-brk option', () => {
    process.env.NODE_OPTIONS = '--other --inspect-brk --additional'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other --additional')
  })

  it('removes --inspect-brk option with parameters', () => {
    process.env.NODE_OPTIONS = '--other --inspect-brk=0.0.0.0:1234 --additional'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other --additional')
  })

  it('ignores unrelated options starting with --inspect-', () => {
    process.env.NODE_OPTIONS =
      '--other --inspect-port=0.0.0.0:1234 --additional'
    const result = getFormattedNodeOptionsWithoutInspect()

    expect(result).toBe('--other --inspect-port=0.0.0.0:1234 --additional')
  })
})
Quest for Codev2.0.0
/
SIGN IN