Skip to content

Commit 877c774

Browse files
committed
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.
1 parent b9738bf commit 877c774

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## unreleased
44

5+
### Internals
6+
- 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
7+
58
---
69
## 0.13.0 (2024-12-09)
710

tests_config/run_spec.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ const defaultOptions = {
2929
* Generate the snapshot file name from the source file name.
3030
*
3131
* @param {string} sourceFile The sourcefile
32+
* @param {string} prefix Add prefix to output filename
33+
* @param {string} suffix Add suffix to output filename
3234
* @returns {string} The snapshot file name
3335
*/
34-
function generateSnapshotFileName(sourceFile) {
36+
function generateSnapshotFileName(sourceFile, prefix = "", suffix = "") {
3537
const ext = extname(sourceFile);
3638
let base = basename(sourceFile, ext);
3739
if (base === "unformatted") {
3840
base = "formatted";
3941
}
40-
return `${base}.snap${ext}`;
42+
return `${prefix}${base}${suffix}.snap${ext}`;
4143
}
4244

4345
/**
4446
* @typedef RunSpecOptions
4547
* @property {string} [source] The source file. Default `"unformatted.twig"`.
48+
* @property {string} [prefix] Add prefix to output filename.
49+
* @property {string} [suffix] Add suffix to output filename.
4650
* @property {FormattingOptions} [formatOptions] Combined formatting options. Default `defaultOptions`.
4751
*/
4852

@@ -61,14 +65,19 @@ function generateSnapshotFileName(sourceFile) {
6165
* @returns {Promise<RunSpecResult>} The result to be passed in expect calls.
6266
*/
6367
export async function run_spec(metaUrl, options = {}) {
64-
const { source = "unformatted.twig", formatOptions = {} } = options;
68+
const {
69+
prefix,
70+
suffix,
71+
source = "unformatted.twig",
72+
formatOptions = {}
73+
} = options;
6574
const dirname = fileURLToPath(new URL(".", metaUrl));
6675

6776
const code = readFileSync(resolve(dirname, source), "utf8");
6877
const snapshotFile = resolve(
6978
dirname,
7079
"__snapshots__",
71-
generateSnapshotFileName(source)
80+
generateSnapshotFileName(source, prefix, suffix)
7281
);
7382
const actual = await format(code, {
7483
parser: "twig",

0 commit comments

Comments
 (0)