Skip to content

Commit aba48b9

Browse files
committed
Move fixture tests to separate file
1 parent be81958 commit aba48b9

File tree

2 files changed

+140
-139
lines changed

2 files changed

+140
-139
lines changed

tests/fixtures.test.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const path = require('path')
2+
const fs = require('fs')
3+
const { exec } = require('child_process')
4+
const { format, pluginPath } = require('./utils')
5+
const { promisify } = require('util')
6+
const execAsync = promisify(exec)
7+
8+
async function formatFixture(name, extension) {
9+
let binPath = path.resolve(__dirname, '../node_modules/.bin/prettier')
10+
let filePath = path.resolve(__dirname, `fixtures/${name}/index.${extension}`)
11+
12+
let cmd = `${binPath} ${filePath} --plugin ${pluginPath}`
13+
14+
return execAsync(cmd).then(({ stdout }) => stdout.trim())
15+
}
16+
17+
let fixtures = [
18+
{
19+
name: 'no prettier config',
20+
dir: 'no-prettier-config',
21+
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
22+
},
23+
{
24+
name: 'inferred config path',
25+
dir: 'basic',
26+
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
27+
},
28+
{
29+
name: 'inferred config path (.cjs)',
30+
dir: 'cjs',
31+
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
32+
},
33+
{
34+
name: 'using esm config',
35+
dir: 'esm',
36+
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
37+
},
38+
{
39+
name: 'using esm config (explicit path)',
40+
dir: 'esm-explicit',
41+
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
42+
},
43+
{
44+
name: 'using ts config',
45+
dir: 'ts',
46+
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
47+
},
48+
{
49+
name: 'using ts config (explicit path)',
50+
dir: 'ts-explicit',
51+
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
52+
},
53+
{
54+
name: 'using v3.2.7',
55+
dir: 'v3-2',
56+
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
57+
},
58+
{
59+
name: 'plugins',
60+
dir: 'plugins',
61+
output: '<div class="uppercase foo sm:bar"></div>',
62+
},
63+
{
64+
name: 'customizations: js/jsx',
65+
dir: 'custom-jsx',
66+
ext: 'jsx',
67+
output: `const a = sortMeFn("p-2 sm:p-1");
68+
const b = sortMeFn({
69+
foo: "p-2 sm:p-1",
70+
});
71+
72+
const c = dontSortFn("sm:p-1 p-2");
73+
const d = sortMeTemplate\`p-2 sm:p-1\`;
74+
const e = dontSortMeTemplate\`sm:p-1 p-2\`;
75+
const f = tw.foo\`p-2 sm:p-1\`;
76+
const g = tw.foo.bar\`p-2 sm:p-1\`;
77+
const h = no.foo\`sm:p-1 p-2\`;
78+
const i = no.tw\`sm:p-1 p-2\`;
79+
80+
const A = (props) => <div className={props.sortMe} />;
81+
const B = () => <A sortMe="p-2 sm:p-1" dontSort="sm:p-1 p-2" />;`,
82+
},
83+
{
84+
name: 'customizations: vue',
85+
dir: 'custom-vue',
86+
ext: 'vue',
87+
output: `<script setup>
88+
let a = sortMeFn("p-2 sm:p-1");
89+
let b = sortMeFn({ "p-2 sm:p-1": true });
90+
let c = dontSortFn("sm:p-1 p-2");
91+
let d = sortMeTemplate\`p-2 sm:p-1\`;
92+
let e = dontSortMeTemplate\`sm:p-1 p-2\`;
93+
</script>
94+
<template>
95+
<div class="p-2 sm:p-1" sortMe="p-2 sm:p-1" dontSortMe="sm:p-1 p-2"></div>
96+
<div :class="{ 'p-2 sm:p-1': true }"></div>
97+
</template>`,
98+
},
99+
]
100+
101+
let configs = [
102+
{
103+
from: __dirname + '/../.prettierignore',
104+
to: __dirname + '/../.prettierignore.testing',
105+
},
106+
{
107+
from: __dirname + '/../prettier.config.js',
108+
to: __dirname + '/../prettier.config.js.testing',
109+
},
110+
]
111+
112+
test('explicit config path', async () => {
113+
expect(
114+
await format('<div class="sm:bg-tomato bg-red-500"></div>', {
115+
tailwindConfig: path.resolve(
116+
__dirname,
117+
'fixtures/basic/tailwind.config.js',
118+
),
119+
}),
120+
).toEqual('<div class="bg-red-500 sm:bg-tomato"></div>')
121+
})
122+
123+
describe('fixtures', () => {
124+
// Temporarily move config files out of the way so they don't interfere with the tests
125+
beforeAll(() =>
126+
Promise.all(configs.map(({ from, to }) => fs.promises.rename(from, to))),
127+
)
128+
129+
afterAll(() =>
130+
Promise.all(configs.map(({ from, to }) => fs.promises.rename(to, from))),
131+
)
132+
133+
for (const fixture of fixtures) {
134+
test(fixture.name, async () => {
135+
let formatted = await formatFixture(fixture.dir, fixture.ext ?? 'html')
136+
expect(formatted).toEqual(fixture.output)
137+
})
138+
}
139+
})

tests/test.js

Lines changed: 1 addition & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
const path = require('path')
2-
const fs = require('fs')
3-
const { exec } = require('child_process')
4-
const { t, yes, no, format, pluginPath } = require('./utils')
5-
const { promisify } = require('util')
6-
const execAsync = promisify(exec)
7-
8-
async function formatFixture(name, extension) {
9-
let binPath = path.resolve(__dirname, '../node_modules/.bin/prettier')
10-
let filePath = path.resolve(__dirname, `fixtures/${name}/index.${extension}`)
11-
12-
let cmd = `${binPath} ${filePath} --plugin ${pluginPath}`
13-
14-
return execAsync(cmd).then(({ stdout }) => stdout.trim())
15-
}
1+
const { t, yes, no, format } = require('./utils')
162

173
let html = [
184
t`<div class="${yes}"></div>`,
@@ -173,90 +159,6 @@ let tests = {
173159
.map((test) => test.map((t) => t.replace(/^;/, ''))),
174160
}
175161

176-
let fixtures = [
177-
{
178-
name: 'no prettier config',
179-
dir: 'no-prettier-config',
180-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
181-
},
182-
{
183-
name: 'inferred config path',
184-
dir: 'basic',
185-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
186-
},
187-
{
188-
name: 'inferred config path (.cjs)',
189-
dir: 'cjs',
190-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
191-
},
192-
{
193-
name: 'using esm config',
194-
dir: 'esm',
195-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
196-
},
197-
{
198-
name: 'using esm config (explicit path)',
199-
dir: 'esm-explicit',
200-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
201-
},
202-
{
203-
name: 'using ts config',
204-
dir: 'ts',
205-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
206-
},
207-
{
208-
name: 'using ts config (explicit path)',
209-
dir: 'ts-explicit',
210-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
211-
},
212-
{
213-
name: 'using v3.2.7',
214-
dir: 'v3-2',
215-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
216-
},
217-
{
218-
name: 'plugins',
219-
dir: 'plugins',
220-
output: '<div class="uppercase foo sm:bar"></div>',
221-
},
222-
{
223-
name: 'customizations: js/jsx',
224-
dir: 'custom-jsx',
225-
ext: 'jsx',
226-
output: `const a = sortMeFn("p-2 sm:p-1");
227-
const b = sortMeFn({
228-
foo: "p-2 sm:p-1",
229-
});
230-
231-
const c = dontSortFn("sm:p-1 p-2");
232-
const d = sortMeTemplate\`p-2 sm:p-1\`;
233-
const e = dontSortMeTemplate\`sm:p-1 p-2\`;
234-
const f = tw.foo\`p-2 sm:p-1\`;
235-
const g = tw.foo.bar\`p-2 sm:p-1\`;
236-
const h = no.foo\`sm:p-1 p-2\`;
237-
const i = no.tw\`sm:p-1 p-2\`;
238-
239-
const A = (props) => <div className={props.sortMe} />;
240-
const B = () => <A sortMe="p-2 sm:p-1" dontSort="sm:p-1 p-2" />;`,
241-
},
242-
{
243-
name: 'customizations: vue',
244-
dir: 'custom-vue',
245-
ext: 'vue',
246-
output: `<script setup>
247-
let a = sortMeFn("p-2 sm:p-1");
248-
let b = sortMeFn({ "p-2 sm:p-1": true });
249-
let c = dontSortFn("sm:p-1 p-2");
250-
let d = sortMeTemplate\`p-2 sm:p-1\`;
251-
let e = dontSortMeTemplate\`sm:p-1 p-2\`;
252-
</script>
253-
<template>
254-
<div class="p-2 sm:p-1" sortMe="p-2 sm:p-1" dontSortMe="sm:p-1 p-2"></div>
255-
<div :class="{ 'p-2 sm:p-1': true }"></div>
256-
</template>`,
257-
},
258-
]
259-
260162
describe('parsers', () => {
261163
for (let parser in tests) {
262164
test(parser, async () => {
@@ -281,44 +183,4 @@ describe('other', () => {
281183
),
282184
).toEqual('<div class="unknown-class group peer container p-0"></div>')
283185
})
284-
285-
test('explicit config path', async () => {
286-
expect(
287-
await format('<div class="sm:bg-tomato bg-red-500"></div>', {
288-
tailwindConfig: path.resolve(
289-
__dirname,
290-
'fixtures/basic/tailwind.config.js',
291-
),
292-
}),
293-
).toEqual('<div class="bg-red-500 sm:bg-tomato"></div>')
294-
})
295-
})
296-
297-
describe('fixtures', () => {
298-
let configs = [
299-
{
300-
from: __dirname + '/../.prettierignore',
301-
to: __dirname + '/../.prettierignore.testing',
302-
},
303-
{
304-
from: __dirname + '/../prettier.config.js',
305-
to: __dirname + '/../prettier.config.js.testing',
306-
},
307-
]
308-
309-
// Temporarily move config files out of the way so they don't interfere with the tests
310-
beforeAll(() =>
311-
Promise.all(configs.map(({ from, to }) => fs.promises.rename(from, to))),
312-
)
313-
314-
afterAll(() =>
315-
Promise.all(configs.map(({ from, to }) => fs.promises.rename(to, from))),
316-
)
317-
318-
for (const fixture of fixtures) {
319-
test(fixture.name, async () => {
320-
let formatted = await formatFixture(fixture.dir, fixture.ext ?? 'html')
321-
expect(formatted).toEqual(fixture.output)
322-
})
323-
}
324186
})

0 commit comments

Comments
 (0)