Skip to content

Commit

Permalink
test: plugin-rsc-analyze (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Jun 25, 2024
1 parent 45bb774 commit aa8b021
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'
- run: pnpm install --frozen-lockfile
Expand Down
2 changes: 2 additions & 0 deletions packages/waku/tests/fixtures/plugin-rsc-analyze/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use client';
console.log('This is client side code!');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './client.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './server.js';
2 changes: 2 additions & 0 deletions packages/waku/tests/fixtures/plugin-rsc-analyze/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use server';
console.log('This is server side code');
155 changes: 155 additions & 0 deletions packages/waku/tests/vite-plugin-rsc-analyze.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { build } from 'vite';
import { expect, test, describe } from 'vitest';
import { rscAnalyzePlugin } from '../src/lib/plugins/vite-plugin-rsc-analyze.js';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import type { LoggingFunction, RollupLog } from 'rollup';

const root = fileURLToPath(new URL('./fixtures', import.meta.url));

const onwarn = (warning: RollupLog, defaultHandler: LoggingFunction) => {
if (
warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
/"use (client|server)"/.test(warning.message)
) {
return;
} else if (
warning.code === 'SOURCEMAP_ERROR' &&
warning.loc?.column === 0 &&
warning.loc?.line === 1
) {
return;
}
defaultHandler(warning);
};

async function runTest(
root: string,
isClient: boolean,
inputFile: string,
expectedClientFileSet: Set<string>,
expectedServerFileSet: Set<string>,
) {
const clientFileSet = new Set<string>();
const serverFileSet = new Set<string>();
const fileHashMap = new Map<string, string>();
await build({
root: root,
logLevel: 'silent',
build: {
write: false,
rollupOptions: {
onwarn,
cache: false,
input: path.resolve(root, inputFile),
},
},
plugins: [
isClient
? rscAnalyzePlugin({
isClient: true,
serverFileSet,
})
: rscAnalyzePlugin({
isClient: false,
clientFileSet,
serverFileSet,
fileHashMap,
}),
],
});
// remove the base path
[...clientFileSet].forEach((value) => {
clientFileSet.delete(value);
clientFileSet.add(path.relative(root, value));
});
[...serverFileSet].forEach((value) => {
serverFileSet.delete(value);
serverFileSet.add(path.relative(root, value));
});

expect(clientFileSet).toEqual(expectedClientFileSet);
expect(serverFileSet).toEqual(expectedServerFileSet);
}

describe('vite-plugin-rsc-analyze', () => {
test('server - server', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
false,
'server.ts',
new Set(),
new Set(['server.ts']),
);
});

test('client - server', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
true,
'server.ts',
new Set(),
new Set(['server.ts']),
);
});

test('server - client', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
false,
'client.ts',
new Set(['client.ts']),
new Set(),
);
});

test('client - client', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
true,
'client.ts',
new Set(),
new Set(),
);
});

test('server - import client', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
false,
'import-client.ts',
new Set(['client.ts']),
new Set(),
);
});

test('client - import client', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
true,
'import-client.ts',
new Set(),
new Set(),
);
});

test('server - import server', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
false,
'import-server.ts',
new Set(),
new Set(['server.ts']),
);
});

test('client - import server', async () => {
await runTest(
path.resolve(root, './plugin-rsc-analyze'),
true,
'import-server.ts',
new Set(),
new Set(['server.ts']),
);
});
});

0 comments on commit aa8b021

Please sign in to comment.