From c7ba564f926b509048681c0a93d1e3ed3c49eec1 Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Mon, 31 Mar 2025 15:26:01 +0200 Subject: [PATCH] Fix slow unit test (#17465) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. 🚀 --- .gitignore | 2 +- .../@tailwindcss-postcss/src/index.test.ts | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 9d75bba27..bbac7d227 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ playwright-report/ blob-report/ playwright/.cache/ target/ -.debug +.debug/ diff --git a/packages/@tailwindcss-postcss/src/index.test.ts b/packages/@tailwindcss-postcss/src/index.test.ts index fa9d5abbb..9415fcc4d 100644 --- a/packages/@tailwindcss-postcss/src/index.test.ts +++ b/packages/@tailwindcss-postcss/src/index.test.ts @@ -1,7 +1,9 @@ import dedent from 'dedent' -import { unlink, writeFile } from 'node:fs/promises' +import { mkdir, mkdtemp, unlink, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'path' import postcss from 'postcss' -import { afterEach, beforeEach, describe, expect, test } from 'vitest' +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' import tailwindcss from './index' // 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 () = }) describe('processing without specifying a base path', () => { - let filepath = `${process.cwd()}/my-test-file.html` + let filepath: string + let dir: string - beforeEach(() => - writeFile(filepath, `
`), - ) + beforeEach(async () => { + dir = await mkdtemp(path.join(tmpdir(), 'tw-postcss')) + await mkdir(dir, { recursive: true }) + filepath = path.join(dir, 'my-test-file.html') + await writeFile(filepath, `
`) + }) afterEach(() => unlink(filepath)) test('the current working directory is used by default', async () => { + const spy = vi.spyOn(process, 'cwd') + spy.mockReturnValue(dir) + let processor = postcss([tailwindcss({ optimize: { minify: false } })]) let result = await processor.process(`@import "tailwindcss"`, { from: inputCssFilePath() })