Skip to content

Commit

Permalink
test: add e2e case
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Aug 10, 2024
1 parent feae6af commit c27efa6
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"nano-staged": "^0.8.0",
"playwright": "^1.45.3",
"simple-git-hooks": "^2.11.1",
"styled-components": "^6.1.12",
"tsup": "^8.2.3",
"typescript": "^5.5.4"
},
Expand Down
132 changes: 127 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createRequire } from 'node:module';
import type {
MergedEnvironmentConfig,
RsbuildConfig,
Expand All @@ -6,6 +7,8 @@ import type {
} from '@rsbuild/core';
import { type ConfigChain, reduceConfigs } from 'reduce-configs';

const require = createRequire(import.meta.url);

/**
* The options of [@swc/plugin-styled-components](https://www.npmjs.com/package/@swc/plugin-styled-components).
*/
Expand Down
37 changes: 16 additions & 21 deletions test/basic/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import { dirname } from 'node:path';
import { readFileSync } 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 { pluginStyledComponents } from '../../src';
import { getRandomPort } from '../helper';
import { pluginStyledComponents } from '../../dist';

const __dirname = dirname(fileURLToPath(import.meta.url));

test('should render page as expected', async ({ page }) => {
test('should transform styled-components with pluginStyledComponents', async ({
page,
}) => {
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [pluginStyledComponents()],
server: {
port: getRandomPort(),
output: {
minify: false,
filenameHash: false,
},
},
});

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

await page.goto(urls[0]);
expect(await page.evaluate('window.test')).toBe(1);
await rsbuild.build();

await server.close();
});
const indexJs = readFileSync(
join(rsbuild.context.distPath, 'static/js/index.js'),
'utf-8',
);

test('should build succeed', async ({ page }) => {
const rsbuild = await createRsbuild({
cwd: __dirname,
rsbuildConfig: {
plugins: [pluginStyledComponents()],
},
});
expect(indexJs).toContain('div.withConfig');

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

await page.goto(urls[0]);
expect(await page.evaluate('window.test')).toBe(1);
expect(await page.evaluate('typeof window.test')).toBe('object');

await server.close();
});
6 changes: 5 additions & 1 deletion test/basic/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
window.test = 1;
import styled from 'styled-components';

window.test = styled.div`
color: red;
`;

0 comments on commit c27efa6

Please sign in to comment.