|
8 | 8 | normalizeDirectory, |
9 | 9 | } from "./utilities.js"; |
10 | 10 | import { format } from "./run-prettier.js"; |
| 11 | +import { replacePlaceholders } from "./replace-placeholders.js"; |
11 | 12 | import { runTest } from "./run-test.js"; |
12 | 13 | import { shouldThrowOnFormat } from "./utilities.js"; |
13 | 14 |
|
@@ -38,67 +39,81 @@ function runFormatTest(rawFixtures, explicitParsers, rawOptions) { |
38 | 39 | options = { errors: true, ...options }; |
39 | 40 | } |
40 | 41 |
|
41 | | - const [parser] = explicitParsers; |
42 | | - const allParsers = [...explicitParsers]; |
43 | | - |
44 | 42 | const context = { |
45 | 43 | dirname, |
46 | 44 | stringifiedOptions: stringifyOptionsForTitle(rawOptions), |
47 | | - parsers: allParsers, |
| 45 | + parsers: [...explicitParsers], |
48 | 46 | options, |
49 | 47 | explicitParsers, |
50 | 48 | rawOptions, |
51 | 49 | snippets, |
52 | 50 | }; |
53 | 51 |
|
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 | + |
55 | 56 | const title = `${name}${ |
56 | | - context.stringifiedOptions ? ` - ${context.stringifiedOptions}` : "" |
| 57 | + stringifiedOptions ? ` - ${stringifiedOptions}` : "" |
57 | 58 | }`; |
58 | 59 |
|
59 | 60 | 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)); |
79 | 62 |
|
80 | | - for (const currentParser of allParsers) { |
| 63 | + for (const testCase of testCases) { |
81 | 64 | const testTitle = |
82 | | - shouldThrowOnMainParserFormat || |
83 | | - formatOptions.parser !== currentParser |
84 | | - ? `[${currentParser}] format` |
| 65 | + testCase.expectFail || |
| 66 | + testCase.formatOptions.parser !== testCase.parser |
| 67 | + ? `[${testCase.parser}] format` |
85 | 68 | : "format"; |
86 | 69 |
|
87 | 70 | test(testTitle, async () => { |
88 | 71 | await runTest({ |
89 | | - parsers: explicitParsers, |
| 72 | + parsers, |
90 | 73 | 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, |
97 | 80 | }); |
98 | 81 | }); |
99 | 82 | } |
100 | 83 | }); |
101 | 84 | } |
102 | 85 | } |
103 | 86 |
|
| 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 | + |
104 | 119 | export default runFormatTest; |
0 commit comments