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