Skip to content

Commit c7ba564

Browse files
Fix slow unit test (#17465)
This PR reworks a unit test that created a file in the project root and then proceeded by scanning everything in the git root for candidates. The issue specifically is that with the `.debug/` folder, our project root can grow quite a bit which makes this test slower the more you work on other tests... To fix this we now simply create a tmp folder with only that one test file. 🚀
1 parent eec1bf2 commit c7ba564

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ playwright-report/
77
blob-report/
88
playwright/.cache/
99
target/
10-
.debug
10+
.debug/

packages/@tailwindcss-postcss/src/index.test.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import dedent from 'dedent'
2-
import { unlink, writeFile } from 'node:fs/promises'
2+
import { mkdir, mkdtemp, unlink, writeFile } from 'node:fs/promises'
3+
import { tmpdir } from 'node:os'
4+
import path from 'path'
35
import postcss from 'postcss'
4-
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
6+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
57
import tailwindcss from './index'
68

79
// We give this file path to PostCSS for processing.
@@ -106,14 +108,21 @@ test('@apply can be used without emitting the theme in the CSS file', async () =
106108
})
107109

108110
describe('processing without specifying a base path', () => {
109-
let filepath = `${process.cwd()}/my-test-file.html`
110-
111-
beforeEach(() =>
112-
writeFile(filepath, `<div class="md:[&:hover]:content-['testing_default_base_path']">`),
113-
)
111+
let filepath: string
112+
let dir: string
113+
114+
beforeEach(async () => {
115+
dir = await mkdtemp(path.join(tmpdir(), 'tw-postcss'))
116+
await mkdir(dir, { recursive: true })
117+
filepath = path.join(dir, 'my-test-file.html')
118+
await writeFile(filepath, `<div class="md:[&:hover]:content-['testing_default_base_path']">`)
119+
})
114120
afterEach(() => unlink(filepath))
115121

116122
test('the current working directory is used by default', async () => {
123+
const spy = vi.spyOn(process, 'cwd')
124+
spy.mockReturnValue(dir)
125+
117126
let processor = postcss([tailwindcss({ optimize: { minify: false } })])
118127

119128
let result = await processor.process(`@import "tailwindcss"`, { from: inputCssFilePath() })

0 commit comments

Comments
 (0)