Skip to content

Commit

Permalink
chore(rsbuild): add test for plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Dec 6, 2024
1 parent a2cebe6 commit 871ebe3
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
142 changes: 142 additions & 0 deletions packages/rsbuild/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { type CreateNodesContext } from '@nx/devkit';
import { createNodesV2 } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';

jest.mock('@rsbuild/core', () => ({
...jest.requireActual('@rsbuild/core'),
loadConfig: jest.fn().mockResolvedValue({
filePath: 'my-app/rsbuild.config.ts',
content: {},
}),
}));

jest.mock('@nx/js/src/utils/typescript/ts-solution-setup', () => ({
...jest.requireActual('@nx/js/src/utils/typescript/ts-solution-setup'),
isUsingTsSolutionSetup: jest.fn().mockReturnValue(false),
}));

describe('@nx/rsbuild/plugin', () => {
let createNodesFunction = createNodesV2[1];
let context: CreateNodesContext;
let tempFs: TempFs;

beforeEach(() => {
tempFs = new TempFs('rsbuild-test');
context = {
configFiles: [],
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: tempFs.tempDir,
};

tempFs.createFileSync(
'my-app/project.json',
JSON.stringify({ name: 'my-app' })
);
tempFs.createFileSync('my-app/rsbuild.config.ts', `export default {};`);
});

afterEach(() => {
jest.resetModules();
tempFs.cleanup();
});

it('should create nodes', async () => {
const nodes = await createNodesFunction(
['my-app/rsbuild.config.ts'],
{
buildTargetName: 'build-something',
devTargetName: 'dev-serve',
previewTargetName: 'preview-serve',
inspectTargetName: 'inspect-serve',
typecheckTargetName: 'typecheck-app',
},
context
);

expect(nodes).toMatchInlineSnapshot(`
[
[
"my-app/rsbuild.config.ts",
{
"projects": {
"my-app": {
"metadata": {},
"root": "my-app",
"targets": {
"build-something": {
"cache": true,
"command": "rsbuild build",
"dependsOn": [
"^build-something",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"@rsbuild/core",
],
},
],
"metadata": {
"description": "Run Rsbuild build",
"help": {
"command": "npx rsbuild build --help",
"example": {
"options": {
"watch": false,
},
},
},
"technologies": [
"rsbuild",
],
},
"options": {
"args": [
"--mode=production",
],
"cwd": "my-app",
},
"outputs": [
"{workspaceRoot}/dist/{projectRoot}",
],
},
"dev-serve": {
"command": "rsbuild dev",
"options": {
"args": [
"--mode=development",
],
"cwd": "my-app",
},
},
"inspect-serve": {
"command": "rsbuild inspect",
"options": {
"cwd": "my-app",
},
},
"preview-serve": {
"command": "rsbuild preview",
"options": {
"args": [
"--mode=production",
],
"cwd": "my-app",
},
},
},
},
},
},
],
]
`);
});
});
7 changes: 3 additions & 4 deletions packages/rsbuild/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,15 @@ function getOutputs(
projectRoot: string,
workspaceRoot: string
): { buildOutputs: string[] } {
const { output, dev } = rsbuildConfig;
const buildOutputPath = normalizeOutputPath(
output?.distPath?.root ? dirname(output.distPath.root) : undefined,
rsbuildConfig?.output?.distPath?.root
? dirname(rsbuildConfig?.output.distPath.root)
: undefined,
projectRoot,
workspaceRoot,
'dist'
);

const hasServeConfig = Boolean(dev);

return {
buildOutputs: [buildOutputPath],
};
Expand Down

0 comments on commit 871ebe3

Please sign in to comment.