Skip to content

Commit 9b7cea7

Browse files
committed
using the fixture approach, preparing to delegate this section
1 parent 4164a6b commit 9b7cea7

2 files changed

Lines changed: 52 additions & 37 deletions

File tree

tests/config/run-format-test.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
normalizeDirectory,
99
} from "./utilities.js";
1010
import { format } from "./run-prettier.js";
11+
import { replacePlaceholders } from "./replace-placeholders.js";
1112
import { runTest } from "./run-test.js";
1213
import { shouldThrowOnFormat } from "./utilities.js";
1314

@@ -38,67 +39,81 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
3839
options = { errors: true, ...options };
3940
}
4041

41-
const [parser] = explicitParsers;
42-
const allParsers = [...explicitParsers];
43-
4442
const context = {
4543
dirname,
4644
stringifiedOptions: stringifyOptionsForTitle(rawOptions),
47-
parsers: allParsers,
45+
parsers: [...explicitParsers],
4846
options,
4947
explicitParsers,
5048
rawOptions,
5149
snippets,
5250
};
5351

54-
for (const { name, filename, code, output } of getFixtures(context)) {
52+
for (const fixture of getFixtures(context)) {
53+
const { name, context, filepath } = fixture;
54+
const { stringifiedOptions, parsers } = context;
55+
5556
const title = `${name}${
56-
context.stringifiedOptions ? ` - ${context.stringifiedOptions}` : ""
57+
stringifiedOptions ? ` - ${stringifiedOptions}` : ""
5758
}`;
5859

5960
describe(title, () => {
60-
const formatOptions = {
61-
printWidth: 80,
62-
...options,
63-
filepath: filename,
64-
parser,
65-
};
66-
const shouldThrowOnMainParserFormat = shouldThrowOnFormat(
67-
name,
68-
formatOptions,
69-
);
70-
71-
let mainParserFormatResult;
72-
if (shouldThrowOnMainParserFormat) {
73-
mainParserFormatResult = { options: formatOptions, error: true };
74-
} else {
75-
beforeAll(async () => {
76-
mainParserFormatResult = await format(code, formatOptions);
77-
});
78-
}
61+
const testCases = parsers.map((parser) => getTestCase(fixture, parser));
7962

80-
for (const currentParser of allParsers) {
63+
for (const testCase of testCases) {
8164
const testTitle =
82-
shouldThrowOnMainParserFormat ||
83-
formatOptions.parser !== currentParser
84-
? `[${currentParser}] format`
65+
testCase.expectFail ||
66+
testCase.formatOptions.parser !== testCase.parser
67+
? `[${testCase.parser}] format`
8568
: "format";
8669

8770
test(testTitle, async () => {
8871
await runTest({
89-
parsers: explicitParsers,
72+
parsers,
9073
name,
91-
filename,
92-
code,
93-
output,
94-
parser: currentParser,
95-
mainParserFormatResult,
96-
mainParserFormatOptions: formatOptions,
74+
filename: filepath,
75+
code: testCase.code,
76+
output: testCase.expectedOutput,
77+
parser: testCase.parser,
78+
mainParserFormatResult: await testCase.runFormat(),
79+
mainParserFormatOptions: testCase.formatOptions,
9780
});
9881
});
9982
}
10083
});
10184
}
10285
}
10386

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+
104119
export default runFormatTest;

tests/config/utilities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const normalizeDirectory = (directory) => path.normalize(directory + path.sep);
55
const isErrorTest = (dirname) =>
66
normalizeDirectory(dirname).includes(`${path.sep}_errors_${path.sep}`);
77

8-
const shouldThrowOnFormat = (filename, options) => {
8+
const shouldThrowOnFormat = ({ filename }, options) => {
99
const { errors = {}, parser } = options;
1010
if (errors === true) {
1111
return true;

0 commit comments

Comments
 (0)