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
26 changes: 26 additions & 0 deletions test/cjs-compat/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { dirname } 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));

test('should not interfere with CJS modules', async ({ page }) => {
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [pluginTailwindCSS()],
},
});

await rsbuild.build();
const { server, urls } = await rsbuild.preview();

await page.goto(urls[0]);

const locator = page.locator('#cjs-output');
await expect(locator).toHaveText('Hello from CJS');

await server.close();
Comment on lines +20 to +25

Choose a reason for hiding this comment

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

medium

The test logic that interacts with the server should be wrapped in a try...finally block. This ensures that server.close() is always called, even if assertions within the try block fail. This prevents leaving dangling server processes, which can lead to resource leaks and instability in test environments.

  try {
    await page.goto(urls[0]);

    const locator = page.locator('#cjs-output');
    await expect(locator).toHaveText('Hello from CJS');
  } finally {
    await server.close();
  }

});
6 changes: 6 additions & 0 deletions test/cjs-compat/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import lib from './lib.cjs';

const div = document.createElement('div');
div.id = 'cjs-output';
div.textContent = lib.message;
document.body.appendChild(div);
4 changes: 4 additions & 0 deletions test/cjs-compat/src/lib.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
message: 'Hello from CJS',
data: Array.from({ length: 1000 }, (_, i) => i), // Some data to make it "large"
};