import crypto from 'crypto' ; import { readFileSync } from 'fs' ; import { expect } from '@jest/globals' ; import upath from 'upath' ; import { getConfig } from '../lib/config/defaults' ; import type { RenovateConfig } from '../lib/config/types' ; import * as _logger from '../lib/logger' ; import { platform as _platform } from '../lib/platform' ; import * as _env from '../lib/util/exec/env' ; import * as _fs from '../lib/util/fs' ; import * as _git from '../lib/util/git' ; import * as _hostRules from '../lib/util/host-rules' ; param module export function mocked < T > ( module ) { return module as never ; } param obj export function partial < T > ( obj ) { return obj as T ; } export const fs = mocked ( _fs ) ; export const git = mocked ( _git ) ; export const platform = mocked ( _platform ) ; export const env = mocked ( _env ) ; export const hostRules = mocked ( _hostRules ) ; export const logger = mocked ( _logger ) ; export type { RenovateConfig } ; export const defaultConfig = getConfig ( ) ; export { getConfig } ; function getCallerFileName ( ) { let result = null ; const prepareStackTrace = Error . prepareStackTrace ; const stackTraceLimit = Error . stackTraceLimit ; Error . prepareStackTrace = ( _err , stack ) => stack ; Error . stackTraceLimit = 5 ; try { const err = new Error ( ) ; const stack = err . stack as unknown as NodeJS . CallSite [ ] ; let currentFile = null ; for ( const frame of stack ) { const fileName = frame . getFileName ( ) ; if ( ! currentFile ) { currentFile = fileName ; } else if ( currentFile !== fileName ) { result = fileName ; break ; } } } catch ( e ) { } Error . prepareStackTrace = prepareStackTrace ; Error . stackTraceLimit = stackTraceLimit ; return result ; } export function getFixturePath ( fixtureFile , fixtureRoot = '.' ) { const callerDir = upath . dirname ( getCallerFileName ( ) ) ; return upath . join ( callerDir , fixtureRoot , '__fixtures__' , fixtureFile ) ; } export function loadBinaryFixture ( fixtureFile , fixtureRoot = '.' ) { const fixtureAbsFile = getFixturePath ( fixtureFile , fixtureRoot ) ; return readFileSync ( fixtureAbsFile ) ; } export function loadFixture ( fixtureFile , fixtureRoot = '.' ) { const fixtureAbsFile = getFixturePath ( fixtureFile , fixtureRoot ) ; return readFileSync ( fixtureAbsFile , { encoding : 'utf8' } ) ; } export function loadJsonFixture < T = any > ( fixtureFile , fixtureRoot = '.' ) { const rawFixture = loadFixture ( fixtureFile , fixtureRoot ) ; return JSON . parse ( rawFixture ) as T ; } example export const replacingSerializer = ( search , replacement ) : jest . SnapshotSerializerPlugin => ( { test : ( value ) => typeof value === 'string' && value . includes ( search ) , serialize : ( val , config , indent , depth , refs , printer ) => { const replaced = ( val as string ) . replace ( search , replacement ) ; return printer ( replaced , config , indent , depth , refs ) ; } , } ) ; export function addReplacingSerializer ( from , to ) { expect . addSnapshotSerializer ( replacingSerializer ( from , to ) ) ; } function toHash ( buf ) { return crypto . createHash ( 'sha256' ) . update ( buf ) . digest ( 'hex' ) ; } const bufferSerializer = { test : ( value ) => Buffer . isBuffer ( value ) , serialize : ( val , config , indent , depth , refs , printer ) => { const replaced = toHash ( val ) ; return printer ( replaced , config , indent , depth , refs ) ; } , } ; export function addBufferSerializer ( ) { expect . addSnapshotSerializer ( bufferSerializer ) ; }