-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcompatibleWithPlugins.test.ts
More file actions
executable file
·283 lines (232 loc) · 7.38 KB
/
Copy pathcompatibleWithPlugins.test.ts
File metadata and controls
executable file
·283 lines (232 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import * as prettier from "prettier";
import { AllOptions } from "../src/types";
import { execSync } from "child_process";
import { mkdirSync, writeFileSync, readFileSync, rmSync } from "fs";
import { join } from "path";
function subject(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
parser: "typescript",
plugins: ["./prettier-plugin-fake/index.js", "prettier-plugin-jsdoc"],
jsdocSpaces: 1,
...options,
} as AllOptions);
}
function subjectTailwindcss(code: string, options: Partial<AllOptions> = {}) {
return prettier.format(code, {
parser: "typescript",
plugins: ["prettier-plugin-tailwindcss", "prettier-plugin-jsdoc"],
jsdocSpaces: 1,
...options,
} as AllOptions);
}
test("Should format regular jsDoc", async () => {
const code = `
import b from "b"
import {k} from "k"
import a from "a"
/**
* function example description that was wrapped by hand
* so it have more then one line and don't end with a dot
* REPEATED TWO TIMES BECAUSE IT WAS EASIER to copy
* function example description that was wrapped by hand
* so it have more then one line.
* @return {Boolean} Description for @returns with s
* @param {String|Number} text - some text description that is very long and needs to be wrapped
* @param {String} [defaultValue="defaultTest"] TODO
* @arg {Number|Null} [optionalNumber]
* @private
*@memberof test
@async
* @examples
* var one = 5
* var two = 10
*
* if(one > 2) { two += one }
* @undefiendTag${" "}
* @undefiendTag {number} name des
*/
const testFunction = (text, defaultValue, optionalNumber) => true
`;
const result = await subject(code);
expect(result).toMatchSnapshot();
expect(await subject(result)).toMatchSnapshot();
});
test("Should format jsDoc default values", async () => {
const result = await subject(`
/**
* @param {String} [arg1="defaultTest"] foo
* @param {number} [arg2=123] the width of the rectangle
* @param {number} [arg3= 123 ]
* @param {number} [arg4= Foo.bar.baz ]
* @param {number|string} [arg5=123] Something. Default is \`"wrong"\`
*/
`);
expect(result).toMatchSnapshot();
expect(await subject(result)).toMatchSnapshot();
});
test("Should convert to single line if necessary", async () => {
const Result1 = await subject(`/** single line description*/`);
const Result2 = await subject(`/**
* single line description
* @example
*/`);
const Result3 = await subject(`/**
* single line description
* @return {Boolean} Always true
* @example
*/`);
expect(Result1).toMatchSnapshot();
expect(Result2).toMatchSnapshot();
expect(Result3).toMatchSnapshot();
});
test("Should compatible with tailwindcss", async () => {
const code = `
/**
* @param {String} [arg1="defaultTest"] foo
* @param {number} [arg2=123] the width of the rectangle
* @param {number} [arg3= 123 ]
* @param {number} [arg4= Foo.bar.baz ]
* @param {number|string} [arg5=123] Something. Default is \`"wrong"\`
*/
`;
const result = await subjectTailwindcss(code);
expect(result).toMatchSnapshot();
});
describe("CLI Compatibility", () => {
// CLI-based tests
const testDir = join(process.cwd(), ".test-cli-temp");
beforeAll(() => {
try {
mkdirSync(testDir, { recursive: true });
} catch (err) {
// Directory might already exist
}
});
afterAll(() => {
try {
rmSync(testDir, { recursive: true, force: true });
} catch (err) {
// Ignore cleanup errors
}
});
function runCommand(command: string): string {
return execSync(`cd ${testDir} && ${command}`, {
timeout: 10000,
encoding: "utf8",
cwd: process.cwd(),
});
}
test("Should format with tailwindcss plugin via CLI without infinite recursion", () => {
const testFile = join(testDir, "test-cli-tailwind.js");
const configFile = join(testDir, ".prettierrc.json");
const code = `/**
* @param {String|Number} text - some text description
* @param {String} [defaultValue="defaultTest"] TODO
* @returns {Boolean} Description for returns
*/
const testFunction = (text, defaultValue) => true;
`;
const prettierConfig = {
semi: false,
tabWidth: 2,
printWidth: 100,
singleQuote: true,
plugins: ["prettier-plugin-tailwindcss", "prettier-plugin-jsdoc"],
};
writeFileSync(testFile, code);
writeFileSync(configFile, JSON.stringify(prettierConfig, null, 2));
const output = runCommand(
`npx prettier --config ".prettierrc.json" "test-cli-tailwind.js"`,
);
expect(output).toBeDefined();
expect(output).toMatchSnapshot();
});
test("Should format via CLI with --write flag", () => {
const testFile = join(testDir, "test-cli-write.js");
const configFile = join(testDir, ".prettierrc.json");
const code = `/**
* @param {number} [arg1=123] the width
* @returns {void}
*/
function myFunc(arg1) {}
`;
const prettierConfig = {
semi: false,
plugins: ["prettier-plugin-tailwindcss", "prettier-plugin-jsdoc"],
};
writeFileSync(testFile, code);
writeFileSync(configFile, JSON.stringify(prettierConfig, null, 2));
runCommand(
`npx prettier --config ".prettierrc.json" --write "test-cli-write.js"`,
);
const formatted = readFileSync(testFile, "utf8");
expect(formatted).toMatchSnapshot();
});
test("Should work with plugins in different orders via CLI", () => {
const testFile = join(testDir, "test-cli-order.ts");
const configFile = join(testDir, ".prettierrc.json");
const code = `/**
* @param {string} name
* @returns {Promise<void>}
*/
async function example(name: string): Promise<void> {}
`;
const configs = [
["prettier-plugin-jsdoc", "prettier-plugin-tailwindcss"],
["prettier-plugin-tailwindcss", "prettier-plugin-jsdoc"],
];
configs.forEach((plugins) => {
const prettierConfig = {
parser: "typescript",
plugins,
};
writeFileSync(testFile, code);
writeFileSync(configFile, JSON.stringify(prettierConfig, null, 2));
const output = runCommand(
`npx prettier --config ".prettierrc.json" "test-cli-order.ts"`,
);
expect(output).toBeDefined();
expect(output).toMatchSnapshot();
});
});
});
describe("combined with a plugin that chains", () => {
function format(code: string, plugins: string[]) {
return prettier.format(code, {
parser: "typescript",
plugins,
} as AllOptions);
}
const code = `/**
* @param {String|Number} text - some text description
* @param {String} [defaultValue="defaultTest"] TODO
* @returns {Boolean} Description for returns
*/
const testFunction = (text, defaultValue) => true;
`;
test("Should format without infinite recursion (chaining plugin first)", async () => {
const result = await format(code, [
"./prettier-plugin-fake-chaining/index.js",
"prettier-plugin-jsdoc",
]);
expect(result).toMatchSnapshot();
});
test("Should format without infinite recursion (chaining plugin last)", async () => {
const result = await format(code, [
"prettier-plugin-jsdoc",
"./prettier-plugin-fake-chaining/index.js",
]);
expect(result).toMatchSnapshot();
});
test("Should format concurrent calls consistently", async () => {
const plugins = [
"./prettier-plugin-fake-chaining/index.js",
"prettier-plugin-jsdoc",
];
const results = await Promise.all([
format(code, plugins),
format(code, plugins),
]);
expect(results).toMatchSnapshot();
});
});