Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Add option to add prefix and/or suffix to test output #105

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 13 additions & 4 deletions tests_config/run_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/

Expand All @@ -61,14 +65,19 @@ function generateSnapshotFileName(sourceFile) {
* @returns {Promise<RunSpecResult>} 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",
Expand Down
Loading