From 7b14a80bb0c4bafcad132a71f8f870d366a9273b Mon Sep 17 00:00:00 2001 From: Jesse Maltese Date: Wed, 8 May 2024 11:07:34 -0400 Subject: [PATCH 1/3] Update snapshots to use it() so we can filter tests with -t --- prettier/configuration/run_spec.js | 109 ++++++++++-------- .../tests/__snapshots__/run_tests.js.snap | 50 ++++---- 2 files changed, 86 insertions(+), 73 deletions(-) diff --git a/prettier/configuration/run_spec.js b/prettier/configuration/run_spec.js index 3f38fcd..9d0ecdb 100644 --- a/prettier/configuration/run_spec.js +++ b/prettier/configuration/run_spec.js @@ -6,6 +6,7 @@ import path from "path"; import prettier from "prettier"; import { fileURLToPath } from "url"; import { expect, test } from "@jest/globals"; +import { describe } from "node:test"; const testDirectory = path.dirname(fileURLToPath(import.meta.url)); @@ -23,55 +24,71 @@ expect.addSnapshotSerializer({ }, }); -async function run_spec(dirname, options) { - fs.readdirSync(dirname).forEach(async (filename) => { - const filepath = `${dirname}${filename}`; - if ( - path.extname(filename) !== ".snap" && - fs.lstatSync(filepath).isFile() && - filename[0] !== "." && - filename !== "run_tests.js" - ) { - let rangeStart = 0; - let rangeEnd = Infinity; - let cursorOffset; - const source = read(filepath) - .replace(/\r\n/g, "\n") - .replace("<<>>", (match, offset) => { - rangeStart = offset; - return ""; - }) - .replace("<<>>", (match, offset) => { - rangeEnd = offset; - return ""; - }); +function createTestObject(dirName, fileName, options) { + const filePath = path.join(dirName, fileName); + const isValidTestFile = + path.extname(fileName) !== ".snap" && + fs.lstatSync(filePath).isFile() && + fileName[0] !== "." && + fileName !== "run_tests.js"; - const input = source.replace("<|>", (match, offset) => { - cursorOffset = offset; - return ""; - }); + if (!isValidTestFile) return undefined; - const mergedOptions = Object.assign(mergeDefaultOptions(options || {}), { - filepath, - rangeStart, - rangeEnd, - cursorOffset, - parser: "hubl", - }); + let rangeStart = 0; + let rangeEnd = Infinity; + let cursorOffset; + const source = fs + .readFileSync(filePath, "utf-8") + .replace(/\r\n/g, "\n") + .replace("<<>>", (_, offset) => { + rangeStart = offset; + return ""; + }) + .replace("<<>>", (_, offset) => { + rangeEnd = offset; + return ""; + }); + + const input = source.replace("<|>", (_, offset) => { + cursorOffset = offset; + return ""; + }); + + const mergedOptions = Object.assign(mergeDefaultOptions(options || {}), { + filePath, + rangeStart, + rangeEnd, + cursorOffset, + parser: "hubl", + }); + + return { + fileName, + dirName, + source, + input, + mergedOptions, + }; +} - const output = prettyprint(input, mergedOptions); +async function run_spec(dirName, options) { + const testObjects = fs + .readdirSync(dirName) + .map((fileName) => createTestObject(dirName, fileName, options)) + .filter((testObj) => testObj !== undefined); - test(filename, async () => { - expect( - raw( - source + - "~".repeat(mergedOptions.printWidth) + - "\n" + - (await output), - ), - ).toMatchSnapshot(); + describe("Formatting tests", () => { + testObjects.forEach(async (testObj) => { + const { fileName, source, input, mergedOptions } = testObj; + it(`formats ${fileName} correctly`, async () => { + const output = await prettyprint(input, mergedOptions); + const snapshot = raw( + source + "~".repeat(mergedOptions.printWidth) + "\n" + output, + ); + console.log("Snap", snapshot); + expect(snapshot).toMatchSnapshot(); }); - } + }); }); } @@ -88,10 +105,6 @@ async function prettyprint(src, options) { global.run_spec = run_spec; -function read(filename) { - return fs.readFileSync(filename, "utf8"); -} - /** * Wraps a string in a marker object that is used by `./raw-serializer.js` to * directly print that string in a snapshot without escaping all double quotes. diff --git a/prettier/tests/__snapshots__/run_tests.js.snap b/prettier/tests/__snapshots__/run_tests.js.snap index f004075..44d4611 100644 --- a/prettier/tests/__snapshots__/run_tests.js.snap +++ b/prettier/tests/__snapshots__/run_tests.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`404.html 1`] = ` +exports[`formats 404.html correctly 1`] = ` @@ -570,7 +570,7 @@ templateType: none `; -exports[`contact.html 1`] = ` +exports[`formats contact.html correctly 1`] = ` @@ -4308,7 +4308,7 @@ This is a simple dialog rendered by using a macro and a call block. `; -exports[`ternary.html 1`] = ` +exports[`formats ternary.html correctly 1`] = ` {{ "x" == 0 ? "hello" : "bye" }} {% set x = blue === red ? yellow : orange %} @@ -4331,7 +4331,7 @@ exports[`ternary.html 1`] = ` `; -exports[`website-header.html 1`] = ` +exports[`formats website-header.html correctly 1`] = ` @@ -600,7 +600,7 @@ templateType: none `; -exports[`contact.html 1`] = ` +exports[`formats contact.html correctly 1`] = ` @@ -4338,7 +4338,7 @@ This is a simple dialog rendered by using a macro and a call block. `; -exports[`ternary.html 1`] = ` +exports[`formats ternary.html correctly 1`] = ` {{ "x" == 0 ? "hello" : "bye" }} {% set x = blue === red ? yellow : orange %} @@ -4361,7 +4361,7 @@ exports[`ternary.html 1`] = ` `; -exports[`website-header.html 1`] = ` +exports[`formats website-header.html correctly 1`] = `