A collection of utility functions for jest tests.
-
npm
npm install --save-dev @guanghechen/helper-jest
-
yarn
yarn add --dev @guanghechen/helper-jest
-
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 thereplaceString
).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 ofobject
/array
or literalstring
andnumber
string
: Desensitizenumber
type value ofobject
/array
or literalstring
.number
: Desensitizenumber
type value ofobject
/array
or literalnumber
.number
: Desensitize other type value ofobject
/array
or other literal primitive values.
-
keyDesensitizer
: Desensitizer for keys of object
-
-
-
fileSnapshot
: Create snapshot for give filepaths.fileSnapshot( baseDir: string, filenames: string[], desensitize?: IStringDesensitizer, encoding: BufferEncoding = 'utf-8', ): void
-
createConsoleMock
: Create mock funcs onconsole
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()