Skip to content

Commit ebb63ab

Browse files
committed
moving is error test logic into utilities.js
1 parent a81e55f commit ebb63ab

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

tests/config/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { normalizeDirectory } from "./utilities.js";
44

55
const { __dirname } = createEsmUtils(import.meta);
66

7+
export const FORMAT_SCRIPT_FILENAME = "format.test.js";
8+
79
export const FORMAT_TEST_DIRECTORY = normalizeDirectory(
810
path.join(__dirname, "../format/"),
911
);

tests/config/run-format-test.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,41 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import url from "node:url";
4-
import createEsmUtils from "esm-utils";
4+
import { FORMAT_SCRIPT_FILENAME } from "./constants.js";
55
import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js";
6+
import {
7+
isErrorTest as isErrorTestDirectory,
8+
normalizeDirectory,
9+
} from "./utilities.js";
610
import { format } from "./run-prettier.js";
711
import { runTest } from "./run-test.js";
812
import { shouldThrowOnFormat } from "./utilities.js";
913

10-
const { __dirname } = createEsmUtils(import.meta);
11-
12-
const isTestDirectory = (dirname, name) =>
13-
(dirname + path.sep).startsWith(
14-
path.join(__dirname, "../format", name) + path.sep,
15-
);
16-
17-
function runFormatTest(fixtures, parsers, options) {
18-
let { importMeta, snippets = [] } = fixtures.importMeta
19-
? fixtures
20-
: { importMeta: fixtures };
14+
function runFormatTest(rawFixtures, explicitParsers, rawOptions) {
15+
let { importMeta, snippets = [] } = rawFixtures.importMeta
16+
? rawFixtures
17+
: { importMeta: rawFixtures };
2118

2219
const filename = path.basename(new URL(importMeta.url).pathname);
23-
if (filename !== "format.test.js") {
24-
throw new Error(`Format test should run in file named 'format.test.js'.`);
20+
if (filename !== FORMAT_SCRIPT_FILENAME) {
21+
throw new Error(
22+
`Format test should run in file named '${FORMAT_SCRIPT_FILENAME}'.`,
23+
);
2524
}
2625

27-
const dirname = path.dirname(url.fileURLToPath(importMeta.url));
28-
29-
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
30-
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
31-
dirname,
32-
"misc/parser-inference",
26+
const dirname = normalizeDirectory(
27+
path.dirname(url.fileURLToPath(importMeta.url)),
3328
);
3429

35-
// `IS_ERROR_TESTS` mean to watch errors like:
30+
let options = { ...rawOptions };
31+
32+
// `IS_ERROR_TEST` mean to watch errors like:
3633
// - syntax parser hasn't supported yet
3734
// - syntax errors that should throws
38-
const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors");
39-
if (IS_ERROR_TESTS) {
40-
options = { errors: true, ...options };
41-
}
35+
const isErrorTest = isErrorTestDirectory(dirname);
4236

43-
if (IS_PARSER_INFERENCE_TESTS) {
44-
parsers = [undefined];
37+
if (isErrorTest) {
38+
options = { errors: true, ...options };
4539
}
4640

4741
snippets = snippets.map((test, index) => {
@@ -83,8 +77,8 @@ function runFormatTest(fixtures, parsers, options) {
8377
})
8478
.filter(Boolean);
8579

86-
const [parser] = parsers;
87-
const allParsers = [...parsers];
80+
const [parser] = explicitParsers;
81+
const allParsers = [...explicitParsers];
8882

8983
const stringifiedOptions = stringifyOptionsForTitle(options);
9084

@@ -123,7 +117,7 @@ function runFormatTest(fixtures, parsers, options) {
123117

124118
test(testTitle, async () => {
125119
await runTest({
126-
parsers,
120+
parsers: explicitParsers,
127121
name,
128122
filename,
129123
code,

tests/config/utilities.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import path from "node:path";
22

33
const normalizeDirectory = (directory) => path.normalize(directory + path.sep);
44

5+
const isErrorTest = (dirname) =>
6+
normalizeDirectory(dirname).includes(`${path.sep}_errors_${path.sep}`);
7+
58
const shouldThrowOnFormat = (filename, options) => {
69
const { errors = {}, parser } = options;
710
if (errors === true) {
@@ -17,4 +20,4 @@ const shouldThrowOnFormat = (filename, options) => {
1720
return false;
1821
};
1922

20-
export { normalizeDirectory, shouldThrowOnFormat };
23+
export { normalizeDirectory, isErrorTest, shouldThrowOnFormat };

0 commit comments

Comments
 (0)