Skip to content

Commit f101793

Browse files
akxthecrypticace
andauthored
Move fixtures' expected outputs to the fixture directories (#298)
* Move fixtures' expected outputs to the fixture directories This makes them easier to diff with conventional tools. * Tweak code a bit --------- Co-authored-by: Jordan Pittman <[email protected]>
1 parent 031e5f1 commit f101793

File tree

12 files changed

+68
-63
lines changed

12 files changed

+68
-63
lines changed

tests/fixtures.test.js

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,66 @@
11
import { exec } from 'node:child_process'
2-
import * as fs from 'node:fs'
2+
import * as fs from 'node:fs/promises'
33
import * as path from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import { promisify } from 'node:util'
6-
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
6+
import { afterAll, beforeAll, describe, test } from 'vitest'
77
import { format, pluginPath } from './utils'
88

99
const __filename = fileURLToPath(import.meta.url)
1010
const __dirname = path.dirname(__filename)
1111

1212
const execAsync = promisify(exec)
1313

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-
2314
let fixtures = [
2415
{
2516
name: 'no prettier config',
2617
dir: 'no-prettier-config',
27-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
18+
ext: 'html',
2819
},
2920
{
3021
name: 'inferred config path',
3122
dir: 'basic',
32-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
23+
ext: 'html',
3324
},
3425
{
3526
name: 'inferred config path (.cjs)',
3627
dir: 'cjs',
37-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
28+
ext: 'html',
3829
},
3930
{
4031
name: 'using esm config',
4132
dir: 'esm',
42-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
33+
ext: 'html',
4334
},
4435
{
4536
name: 'using esm config (explicit path)',
4637
dir: 'esm-explicit',
47-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
38+
ext: 'html',
4839
},
4940
{
5041
name: 'using ts config',
5142
dir: 'ts',
52-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
43+
ext: 'html',
5344
},
5445
{
5546
name: 'using ts config (explicit path)',
5647
dir: 'ts-explicit',
57-
output: '<div class="bg-red-500 sm:bg-hotpink"></div>',
48+
ext: 'html',
5849
},
5950
{
6051
name: 'using v3.2.7',
6152
dir: 'v3-2',
62-
output: '<div class="bg-red-500 sm:bg-tomato"></div>',
53+
ext: 'html',
6354
},
6455
{
6556
name: 'plugins',
6657
dir: 'plugins',
67-
output: '<div class="uppercase foo sm:bar"></div>',
58+
ext: 'html',
6859
},
6960
{
7061
name: 'customizations: js/jsx',
7162
dir: 'custom-jsx',
7263
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>`,
10964
},
11065
]
11166

@@ -120,7 +75,7 @@ let configs = [
12075
},
12176
]
12277

123-
test('explicit config path', async () => {
78+
test.concurrent('explicit config path', async ({ expect }) => {
12479
expect(
12580
await format('<div class="sm:bg-tomato bg-red-500"></div>', {
12681
tailwindConfig: path.resolve(
@@ -134,17 +89,27 @@ test('explicit config path', async () => {
13489
describe('fixtures', () => {
13590
// Temporarily move config files out of the way so they don't interfere with the tests
13691
beforeAll(() =>
137-
Promise.all(configs.map(({ from, to }) => fs.promises.rename(from, to))),
92+
Promise.all(configs.map(({ from, to }) => fs.rename(from, to))),
13893
)
13994

14095
afterAll(() =>
141-
Promise.all(configs.map(({ from, to }) => fs.promises.rename(to, from))),
96+
Promise.all(configs.map(({ from, to }) => fs.rename(to, from))),
14297
)
14398

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())
148113
})
149114
}
150115
})

tests/fixtures/basic/output.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-tomato"></div>

tests/fixtures/cjs/output.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-hotpink"></div>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const a = sortMeFn("p-2 sm:p-1");
2+
const b = sortMeFn({
3+
foo: "p-2 sm:p-1",
4+
});
5+
6+
const c = dontSortFn("sm:p-1 p-2");
7+
const d = sortMeTemplate`p-2 sm:p-1`;
8+
const e = dontSortMeTemplate`sm:p-1 p-2`;
9+
const f = tw.foo`p-2 sm:p-1`;
10+
const g = tw.foo.bar`p-2 sm:p-1`;
11+
const h = no.foo`sm:p-1 p-2`;
12+
const i = no.tw`sm:p-1 p-2`;
13+
const k = tw.foo("p-2 sm:p-1");
14+
const l = tw.foo.bar("p-2 sm:p-1");
15+
const m = no.foo("sm:p-1 p-2");
16+
const n = no.tw("sm:p-1 p-2");
17+
18+
const A = (props) => <div className={props.sortMe} />;
19+
const B = () => <A sortMe="p-2 sm:p-1" dontSort="sm:p-1 p-2" />;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup>
2+
let a = sortMeFn("p-2 sm:p-1");
3+
let b = sortMeFn({ "p-2 sm:p-1": true });
4+
let c = dontSortFn("sm:p-1 p-2");
5+
let d = sortMeTemplate`p-2 sm:p-1`;
6+
let e = dontSortMeTemplate`sm:p-1 p-2`;
7+
</script>
8+
<template>
9+
<div class="p-2 sm:p-1" sortMe="p-2 sm:p-1" dontSortMe="sm:p-1 p-2"></div>
10+
<div :class="{ 'p-2 sm:p-1': true }"></div>
11+
<div :sortMe="{ 'p-2 sm:p-1': true }"></div>
12+
</template>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-hotpink"></div>

tests/fixtures/esm/output.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-hotpink"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-tomato"></div>

tests/fixtures/plugins/output.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="uppercase foo sm:bar"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bg-red-500 sm:bg-hotpink"></div>

0 commit comments

Comments
 (0)