Skip to content

Latest commit

 

History

History

helper-jest

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

A collection of utility functions for jest tests.

Install

  • npm

    npm install --save-dev @guanghechen/helper-jest
  • yarn

    yarn add --dev @guanghechen/helper-jest

Usage

Desensitizers

  • IStringDesensitizer: (text: string, key?: string) => string

    • composeStringDesensitizers: Compose multiple desensitizers into one.

      function composeStringDesensitizers(
        ...desensitizers: ReadonlyArray<IStringDesensitizer>
      ): IStringDesensitizer
    • createFilepathDesensitizer: Create a IStringDesensitizer to eliminate sensitive filepath data (Replace matched filepath with the replaceString).

      export function createFilepathDesensitizer(
        baseDir: string,
        replaceString = '<WORKSPACE>',
      ): IStringDesensitizer
    • createPackageVersionDesensitizer: Create a IStringDesensitizer to eliminate volatile package versions.

      function createPackageVersionDesensitizer(
        nextVersion: (packageVersion: string, packageName: string) => string,
        testPackageName?: (packageName: string) => boolean,
      ): IStringDesensitizer
      • nextVersion: Determine the new version of the given {packageName} package.

        • @param packageVersion: packageVersion (without prefix)
        • @param packageName: packageName /([\^><~]=?)?/)
        • @returns:
      • testPackageName: Test if the version should be change of the {packageName} package.

        • @param packageName:
  • JsonDesensitizer: (json: unknown, key?: string) => unknown

    • createJsonDesensitizer: Create a desensitizer to eliminate sensitive json data.

      createJsonDesensitizer(
        valDesensitizers: {
          string?: IStringDesensitizer
          number?: INumberDesensitizer
          fallback?: IDesensitizer<unknown>
        },
        keyDesensitizer?: IStringDesensitizer,
      ): JsonDesensitizer
      • valDesensitizers: Desensitizers for values of object / array or literal string and number

        • string: Desensitize number type value of object / array or literal string.
        • number: Desensitize number type value of object / array or literal number.
        • number: Desensitize other type value of object / array or other literal primitive values.
      • keyDesensitizer: Desensitizer for keys of object

snapshots

  • fileSnapshot: Create snapshot for give filepaths.

    fileSnapshot(
      baseDir: string,
      filenames: string[],
      desensitize?: IStringDesensitizer,
      encoding: BufferEncoding = 'utf-8',
    ): void

mocks

  • createConsoleMock: Create mock funcs on console

    export function createConsoleMock(
      methodNames?: ReadonlyArray<'debug' | 'log' | 'info' | 'warn' | 'error'> =
        ['debug', 'log', 'info', 'warn', 'error'],
      desensitize?: (args: unknown[]) => unknown[]
    ): ConsoleMock

    Example:

    const mock = createConsoleMock()
    
    console.debug('debug waw1')
    console.log('log waw2')
    expect(mock.get('debug')).toEqual([['debug waw1']])
    expect(mock.get('log')).toEqual([['log waw2']])
    expect(mock.getIndiscriminateAll()).toEqual([['debug waw1'], ['log waw2']])
    
    mock.reset()
    console.info('info waw3')
    console.error('error waw4')
    expect(mock.get('info')).toEqual([['info waw3']])
    expect(mock.get('error')).toEqual([['error waw4']])
    expect(mock.getIndiscriminateAll()).toEqual([['info waw3'], ['error waw4']])
    
    mock.restore()

Related