|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | 3 | import url from "node:url"; |
4 | | -import createEsmUtils from "esm-utils"; |
| 4 | +import { FORMAT_SCRIPT_FILENAME } from "./constants.js"; |
5 | 5 | import { stringifyOptionsForTitle } from "./utils/stringify-options-for-title.js"; |
| 6 | +import { |
| 7 | + isErrorTest as isErrorTestDirectory, |
| 8 | + normalizeDirectory, |
| 9 | +} from "./utilities.js"; |
6 | 10 | import { format } from "./run-prettier.js"; |
7 | 11 | import { runTest } from "./run-test.js"; |
8 | 12 | import { shouldThrowOnFormat } from "./utilities.js"; |
9 | 13 |
|
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 }; |
21 | 18 |
|
22 | 19 | 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 | + ); |
25 | 24 | } |
26 | 25 |
|
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)), |
33 | 28 | ); |
34 | 29 |
|
35 | | - // `IS_ERROR_TESTS` mean to watch errors like: |
| 30 | + let options = { ...rawOptions }; |
| 31 | + |
| 32 | + // `IS_ERROR_TEST` mean to watch errors like: |
36 | 33 | // - syntax parser hasn't supported yet |
37 | 34 | // - 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); |
42 | 36 |
|
43 | | - if (IS_PARSER_INFERENCE_TESTS) { |
44 | | - parsers = [undefined]; |
| 37 | + if (isErrorTest) { |
| 38 | + options = { errors: true, ...options }; |
45 | 39 | } |
46 | 40 |
|
47 | 41 | snippets = snippets.map((test, index) => { |
@@ -83,8 +77,8 @@ function runFormatTest(fixtures, parsers, options) { |
83 | 77 | }) |
84 | 78 | .filter(Boolean); |
85 | 79 |
|
86 | | - const [parser] = parsers; |
87 | | - const allParsers = [...parsers]; |
| 80 | + const [parser] = explicitParsers; |
| 81 | + const allParsers = [...explicitParsers]; |
88 | 82 |
|
89 | 83 | const stringifiedOptions = stringifyOptionsForTitle(options); |
90 | 84 |
|
@@ -123,7 +117,7 @@ function runFormatTest(fixtures, parsers, options) { |
123 | 117 |
|
124 | 118 | test(testTitle, async () => { |
125 | 119 | await runTest({ |
126 | | - parsers, |
| 120 | + parsers: explicitParsers, |
127 | 121 | name, |
128 | 122 | filename, |
129 | 123 | code, |
|
0 commit comments