Skip to content

Instantly share code, notes, and snippets.

@lars-erik
Created September 15, 2025 07:01
Show Gist options
  • Save lars-erik/6cfa18b0d65bb528d2762c24e0a32cac to your computer and use it in GitHub Desktop.
Save lars-erik/6cfa18b0d65bb528d2762c24e0a32cac to your computer and use it in GitHub Desktop.
A types "shim" for approvaltests.node to avoid the TypeScript compiler trying to recompile approvals.
declare module 'approvals' {
// Scrubbers.d.ts copy
export type Scrubber = (t: string) => string;
type ReplacementFunction = (index: number) => string;
export class Scrubbers {
static createReqexScrubber(regex: RegExp, replacement: string | ReplacementFunction): Scrubber;
static createGuidScrubber(): Scrubber;
/**
* This method exists as a convenient way to get an example scrubber for you to use.
* To use this template, simply inline the method in your IDE.
*/
static templates: {
new(): {};
regexScrubberWithLambda(): Scrubber;
regexScrubber(): Scrubber;
dateScrubber(): Scrubber;
};
static noScrubber(data: any): any;
static guidScrubber(data: string): string;
static multiScrubber(scrubbers: Scrubber[]): (data: any) => any;
}
// Core namer.d.ts copy
export interface CoreNamer {
getApprovedFile(ext: string): string;
getReceivedFile(ext: string): string;
}
export function convertToFilename(name: string): string;
// Namer.d.ts copy
export class Namer implements CoreNamer {
basePath: string;
protected name: string;
constructor(basePath: string, name: string);
protected pathCreator(type: string, ext?: string): string;
getReceivedFile(ext: string): string;
getApprovedFile(ext: string): string;
}
// Core Writer.d.ts copy
export interface Writer {
getFileExtension(): string;
write(filePath: string): void;
}
// Core Reporter.d.ts copy
export interface Reporter {
name: string;
canReportOn(fileName: string): boolean;
report(approvedFileName: string, receivedFileName: string, options?: Partial<Config>): void;
}
// Core ReporterFactory.d.ts copy
export type ReporterLoader = () => Reporter[];
export class ReporterFactory {
static loadReporter(name: string | (string | Reporter)[]): Reporter;
static throwUnknownReporterError(reporter: unknown): never;
static assertValidReporter(reporter: Partial<Reporter>): reporter is Reporter;
static loadAllReporters(reporters: (string | Reporter)[]): Reporter[];
}
// Config.d.ts copy
export interface Config {
reporters: (string | Reporter)[];
normalizeLineEndingsTo: boolean | string;
failOnLineEndingDifferences: boolean;
appendEOL: boolean;
EOL: string;
errorOnStaleApprovedFiles: boolean;
shouldIgnoreStaleApprovedFile: (fileName?: string) => boolean;
stripBOM: boolean;
forceApproveAll: boolean;
blockUntilReporterExits: boolean;
maxLaunches: number;
cmdOptionOverrides?: any;
cmdArgs?: string[];
}
export const defaultConfig: Config;
export function getHomeApprovalConfig(): Config | null;
export function getConfig(configOverrides?: Partial<Config>): Config;
export function configure(overrideOptions?: Partial<Config>): Config;
export function currentConfig(): Config;
export function reset(): void;
// Approvals.d.ts copy
/**
*
* @example
* // basic approval test
* const approvals = require('approvals');
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify");
*
* @example
* // basic approval test providing an option to override configuration
* const approvals = require('approvals');
* approvals.verify(__dirname, 'sample-approval-test', "some text to verify", { normalizeLineEndingsTo: true });
*
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
* @param {string} testName - A file name save string to call the file associated with this test.
* @param {(string|Buffer)} data - Either the string to save as a text file or a Buffer that represents an image
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
*/
export function verify(dirName: string, testName: string, data: any, optionsOverride?: any): void;
/**
* You can pass as "data" any javascript object to be JSON.stringified and run verify against.
*
* @example
* const approvals = require('approvals');
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" });
*
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
* @param {string} testName - A file name safe string to call the file associated with this test.
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
*/
export function verifyAsJSON(dirName: string, testName: string, data: any, optionsOverride: any): void;
/**
* You can pass as "data" any javascript object to be JSON.stringified. Before we run verify the scrubber will be run against the complete string before running verify against it.
* @example
* // basic approval test with a custom scrubber
* const approvals = require('approvals');
* const scrubber = approvals.scrubbers.multiScrubber([
* function (data) {
* return (data || '').replace("some text", "some other text");
* },
* approvals.scrubbers.guidScrubber // to remove guids from the received data
* });
* approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in an object" }, scrubber);
*
* @param {string} dirName - Typically `__dirname` but could be the base-directory (anywhere) to store both approved and received files.
* @param {string} testName - A file name safe string to call the file associated with this test.
* @param {(string|Buffer)} data - This can be any JavaScript object/array that will be JSON.stringified before running verify
* @param {*} scrubber - A function that takes a string and returns a string. Approvals will call this if it exists to scrub the "data" before writing to any files.
* @param {*} optionsOverride - An object that can contain configurational overrides as defined in the approvals configuration object.
*/
export function verifyAsJSONAndScrub(dirName: string, testName: string, data: any, scrubber: Scrubber, optionsOverride: any): void;
/**
* This allows you to take full control of naming and writing files before verifying.
*
* For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
*
* @param {Object} namer
* @param {Object} writer
* @param {Function} [reporterFactory]
* @param {Object} [optionsOverride]
*/
export function verifyWithControl(namer: Namer, writer: Writer, reporterFactory?: ReporterLoader | null, optionsOverride?: Partial<Config>): void;
const _default: {
// Add things from above when we use them.
verifyWithControl: typeof verifyWithControl;
verifyAsJSON: typeof verifyAsJSON;
verifyAsJSONAndScrub: typeof verifyAsJSONAndScrub;
verify: typeof verify;
configure: typeof configure;
};
export default _default;
}
export default approvals;
import approvals from "approvals";
export default approvals;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment