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
2 changes: 1 addition & 1 deletion e2e/browser-mode/fixtures/basic/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Async operations', () => {
count++;
}, 20);

await sleep(70);
await sleep(300);
globalThis.clearInterval(intervalId);

expect(count).toBeGreaterThanOrEqual(2);
Expand Down
57 changes: 57 additions & 0 deletions e2e/cli/initBrowser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fs from 'node:fs';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, beforeEach, describe, expect, it } from '@rstest/core';
import { runRstestCli } from '../scripts';

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

function readJson(filePath: string): Record<string, any> {
return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as Record<string, any>;
}

describe('cli init browser', () => {
const projectDir = path.join(__dirname, 'fixtures', 'test-temp-init-browser');

beforeEach(() => {
fs.mkdirSync(projectDir, { recursive: true });
fs.writeFileSync(
path.join(projectDir, 'package.json'),
`${JSON.stringify({ name: 'fixture', private: true }, null, 2)}\n`,
'utf-8',
);
});

afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true });
});

it('should generate files and update package.json in --yes mode', async () => {
const { expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: ['init', 'browser', '--yes'],
options: {
nodeOptions: {
cwd: projectDir,
},
},
});

await expectExecSuccess();

expect(
fs.existsSync(path.join(projectDir, 'rstest.browser.config.ts')),
).toBe(true);

expect(
fs.existsSync(path.join(projectDir, 'tests', 'Counter.test.ts')) ||
fs.existsSync(path.join(projectDir, 'tests', 'Counter.test.js')),
).toBe(true);

const pkg = readJson(path.join(projectDir, 'package.json'));
expect(pkg.scripts?.['test:browser']).toBe(
'rstest --config=rstest.browser.config.ts',
);
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"nano-staged": "^0.9.0",
"nx": "^21.6.10",
"path-serializer": "0.5.1",
"playwright": "^1.49.1",
"prettier": "^3.7.4",
"prettier-plugin-packagejson": "^2.5.20",
"simple-git-hooks": "^2.13.1",
Expand Down
280 changes: 280 additions & 0 deletions packages/core/LICENSE.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"@types/jsdom": "^21.1.7",
"@types/picomatch": "^4.0.2",
"@types/sinonjs__fake-timers": "^8.1.5",
"@clack/prompts": "^0.10.1",
"@vercel/detect-agent": "^1.0.0",
"@types/source-map-support": "^0.5.10",
"@vitest/expect": "^3.2.4",
"@vitest/snapshot": "^3.2.4",
Expand All @@ -91,6 +93,7 @@
"jest-diff": "^30.2.0",
"jsdom": "^26.1.0",
"memfs": "^4.51.1",
"package-manager-detector": "^1.6.0",
"pathe": "^2.0.3",
"picocolors": "^1.1.1",
"picomatch": "^4.0.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill';
import { defineConfig, rspack } from '@rslib/core';
import { peerDependencies } from '../browser/package.json';
import { licensePlugin } from './licensePlugin';
import { version } from './package.json';

Expand Down Expand Up @@ -167,6 +168,7 @@ export default defineConfig({
source: {
define: {
RSTEST_VERSION: JSON.stringify(version),
PLAYWRIGHT_VERSION: JSON.stringify(peerDependencies.playwright),
},
},
tools: {
Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,54 @@ export function setupCommands(): void {
},
);

// init command - initialize rstest configuration
cli
.command('init [project]', 'Initialize rstest configuration')
.option('--yes', 'Use default options (non-interactive)')
.action(async (project: string | undefined, options: { yes?: boolean }) => {
try {
let selectedProject = project;

// If no project specified, show selection menu
if (!selectedProject) {
const { select, isCancel } = await import('@clack/prompts');
const color = (await import('picocolors')).default;

console.log();
const selected = await select({
message: 'What would you like to initialize?',
options: [
{
value: 'browser',
label: 'browser',
hint: 'Browser mode for component testing',
},
],
});

if (isCancel(selected)) {
console.log(color.yellow('Operation cancelled.'));
process.exit(0);
}

selectedProject = selected as string;
}

if (selectedProject === 'browser') {
const { create } = await import('./init/browser');
await create({ yes: options.yes });
} else {
logger.error(
`Unknown project type: "${selectedProject}". Available: browser`,
);
process.exit(1);
}
} catch (err) {
logger.error('Failed to initialize rstest.');
logger.error(formatError(err));
process.exit(1);
}
});

cli.parse();
}
Loading
Loading