From 33d32bd546000b2f63838283b7d796edef7583e5 Mon Sep 17 00:00:00 2001 From: milahu Date: Mon, 8 Jul 2024 15:50:54 +0200 Subject: [PATCH] feat: add fileExtension option (#29) --- index.d.ts | 1 + index.js | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index e44ec36..4095431 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,6 +10,7 @@ export const toMatchFile: ( declare interface FileMatcherOptions { diff?: DiffOptions; + fileExtension?: string; } declare global { diff --git a/index.js b/index.js index 6c5fc36..45d5090 100644 --- a/index.js +++ b/index.js @@ -22,12 +22,17 @@ const isEqual = (a, b) => { * * @param {string | Buffer} content Output content to match * @param {string} [filepath] Path to the file to match against - * @param {{ diff?: import('jest-diff').DiffOptions }} options Additional options for matching + * @param {{ + * diff?: import('jest-diff').DiffOptions, + * fileExtension?: string, + * }} options * @this {{ testPath: string, currentTestName: string, assertionCalls: number, isNot: boolean, snapshotState: { added: number, updated: number, unmatched: number, _updateSnapshot: 'none' | 'new' | 'all' } }} */ exports.toMatchFile = function toMatchFile(content, filepath, options = {}) { const { isNot, snapshotState } = this; + const fileExtension = options.fileExtension || ''; + const filename = filepath === undefined ? // If file name is not specified, generate one from the test title @@ -37,7 +42,7 @@ exports.toMatchFile = function toMatchFile(content, filepath, options = {}) { `${filenamify(this.currentTestName, { replacement: '-', }).replace(/\s/g, '-')}-${this.assertionCalls}` - ) + ) + fileExtension : filepath; options = {