Skip to content

Commit 7d17417

Browse files
committed
moving test format into it's own file
1 parent b7f7572 commit 7d17417

2 files changed

Lines changed: 54 additions & 35 deletions

File tree

tests/config/run-test.js

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { FULL_TEST } from "./constants.js";
22
import { replacePlaceholders } from "./replace-placeholders.js";
33
import { format } from "./run-prettier.js";
4-
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
5-
import createSnapshot from "./utils/create-snapshot.js";
6-
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
74
import * as testAstCompare from "./test-ast-compare.js";
85
import * as testBom from "./test-bom.js";
96
import * as testEndOfLine from "./test-end-of-line.js";
7+
import * as testFormat from "./test-format.js";
108
import * as testSecondFormat from "./test-second-format.js";
119
import * as testBytecodeCompare from "./test-bytecode-compare.js";
1210
import * as testAntlrFormat from "./test-antlr-format.js";
@@ -34,40 +32,15 @@ async function testFixture(fixture) {
3432
let { code, expectedOutput, parser, formatOptions } = testCase;
3533
let formatResult = await testCase.runFormat();
3634

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-
),
35+
await testFormat.run(
36+
code,
37+
formatResult,
38+
parser,
39+
parsers,
40+
expectedOutput,
41+
formatOptions,
5642
);
5743

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-
7144
if (!FULL_TEST) {
7245
return;
7346
}

tests/config/test-format.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
2+
import createSnapshot from "./utils/create-snapshot.js";
3+
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
4+
5+
async function testFormat(
6+
code,
7+
formatResult,
8+
parser,
9+
parsers,
10+
expectedOutput,
11+
formatOptions,
12+
) {
13+
// Verify parsers or error tests
14+
if (formatOptions.parser !== parser) {
15+
const runFormat = () => format(code, formatOptions);
16+
17+
if (shouldThrowOnFormat(name, formatOptions)) {
18+
await expect(runFormat()).rejects.toThrowErrorMatchingSnapshot();
19+
return;
20+
}
21+
22+
// Verify parsers format result should be the same as main parser
23+
output = formatResult.outputWithCursor;
24+
formatResult = await runFormat();
25+
}
26+
27+
// Make sure output has consistent EOL
28+
expect(formatResult.eolVisualizedOutput).toEqual(
29+
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)),
30+
);
31+
32+
// The result is assert to equals to `expectedOutput`
33+
if (typeof expectedOutput === "string") {
34+
expect(formatResult.eolVisualizedOutput).toEqual(
35+
visualizeEndOfLine(expectedOutput),
36+
);
37+
return;
38+
}
39+
40+
// All parsers have the same result, only snapshot the result from main parser
41+
expect(
42+
createSnapshot(formatResult, { parsers, formatOptions }),
43+
).toMatchSnapshot();
44+
}
45+
46+
export { testFormat as run };

0 commit comments

Comments
 (0)