Skip to content

Commit b7f7572

Browse files
committed
delegating test description completely to run test.
1 parent f860d65 commit b7f7572

2 files changed

Lines changed: 114 additions & 131 deletions

File tree

tests/config/run-format-test.js

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import path from "node:path";
22
import url from "node:url";
33
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
44
import { getFixtures } from "./get-fixtures.js";
5+
import { testFixture } from "./run-test.js";
56
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
67
import {
78
isErrorTest as isErrorTestDirectory,
89
normalizeDirectory,
910
} from "./utilities.js";
10-
import { format } from "./run-prettier.js";
11-
import { replacePlaceholders } from "./replace-placeholders.js";
12-
import { runTest } from "./run-test.js";
13-
import { shouldThrowOnFormat } from "./utilities.js";
1411

1512
function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
1613
const { importMeta, snippets = [] } = rawFixtures.importMeta
@@ -50,70 +47,8 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
5047
};
5148

5249
for (const fixture of getFixtures(context)) {
53-
const { name, context, filepath } = fixture;
54-
const { stringifiedOptions, parsers } = context;
55-
56-
const title = `${name}${
57-
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
58-
}`;
59-
60-
describe(title, () => {
61-
const testCases = parsers.map((parser) => getTestCase(fixture, parser));
62-
63-
for (const testCase of testCases) {
64-
const testTitle =
65-
testCase.expectFail ||
66-
testCase.formatOptions.parser !== testCase.parser
67-
? `[${testCase.parser}] format`
68-
: "format";
69-
70-
test(testTitle, async () => {
71-
await runTest({
72-
parsers,
73-
name,
74-
filename: filepath,
75-
code: testCase.code,
76-
output: testCase.expectedOutput,
77-
parser: testCase.parser,
78-
mainParserFormatResult: await testCase.runFormat(),
79-
mainParserFormatOptions: testCase.formatOptions,
80-
});
81-
});
82-
}
83-
});
50+
testFixture(fixture);
8451
}
8552
}
8653

87-
function getTestCase(fixture, parser) {
88-
const { code: originalText, context, filepath } = fixture;
89-
90-
const { text: code, options: formatOptions } = replacePlaceholders(
91-
originalText,
92-
{
93-
printWidth: 80,
94-
...context.options,
95-
filepath,
96-
parser,
97-
},
98-
);
99-
100-
const expectFail = shouldThrowOnFormat(fixture, formatOptions);
101-
102-
let promise;
103-
104-
return {
105-
context,
106-
parser,
107-
filepath,
108-
originalText,
109-
code,
110-
formatOptions,
111-
expectFail,
112-
expectedOutput: fixture.output,
113-
isEmpty: code.trim() === "",
114-
runFormat: () =>
115-
promise === undefined ? (promise = format(code, formatOptions)) : promise,
116-
};
117-
}
118-
11954
export default runFormatTest;

tests/config/run-test.js

Lines changed: 112 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FULL_TEST } from "./constants.js";
2+
import { replacePlaceholders } from "./replace-placeholders.js";
23
import { format } from "./run-prettier.js";
34
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
45
import createSnapshot from "./utils/create-snapshot.js";
@@ -12,74 +13,121 @@ import * as testAntlrFormat from "./test-antlr-format.js";
1213
import * as testVariantCoverage from "./test-variant-coverage.js";
1314
import { shouldThrowOnFormat } from "./utilities.js";
1415

15-
async function runTest({
16-
parsers,
17-
name,
18-
filename,
19-
code,
20-
output,
21-
parser,
22-
mainParserFormatResult,
23-
mainParserFormatOptions,
24-
}) {
25-
let formatOptions = mainParserFormatOptions;
26-
let formatResult = mainParserFormatResult;
27-
28-
// Verify parsers or error tests
29-
if (
30-
mainParserFormatResult.error ||
31-
mainParserFormatOptions.parser !== parser
32-
) {
33-
formatOptions = { ...mainParserFormatResult.options, parser };
34-
const runFormat = () => format(code, formatOptions);
35-
36-
if (shouldThrowOnFormat(name, formatOptions)) {
37-
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
38-
return;
16+
async function testFixture(fixture) {
17+
const { name, context, filepath } = fixture;
18+
const { stringifiedOptions, parsers } = context;
19+
20+
const title = `${name}${
21+
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
22+
}`;
23+
24+
describe(title, () => {
25+
const testCases = parsers.map((parser) => getTestCase(fixture, parser));
26+
27+
for (const testCase of testCases) {
28+
const testTitle =
29+
testCase.expectFail || testCase.formatOptions.parser !== testCase.parser
30+
? `[${testCase.parser}] format`
31+
: "format";
32+
33+
test(testTitle, async () => {
34+
let { code, expectedOutput, parser, formatOptions } = testCase;
35+
let formatResult = await testCase.runFormat();
36+
37+
// Verify parsers or error tests
38+
if (formatOptions.parser !== parser) {
39+
const runFormat = () => format(code, formatOptions);
40+
41+
if (shouldThrowOnFormat(name, formatOptions)) {
42+
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
43+
return;
44+
}
45+
46+
// Verify parsers format result should be the same as main parser
47+
output = formatResult.outputWithCursor;
48+
formatResult = await runFormat();
49+
}
50+
51+
// Make sure output has consistent EOL
52+
expect(formatResult.eolVisualizedOutput).toEqual(
53+
visualizeEndOfLine(
54+
consistentEndOfLine(formatResult.outputWithCursor),
55+
),
56+
);
57+
58+
// The result is assert to equals to `expectedOutput`
59+
if (typeof expectedOutput === "string") {
60+
expect(formatResult.eolVisualizedOutput).toEqual(
61+
visualizeEndOfLine(expectedOutput),
62+
);
63+
return;
64+
}
65+
66+
// All parsers have the same result, only snapshot the result from main parser
67+
expect(
68+
createSnapshot(formatResult, { parsers, formatOptions }),
69+
).toMatchSnapshot();
70+
71+
if (!FULL_TEST) {
72+
return;
73+
}
74+
await Promise.all(
75+
[
76+
testAntlrFormat.run,
77+
testVariantCoverage.run,
78+
testSecondFormat.run,
79+
testAstCompare.run,
80+
testBom.run,
81+
testBytecodeCompare.run,
82+
]
83+
.map((test) => test(code, formatResult, filepath, formatOptions))
84+
.join(
85+
["\r\n", "\r"].map((eol) =>
86+
testEndOfLine.run(
87+
code,
88+
formatResult,
89+
filepath,
90+
formatOptions,
91+
eol,
92+
),
93+
),
94+
),
95+
);
96+
});
3997
}
98+
});
99+
}
40100

41-
// Verify parsers format result should be the same as main parser
42-
output = mainParserFormatResult.outputWithCursor;
43-
formatResult = await runFormat();
44-
}
101+
function getTestCase(fixture, parser) {
102+
const { code: originalText, context, filepath } = fixture;
45103

46-
// Make sure output has consistent EOL
47-
expect(formatResult.eolVisualizedOutput).toEqual(
48-
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)),
104+
const { text: code, options: formatOptions } = replacePlaceholders(
105+
originalText,
106+
{
107+
printWidth: 80,
108+
...context.options,
109+
filepath,
110+
parser,
111+
},
49112
);
50113

51-
// The result is assert to equals to `output`
52-
if (typeof output === "string") {
53-
expect(formatResult.eolVisualizedOutput).toEqual(
54-
visualizeEndOfLine(output),
55-
);
56-
return;
57-
}
58-
59-
// All parsers have the same result, only snapshot the result from main parser
60-
expect(
61-
createSnapshot(formatResult, { parsers, formatOptions }),
62-
).toMatchSnapshot();
63-
64-
if (!FULL_TEST) {
65-
return;
66-
}
67-
await Promise.all(
68-
[
69-
testAntlrFormat.run,
70-
testVariantCoverage.run,
71-
testSecondFormat.run,
72-
testAstCompare.run,
73-
testBom.run,
74-
testBytecodeCompare.run,
75-
]
76-
.map((run) => run(code, formatResult, filename, formatOptions))
77-
.join(
78-
["\r\n", "\r"].map((eol) =>
79-
testEndOfLine.run(code, formatResult, filename, formatOptions, eol),
80-
),
81-
),
82-
);
114+
const expectFail = shouldThrowOnFormat(fixture, formatOptions);
115+
116+
let promise;
117+
118+
return {
119+
context,
120+
parser,
121+
filepath,
122+
originalText,
123+
code,
124+
formatOptions,
125+
expectFail,
126+
expectedOutput: fixture.output,
127+
isEmpty: code.trim() === "",
128+
runFormat: () =>
129+
promise === undefined ? (promise = format(code, formatOptions)) : promise,
130+
};
83131
}
84132

85-
export { runTest };
133+
export { testFixture };

0 commit comments

Comments
 (0)