mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-11 22:46:27 +00:00
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import crypto from 'crypto';
|
|
import { expect, jest } from '@jest/globals';
|
|
import { RenovateConfig as _RenovateConfig } from '../lib/config';
|
|
import { getConfig } from '../lib/config/defaults';
|
|
import { platform as _platform } from '../lib/platform';
|
|
import * as _env from '../lib/util/exec/env';
|
|
import * as _gitfs from '../lib/util/gitfs';
|
|
import * as _hostRules from '../lib/util/host-rules';
|
|
|
|
/**
|
|
* Simple wrapper for getting mocked version of a module
|
|
* @param module module which is mocked by `jest.mock`
|
|
*/
|
|
export function mocked<T>(module: T): jest.Mocked<T> {
|
|
return module as never;
|
|
}
|
|
|
|
/**
|
|
* Simply wrapper to create partial mocks.
|
|
* @param obj Object to cast to final type
|
|
*/
|
|
export function partial<T>(obj: Partial<T>): T {
|
|
return obj as T;
|
|
}
|
|
|
|
export const gitfs = mocked(_gitfs);
|
|
export const platform = mocked(_platform);
|
|
export const env = mocked(_env);
|
|
export const hostRules = mocked(_hostRules);
|
|
|
|
// Required because of isolatedModules
|
|
export type RenovateConfig = _RenovateConfig;
|
|
|
|
export const defaultConfig = getConfig();
|
|
|
|
export { getConfig };
|
|
|
|
export function getName(file: string): string {
|
|
const [, name] = /lib\/(.*?)\.spec\.ts$/.exec(file.replace(/\\/g, '/'));
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* Can be used to search and replace strings in jest snapshots.
|
|
* @example
|
|
* expect.addSnapshotSerializer(
|
|
* replacingSerializer(upath.toUnix(gradleDir.path), 'localDir')
|
|
* );
|
|
*/
|
|
export const replacingSerializer = (
|
|
search: string,
|
|
replacement: string
|
|
): 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: string, to: string): void {
|
|
expect.addSnapshotSerializer(replacingSerializer(from, to));
|
|
}
|
|
|
|
function toHash(buf: Buffer): string {
|
|
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
}
|
|
|
|
const bufferSerializer: jest.SnapshotSerializerPlugin = {
|
|
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(): void {
|
|
expect.addSnapshotSerializer(bufferSerializer);
|
|
}
|