From 877c7746da3e3e793f1e87e35cf84a67e6d77984 Mon Sep 17 00:00:00 2001 From: zackad Date: Sun, 8 Dec 2024 09:31:30 +0700 Subject: [PATCH] test: add option to add prefix and/or suffix to test output With this feature we can use single input to generate several ouput snapshot. --- CHANGELOG.md | 3 +++ tests_config/run_spec.js | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec1c3d4..12dbab9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## unreleased +### Internals +- Add option to add prefix/suffix for test snapshot output. This will allow to reuse single input file to produce several snapshot output with different configuration + --- ## 0.13.0 (2024-12-09) diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index ea98d8d..aaa1ee0 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -29,20 +29,24 @@ const defaultOptions = { * Generate the snapshot file name from the source file name. * * @param {string} sourceFile The sourcefile + * @param {string} prefix Add prefix to output filename + * @param {string} suffix Add suffix to output filename * @returns {string} The snapshot file name */ -function generateSnapshotFileName(sourceFile) { +function generateSnapshotFileName(sourceFile, prefix = "", suffix = "") { const ext = extname(sourceFile); let base = basename(sourceFile, ext); if (base === "unformatted") { base = "formatted"; } - return `${base}.snap${ext}`; + return `${prefix}${base}${suffix}.snap${ext}`; } /** * @typedef RunSpecOptions * @property {string} [source] The source file. Default `"unformatted.twig"`. + * @property {string} [prefix] Add prefix to output filename. + * @property {string} [suffix] Add suffix to output filename. * @property {FormattingOptions} [formatOptions] Combined formatting options. Default `defaultOptions`. */ @@ -61,14 +65,19 @@ function generateSnapshotFileName(sourceFile) { * @returns {Promise} The result to be passed in expect calls. */ export async function run_spec(metaUrl, options = {}) { - const { source = "unformatted.twig", formatOptions = {} } = options; + const { + prefix, + suffix, + source = "unformatted.twig", + formatOptions = {} + } = options; const dirname = fileURLToPath(new URL(".", metaUrl)); const code = readFileSync(resolve(dirname, source), "utf8"); const snapshotFile = resolve( dirname, "__snapshots__", - generateSnapshotFileName(source) + generateSnapshotFileName(source, prefix, suffix) ); const actual = await format(code, { parser: "twig",