Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions test/recompilation/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import fs from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, test } from '@playwright/test';
import { createRsbuild } from '@rsbuild/core';
import { pluginTailwindCSS } from '../../src';

const __dirname = dirname(fileURLToPath(import.meta.url));
const themePath = join(__dirname, 'src/theme.css');

test('should update styles when theme file changes', async ({ page }) => {
// Reset theme file
fs.writeFileSync(
themePath,
`
@theme {
--color-dynamic: rgb(1, 2, 3);
}
`,
);

const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [pluginTailwindCSS()],
},
});

// First build
await rsbuild.build();
let result = await rsbuild.preview();

await page.goto(result.urls[0]);
let locator = page.locator('#dynamic');
await expect(locator).toHaveCSS('background-color', 'rgb(1, 2, 3)');
await result.server.close();

// Modify theme file
fs.writeFileSync(
themePath,
`
@theme {
--color-dynamic: rgb(4, 5, 6);
}
`,
);

// Second build (reusing instance if possible, or creating new one if that's how tests work)
// The user asked to "Same createRsbuild instance" if possible.
// rsbuild.build() runs the build.
// We can run it again.
Comment on lines +48 to +51

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These comments appear to be development notes and can be removed to improve code clarity. The code itself is self-explanatory in that it's performing a second build.

await rsbuild.build();
result = await rsbuild.preview(); // New preview server

await page.goto(result.urls[0]);
locator = page.locator('#dynamic');
await expect(locator).toHaveCSS('background-color', 'rgb(4, 5, 6)');
await result.server.close();
});
Comment on lines +11 to +59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test modifies a file on the file system (theme.css), which is a side effect. If the test fails midway, the file could be left in a modified state, potentially affecting other tests or subsequent local runs. It's a good practice to ensure cleanup, even on failure.

You can use Playwright's test.afterEach hook to restore the file to its original state after the test completes, regardless of whether it passed or failed. This makes the test more robust and self-contained.

Here's how you could implement this:

// At the top of the file
const __dirname = dirname(fileURLToPath(import.meta.url));
const themePath = join(__dirname, 'src/theme.css');
const originalTheme = fs.readFileSync(themePath, 'utf-8');

test.afterEach(() => {
  fs.writeFileSync(themePath, originalTheme);
});

// The test itself remains largely the same
test('should update styles when theme file changes', async ({ page }) => {
  // ...
});

6 changes: 6 additions & 0 deletions test/recompilation/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "tailwindcss";
@reference "./theme.css";

.dynamic-bg {
@apply bg-dynamic;
}
7 changes: 7 additions & 0 deletions test/recompilation/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import './index.css';

const div = document.createElement('div');
div.id = 'dynamic';
div.className = 'dynamic-bg';
div.textContent = 'Dynamic';
document.body.appendChild(div);
3 changes: 3 additions & 0 deletions test/recompilation/src/theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@theme {
--color-dynamic: rgb(4, 5, 6);
}