Skip to content

Commit eefe645

Browse files
authored
Cleanup tests (#20053)
This PR does some cleanup work in the tests such that every test is using the custom test helpers instead of creating the compiler itself and using other test helpers such as `optimizeCss` or `pretty`. For tests we have some helpers that introduce a small layer of indirection. While you typically want to avoid indirection, this one allows us to keep the tests the same whenever we make changes to the internals. It also allows us to run through the full core flow where we have to setup a compiler, pass in the CSS, compile the candidates, optimize the CSS and pretty print it for snapshot purposes. Our tests often look like this when you are testing some candidates: ```ts test('…', async () => { expect(await run(['flex', 'hover:flex'])).toMatchInlineSnapshot(` " .flex { display: flex; } @media (hover: hover) { .hover\\:flex:hover { display: flex; } } " `) }) ``` If you need to compile some CSS, we used the following: ```ts test('…', async () => { expect( await compileCss(css` @tailwind utilities; .foo { @apply flex; } `), ).toMatchInlineSnapshot(` " .foo { display: flex; } " `) }) ``` With this PR, I normalized the tests by changing the signatures of those tests slightly: ```ts export async function run( // A list of candidates candidates: string[], // Optionally, custom CSS input = css` @tailwind utilities; `, // Optionally, custom compile options options: Parameters<typeof compile>[1] = {}, ) { let { build } = await compile(input, options) return pretty(optimize(build(candidates)).code) } // Same as above, but without the candidates export async function compileCss(css: string, options: Parameters<typeof compile>[1] = {}) { return run([], css, options) } ``` They are very similar, but if we migrate _everything_ to `run`, then there will be situations where you have to use: ```ts test('…', async () => { expect( await run( [], css` @tailwind utilities; .foo { @apply flex; } `, ), ).toMatchInlineSnapshot(` " .foo { display: flex; } " `) }) ``` Which is a little bit confusing because what does `[]` even mean? The next big change is that a lot of the tests were manually setting up the compiler, passing in the CSS and compiler options, then building the candidates, then manually optimizing the CSS and then manually pretty printing the resulting CSS for snapshot purposes. Other tests, didn't use all those steps and skipped the optimization step for example. This means that not all tests were testing the end-to-end core workflow. This PR uses the tests helpers wherever we could. This now also means that our tests look the same as much as possible. The biggest goal of this was to get everything in a similar state. Future PRs that add additional optimizations will now see those optimizations reflected in the actual test output. ## Test plan 1. All tests still pass - Some test _output_ has changed because the `optimizeCss` will now kick in. But this reflects production better anyway. 2. No source code was changed
1 parent ece4824 commit eefe645

15 files changed

Lines changed: 7773 additions & 8161 deletions

packages/tailwindcss/src/at-import.test.ts

Lines changed: 140 additions & 164 deletions
Large diffs are not rendered by default.

packages/tailwindcss/src/compat/config.test.ts

Lines changed: 1110 additions & 1077 deletions
Large diffs are not rendered by default.

packages/tailwindcss/src/compat/container-config.test.ts

Lines changed: 507 additions & 308 deletions
Large diffs are not rendered by default.

packages/tailwindcss/src/compat/legacy-utilities.test.ts

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { expect, test } from 'vitest'
2-
import { compileCss, run } from '../test-utils/run'
2+
import { run } from '../test-utils/run'
33

44
const css = String.raw
55

66
test('bg-gradient-*', async () => {
77
expect(
8-
await compileCss(
9-
css`
10-
@tailwind utilities;
11-
`,
12-
[
13-
'bg-gradient-to-t',
14-
'bg-gradient-to-tr',
15-
'bg-gradient-to-r',
16-
'bg-gradient-to-br',
17-
'bg-gradient-to-b',
18-
'bg-gradient-to-bl',
19-
'bg-gradient-to-l',
20-
'bg-gradient-to-tl',
21-
],
22-
),
8+
await run([
9+
'bg-gradient-to-t',
10+
'bg-gradient-to-tr',
11+
'bg-gradient-to-r',
12+
'bg-gradient-to-br',
13+
'bg-gradient-to-b',
14+
'bg-gradient-to-bl',
15+
'bg-gradient-to-l',
16+
'bg-gradient-to-tl',
17+
]),
2318
).toMatchInlineSnapshot(`
2419
"
2520
.bg-gradient-to-b {
@@ -67,7 +62,14 @@ test('bg-gradient-*', async () => {
6762

6863
test('max-w-screen', async () => {
6964
expect(
70-
await compileCss(
65+
await run(
66+
[
67+
'max-w-screen-sm',
68+
'max-w-screen-md',
69+
'max-w-screen-lg',
70+
'max-w-screen-xl',
71+
'max-w-screen-2xl',
72+
],
7173
css`
7274
@theme {
7375
--breakpoint-sm: 40rem;
@@ -78,13 +80,6 @@ test('max-w-screen', async () => {
7880
}
7981
@tailwind utilities;
8082
`,
81-
[
82-
'max-w-screen-sm',
83-
'max-w-screen-md',
84-
'max-w-screen-lg',
85-
'max-w-screen-xl',
86-
'max-w-screen-2xl',
87-
],
8883
),
8984
).toMatchInlineSnapshot(`
9085
"
@@ -226,14 +221,7 @@ test('flex-shrink', async () => {
226221

227222
test('start', async () => {
228223
expect(
229-
await compileCss(
230-
css`
231-
@theme {
232-
--spacing: 0.25rem;
233-
--inset-shadowned: 1940px;
234-
}
235-
@tailwind utilities;
236-
`,
224+
await run(
237225
[
238226
'start-shadowned',
239227
'start-auto',
@@ -244,6 +232,13 @@ test('start', async () => {
244232
'-start-4',
245233
'start-[4px]',
246234
],
235+
css`
236+
@theme {
237+
--spacing: 0.25rem;
238+
--inset-shadowned: 1940px;
239+
}
240+
@tailwind utilities;
241+
`,
247242
),
248243
).toMatchInlineSnapshot(`
249244
"
@@ -286,14 +281,7 @@ test('start', async () => {
286281
"
287282
`)
288283
expect(
289-
await compileCss(
290-
css`
291-
@theme reference {
292-
--spacing: 0.25rem;
293-
--inset-shadow-sm: inset 0 1px 1px rgb(0 0 0 / 0.05);
294-
}
295-
@tailwind utilities;
296-
`,
284+
await run(
297285
[
298286
'start-shadow-sm',
299287
'start',
@@ -309,20 +297,20 @@ test('start', async () => {
309297
'-start-4/foo',
310298
'start-[4px]/foo',
311299
],
300+
css`
301+
@theme reference {
302+
--spacing: 0.25rem;
303+
--inset-shadow-sm: inset 0 1px 1px rgb(0 0 0 / 0.05);
304+
}
305+
@tailwind utilities;
306+
`,
312307
),
313308
).toEqual('')
314309
})
315310

316311
test('end', async () => {
317312
expect(
318-
await compileCss(
319-
css`
320-
@theme {
321-
--spacing: 0.25rem;
322-
--inset-shadowned: 1940px;
323-
}
324-
@tailwind utilities;
325-
`,
313+
await run(
326314
[
327315
'end-shadowned',
328316
'end-auto',
@@ -333,6 +321,13 @@ test('end', async () => {
333321
'-end-4',
334322
'end-[4px]',
335323
],
324+
css`
325+
@theme {
326+
--spacing: 0.25rem;
327+
--inset-shadowned: 1940px;
328+
}
329+
@tailwind utilities;
330+
`,
336331
),
337332
).toMatchInlineSnapshot(`
338333
"
@@ -375,14 +370,7 @@ test('end', async () => {
375370
"
376371
`)
377372
expect(
378-
await compileCss(
379-
css`
380-
@theme reference {
381-
--spacing: 0.25rem;
382-
--inset-shadow-sm: inset 0 1px 1px rgb(0 0 0 / 0.05);
383-
}
384-
@tailwind utilities;
385-
`,
373+
await run(
386374
[
387375
'end-shadow-sm',
388376
'end',
@@ -398,6 +386,13 @@ test('end', async () => {
398386
'-end-4/foo',
399387
'end-[4px]/foo',
400388
],
389+
css`
390+
@theme reference {
391+
--spacing: 0.25rem;
392+
--inset-shadow-sm: inset 0 1px 1px rgb(0 0 0 / 0.05);
393+
}
394+
@tailwind utilities;
395+
`,
401396
),
402397
).toEqual('')
403398
})

0 commit comments

Comments
 (0)