Skip to content
Merged
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
3 changes: 2 additions & 1 deletion examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"devDependencies": {
"@rsbuild/core": "1.7.1",
"@rsbuild/plugin-react": "^1.4.2",
"@rstest/adapter-rsbuild": "workspace:*",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.1",
"@types/react": "^19.2.7",
"@types/react-dom": "^19.2.3",
"jsdom": "^26.1.0",
"happy-dom": "^20.0.11",
"typescript": "^5.9.3"
}
}
5 changes: 2 additions & 3 deletions examples/react/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { pluginReact } from '@rsbuild/plugin-react';
import { withRsbuildConfig } from '@rstest/adapter-rsbuild';
import { defineConfig } from '@rstest/core';

export default defineConfig({
plugins: [pluginReact()],
testEnvironment: 'jsdom',
extends: withRsbuildConfig({ cwd: __dirname }),
setupFiles: ['./rstest.setup.ts'],
});
23 changes: 23 additions & 0 deletions packages/adapter-rsbuild/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @rstest/adapter-rsbuild

Rstest adapter for [Rsbuild](https://rsbuild.rs) configuration. This package allows you to extend your Rstest configuration from Rsbuild config files.

## Installation

```bash
npm install @rstest/adapter-rsbuild -D
```

## Usage

```ts
import { defineConfig } from '@rstest/core';
import { withRsbuildConfig } from '@rstest/adapter-rsbuild';

export default defineConfig({
extends: withRsbuildConfig(),
// other rstest config options
});
```

More advanced usage examples can be found in the [Rsbuild integration guide](https://rstest.rs/guide/integration/rsbuild).
49 changes: 49 additions & 0 deletions packages/adapter-rsbuild/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@rstest/adapter-rsbuild",
"version": "0.1.0",
"description": "Rstest adapter for rsbuild configuration",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"files": [
"dist"
],
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch",
"test": "rstest"
},
"keywords": [
"rstest",
"rsbuild",
"adapter",
"configuration"
],
"license": "MIT",
"peerDependencies": {
"@rsbuild/core": "*"
},
"devDependencies": {
"@rslib/core": "^0.19.0",
"@rsbuild/core": "1.7.1",
"@rstest/core": "workspace:*",
"@rstest/tsconfig": "workspace:*"
},
"publishConfig": {
"access": "public"
},
"bugs": {
"url": "https://github.com/web-infra-dev/rstest/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/web-infra-dev/rstest",
"directory": "packages/adapter-rsbuild"
}
}
15 changes: 15 additions & 0 deletions packages/adapter-rsbuild/rslib.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
dts: true,
bundle: true,
syntax: ['node 18.12.0'],
experiments: {
advancedEsm: true,
},
},
],
});
5 changes: 5 additions & 0 deletions packages/adapter-rsbuild/rstest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({
root: __dirname,
});
105 changes: 105 additions & 0 deletions packages/adapter-rsbuild/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
loadConfig,
mergeRsbuildConfig,
type RsbuildConfig,
} from '@rsbuild/core';
import type { ExtendConfig, ExtendConfigFn } from '@rstest/core';

export interface WithRsbuildConfigOptions {
/**
* `cwd` passed to loadConfig of Rsbuild
* @default process.cwd()
*/
cwd?: string;
/**
* Path to rsbuild config file
* @default './rsbuild.config.ts'
*/
configPath?: string;
/**
* The environment name in `environments` field to use, will be merged with the common config.
* Set to a string to use the environment config with matching name.
* @default undefined
*/
environmentName?: string;
/**
* Modify rsbuild config before converting to rstest config
*/
modifyRsbuildConfig?: (buildConfig: RsbuildConfig) => RsbuildConfig;
}

export function withRsbuildConfig(
options: WithRsbuildConfigOptions = {},
): ExtendConfigFn {
return async () => {
const {
configPath,
modifyRsbuildConfig,
environmentName,
cwd = process.cwd(),
} = options;

// Load rsbuild config
const {
content: { environments, ...rawBuildConfig },
filePath,
} = await loadConfig({
cwd,
path: configPath,
});

if (!filePath) {
return {};
}

const environmentConfig = environmentName
? environments?.[environmentName]
: undefined;

const rsbuildConfig = environmentConfig
? mergeRsbuildConfig<RsbuildConfig>(
rawBuildConfig as RsbuildConfig,
environmentConfig as RsbuildConfig,
)
: (rawBuildConfig as RsbuildConfig);

// Allow modification of rsbuild config
const finalBuildConfig = modifyRsbuildConfig
? modifyRsbuildConfig(rsbuildConfig)
: rsbuildConfig;

const { rspack, swc, bundlerChain } = finalBuildConfig.tools || {};
const { cssModules, target, module } = finalBuildConfig.output || {};
const { decorators, define, include, exclude, tsconfigPath } =
finalBuildConfig.source || {};

// Convert rsbuild config to rstest config
const rstestConfig: ExtendConfig = {
// Copy over compatible configurations
root: finalBuildConfig.root,
name: environmentName,
plugins: finalBuildConfig.plugins,
source: {
decorators,
define,
include,
exclude,
tsconfigPath,
},
resolve: finalBuildConfig.resolve,
output: {
cssModules,
module,
},
tools: {
rspack,
swc,
bundlerChain,
} as ExtendConfig['tools'],

testEnvironment: target === 'node' ? 'node' : 'happy-dom',
};

return rstestConfig;
};
}
105 changes: 105 additions & 0 deletions packages/adapter-rsbuild/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { existsSync, unlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from '@rstest/core';
import { withRsbuildConfig } from '../src';

describe('withRsbuildConfig', () => {
const testConfigPath = join(__dirname, 'test-temp-rsbuild.config.ts');
const testConfigContent = `
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
source: {
define: {
'process.env.NODE_ENV': '"common"'
}
},
resolve: {
alias: {
'@': './src'
}
},
environments: {
test: {
source: {
define: {
'process.env.NODE_ENV': '"test"'
}
}
}
}
});
`;

beforeEach(() => {
// Create a temporary rsbuild config file for testing
writeFileSync(testConfigPath, testConfigContent);
});

afterEach(() => {
// Clean up test config file
if (existsSync(testConfigPath)) {
unlinkSync(testConfigPath);
}
});

it('should load and convert rsbuild config to rstest config', async () => {
const config = await withRsbuildConfig({
configPath: testConfigPath,
})({});

expect(config).toBeDefined();
expect(config.source?.define).toEqual({
'process.env.NODE_ENV': '"common"',
});
expect(config.resolve?.alias).toEqual({
'@': './src',
});
expect(config.testEnvironment).toBe('happy-dom');
});

it('should load and merge environment config', async () => {
const config = await withRsbuildConfig({
configPath: testConfigPath,
environmentName: 'test',
})({});

expect(config).toBeDefined();
expect(config.name).toBe('test');
expect(config.source?.define).toEqual({
'process.env.NODE_ENV': '"test"',
});
expect(config.resolve?.alias).toEqual({
'@': './src',
});
});

it('should allow modification of rsbuild config', async () => {
const config = await withRsbuildConfig({
configPath: testConfigPath,
modifyRsbuildConfig: (rsbuildConfig) => ({
...rsbuildConfig,
source: {
...rsbuildConfig.source,
define: {
...rsbuildConfig.source?.define,
'process.env.CUSTOM': '"custom-value"',
},
},
}),
})({});

expect(config.source?.define).toEqual({
'process.env.NODE_ENV': '"common"',
'process.env.CUSTOM': '"custom-value"',
});
});

it('should throw error when config file not found', async () => {
await expect(() =>
withRsbuildConfig({
configPath: './non-existent.config.ts',
})({}),
).rejects.toThrowError(/Cannot find config file/);
});
});
11 changes: 11 additions & 0 deletions packages/adapter-rsbuild/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@rstest/tsconfig/base",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./",
"rootDir": "src",
"composite": true,
"isolatedDeclarations": true
},
"include": ["src"]
}
24 changes: 21 additions & 3 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion website/docs/en/guide/integration/_meta.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["adapters", "rslib"]
["adapters", "rslib", "rsbuild"]
1 change: 1 addition & 0 deletions website/docs/en/guide/integration/adapters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ In this way, you can create powerful and reusable adapters that seamlessly integ
The following are the officially supported Rstest adapters:

- [Rslib Adapter](/guide/integration/rslib#install-adapter): Used to extend Rstest configuration from Rslib configuration.
- [Rsbuild Adapter](/guide/integration/rsbuild#install-adapter): Used to extend Rstest configuration from Rsbuild configuration.
Loading
Loading