Skip to content

Commit b505e9c

Browse files
committed
now that all the files are as close as possible to prettier's current test configuration we update to prettier's version
1 parent 7f2bc55 commit b505e9c

21 files changed

Lines changed: 365 additions & 136 deletions
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { wrap as raw } from "jest-snapshot-serializer-raw";
2-
import { CURSOR_PLACEHOLDER } from "../constants.js";
2+
import { CURSOR_PLACEHOLDER } from "./constants.js";
33
import visualizeEndOfLine from "./visualize-end-of-line.js";
44
import visualizeRange from "./visualize-range.js";
55

tests/config/format-test-setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import runFormatTest from "./run-format-test.js";
1+
import { runFormatTest } from "./run-format-test.js";
22

33
globalThis.runFormatTest = runFormatTest;

tests/config/get-fixtures.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
4-
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
4+
import visualizeEndOfLine from "./visualize-end-of-line.js";
55

6+
/**
7+
@import {Context} from "./run-format-test.js"
8+
@typedef {Exclude<ReturnType<ReturnType<getFixtures>["next"]>["value"], void>} Fixture
9+
*/
10+
11+
/**
12+
@param {Context} context
13+
*/
614
function* getFixtures(context) {
715
yield* getFiles(context);
816
yield* getSnippets(context);
917
}
1018

19+
/**
20+
@param {Context} context
21+
*/
1122
function* getFiles(context) {
1223
const { dirname } = context;
1324
for (const file of fs.readdirSync(dirname, { withFileTypes: true })) {
@@ -36,6 +47,9 @@ function* getFiles(context) {
3647
}
3748
}
3849

50+
/**
51+
@param {Context} context
52+
*/
3953
function* getSnippets(context) {
4054
for (const [index, snippet] of context.snippets.entries()) {
4155
const testCase =

tests/config/replace-placeholders.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const indexProperties = [
1919
},
2020
];
2121

22+
/**
23+
@param {string} originalText
24+
@param {any} originalOptions
25+
*/
2226
function replacePlaceholders(originalText, originalOptions) {
2327
const indexes = indexProperties
2428
.map(({ property, placeholder }) => {

tests/config/run-format-test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,36 @@ import url from "node:url";
33
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
44
import { getFixtures } from "./get-fixtures.js";
55
import { testFixture } from "./run-test.js";
6-
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
6+
import { stringifyOptionsForTitle } from "./stringify-options-for-title.js";
77
import {
88
isErrorTest as isErrorTestDirectory,
99
normalizeDirectory,
1010
} from "./utilities.js";
1111

12+
/**
13+
@typedef {
14+
| string
15+
| { code: string; name?: string; filename?: string; output?: string }
16+
} Snippet
17+
@typedef {{
18+
dirname: string,
19+
stringifiedOptions: string,
20+
parsers: string[],
21+
options: any,
22+
explicitParsers: string[],
23+
rawOptions: any,
24+
snippets: Snippet[],
25+
}} Context
26+
*/
27+
28+
/**
29+
@param {
30+
| ImportMeta
31+
| { importMeta: ImportMeta, snippets?: Snippet[] }
32+
} rawFixtures
33+
@param {string[]} explicitParsers
34+
@param {any} rawOptions
35+
*/
1236
function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
1337
const { importMeta, snippets = [] } = rawFixtures.importMeta
1438
? rawFixtures
@@ -36,6 +60,8 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
3660
options = { errors: true, ...options };
3761
}
3862

63+
// Make sure tests are in correct location
64+
3965
const context = {
4066
dirname,
4167
stringifiedOptions: stringifyOptionsForTitle(rawOptions),
@@ -51,4 +77,4 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
5177
}
5278
}
5379

54-
export default runFormatTest;
80+
export { runFormatTest };

tests/config/run-prettier.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import getPrettier from "./get-prettier.js";
22
import getPlugins from "./get-plugins.js";
33
import { CURSOR_PLACEHOLDER } from "./constants.js";
4-
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
5-
import { replacePlaceholders } from "./replace-placeholders.js";
4+
import visualizeEndOfLine from "./visualize-end-of-line.js";
65

76
async function parse(input, options) {
87
const prettier = await getPrettier();
9-
108
const { ast } = await prettier.__debug.parse(
119
input,
1210
await loadPlugins(options),
@@ -15,11 +13,7 @@ async function parse(input, options) {
1513
return ast;
1614
}
1715

18-
async function format(originalText, originalOptions) {
19-
const { text: input, options } = replacePlaceholders(
20-
originalText,
21-
originalOptions,
22-
);
16+
async function format(input, options) {
2317
const inputWithCursor = insertCursor(input, options.cursorOffset);
2418
const prettier = await getPrettier();
2519

tests/config/run-test.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import * as testAntlrFormat from "./test-antlr-format.js";
1111
import * as testVariantCoverage from "./test-variant-coverage.js";
1212
import { shouldThrowOnFormat } from "./utilities.js";
1313

14-
async function testFixture(fixture) {
14+
/**
15+
@import {Fixture} from "./get-fixtures.js"
16+
@typedef {ReturnType<getTestCase>} TestCase
17+
*/
18+
19+
/**
20+
@param {Fixture} fixture
21+
*/
22+
function testFixture(fixture) {
1523
const { name, context } = fixture;
1624
const { stringifiedOptions, parsers } = context;
1725

@@ -40,94 +48,109 @@ async function testFixture(fixture) {
4048
return name;
4149
},
4250
test: {
43-
run: (testCase) => testFormat.run(testCase, testCaseForSnapshot),
51+
run: (testCase, name) =>
52+
testFormat.run(testCase, name, testCaseForSnapshot),
4453
},
4554
},
4655
{
4756
name: "ast compare",
48-
test: { run: testAstCompare.run },
57+
test: { run: testAstCompare.run, skip: testAstCompare.skip },
4958
skip: () => !FULL_TEST,
5059
},
5160
// The following cases only need run on main parser
5261
{
5362
name: "second format",
54-
test: { run: testSecondFormat.run },
63+
test: { run: testSecondFormat.run, skip: testSecondFormat.skip },
5564
skip: (testCase) => !FULL_TEST || testCase !== testCaseForSnapshot,
5665
},
5766
{
5867
name: "end of line (CRLF)",
59-
test: { run: (testCase) => testEndOfLine.run(testCase, "\r\n") },
68+
test: {
69+
run: (testCase, name) => testEndOfLine.run(testCase, name, "\r\n"),
70+
skip: testEndOfLine.skip,
71+
},
6072
skip: (testCase) => !FULL_TEST || testCase !== testCaseForSnapshot,
6173
},
6274
{
6375
name: "end of line (CR)",
64-
test: { run: (testCase) => testEndOfLine.run(testCase, "\r") },
76+
test: {
77+
run: (testCase, name) => testEndOfLine.run(testCase, name, "\r"),
78+
skip: testEndOfLine.skip,
79+
},
6580
skip: (testCase) => !FULL_TEST || testCase !== testCaseForSnapshot,
6681
},
6782
{
6883
name: "BOM",
69-
test: { run: testBom.run },
84+
test: { run: testBom.run, skip: testBom.skip },
7085
skip: (testCase) => !FULL_TEST || testCase !== testCaseForSnapshot,
7186
},
7287
// The following cases only need run if the parser is Slang
7388
{
7489
name: "ANTLR format",
75-
test: { run: testAntlrFormat.run },
90+
test: { run: testAntlrFormat.run, skip: testAntlrFormat.skip },
7691
skip: (testCase) =>
7792
!FULL_TEST ||
7893
testCase !== testCaseForSnapshot ||
7994
testCase.parser !== "slang",
8095
},
8196
{
8297
name: "bytecode comparison",
83-
test: { run: testBytecodeCompare.run },
98+
test: { run: testBytecodeCompare.run, skip: testBytecodeCompare.skip },
8499
skip: (testCase) =>
85100
!FULL_TEST ||
86101
testCase !== testCaseForSnapshot ||
87102
testCase.parser !== "slang",
88103
},
89104
{
90105
name: "variant coverage",
91-
test: { run: testVariantCoverage.run },
106+
test: { run: testVariantCoverage.run, skip: testVariantCoverage.skip },
92107
skip: (testCase) =>
93108
!FULL_TEST ||
94109
testCase !== testCaseForSnapshot ||
95110
testCase.parser !== "slang",
96111
},
97112
]) {
98113
for (const testCase of testCases) {
99-
if (functionality.skip?.(testCase)) {
114+
if (
115+
functionality.skip?.(testCase) ||
116+
functionality.test.skip?.(testCase)
117+
) {
100118
continue;
101119
}
120+
102121
let { name } = functionality;
103122
if (typeof name === "function") {
104123
name = name(testCase);
105124
} else if (hasMultipleParsers) {
106125
name += ` [${testCase.parser}]`;
107126
}
108-
test(name, async () => {
109-
await functionality.test.run(testCase);
110-
});
127+
128+
functionality.test.run(testCase, name);
111129
}
112130
}
113131
});
114132
}
115133

134+
/**
135+
@param {Fixture} fixture
136+
@param {string} parser
137+
*/
116138
function getTestCase(fixture, parser) {
117139
const { code: originalText, context, filepath } = fixture;
118140

119141
const { text: code, options: formatOptions } = replacePlaceholders(
120142
originalText,
121143
{
122144
printWidth: 80,
123-
...context.options,
124145
filepath,
146+
...context.options,
125147
parser,
126148
},
127149
);
128150

129151
const expectFail = shouldThrowOnFormat(fixture, formatOptions);
130152

153+
/** @type {ReturnType<format> | undefined} */
131154
let promise;
132155

133156
return {
File renamed without changes.

0 commit comments

Comments
 (0)