@rstest/core
Version:
The Rsbuild-based test tool.
209 lines (208 loc) • 10 kB
JavaScript
import 'module';
/*#__PURE__*/ import.meta.url;
export const __webpack_ids__ = [
"920"
];
export const __webpack_modules__ = {
"./src/core/runTests.ts": function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
runTests: ()=>runTests
});
var src_pool = __webpack_require__("./src/pool/index.ts");
var utils = __webpack_require__("./src/utils/index.ts");
const isCliShortcutsEnabled = ()=>(0, utils._l)('stdin');
async function setupCliShortcuts({ closeServer, runAll, updateSnapshot, runFailedTests }) {
const shortcuts = [
{
key: 'c',
description: `${utils.$_.bold('c')} ${utils.$_.dim('clear console')}`,
action: ()=>{
console.clear();
}
},
{
key: 'f',
description: `${utils.$_.bold('f')} ${utils.$_.dim('rerun failed tests')}`,
action: async ()=>{
await runFailedTests();
}
},
{
key: 'a',
description: `${utils.$_.bold('a')} ${utils.$_.dim('rerun all tests')}`,
action: async ()=>{
await runAll();
}
},
{
key: 'u',
description: `${utils.$_.bold('u')} ${utils.$_.dim('update snapshot')}`,
action: async ()=>{
await updateSnapshot();
}
},
{
key: 'q',
description: `${utils.$_.bold('q')} ${utils.$_.dim('quit process')}`,
action: async ()=>{
try {
await closeServer();
} finally{
process.exit(0);
}
}
}
];
const { createInterface } = await import("node:readline");
const rl = createInterface({
input: process.stdin,
output: process.stdout
});
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
const handleKeypress = (str, key)=>{
if (key.ctrl && 'c' === key.name) process.exit(0);
for (const shortcut of shortcuts)if (str === shortcut.key) return void shortcut.action();
if ('h' === str) {
let message = `\n ${utils.$_.bold(utils.$_.blue('Shortcuts:'))}\n`;
for (const shortcut of shortcuts)message += ` ${shortcut.description}\n`;
utils.kg.log(message);
}
};
process.stdin.on('keypress', handleKeypress);
return ()=>{
process.stdin.setRawMode(false);
process.stdin.pause();
rl.close();
};
}
var rsbuild = __webpack_require__("./src/core/rsbuild.ts");
async function runTests(context, fileFilters) {
const { normalizedConfig: { include, exclude, root, name, setupFiles: setups, includeSource }, rootPath, reporters, snapshotManager, command } = context;
const entriesCache = new Map();
const globTestSourceEntries = async ()=>{
const entries = await (0, utils.GL)({
include,
exclude,
includeSource,
root,
fileFilters
});
entriesCache.set(name, entries);
if (!Object.keys(entries).length) {
utils.kg.log(utils.$_.red('No test files found.'));
utils.kg.log('');
if (fileFilters.length) utils.kg.log(utils.$_.gray('filter: '), fileFilters.join(utils.$_.gray(', ')));
utils.kg.log(utils.$_.gray('include:'), include.join(utils.$_.gray(', ')));
utils.kg.log(utils.$_.gray('exclude:'), exclude.join(utils.$_.gray(', ')));
utils.kg.log('');
}
return entries;
};
const setupFiles = (0, utils.aA)(setups, rootPath);
const rsbuildInstance = await (0, rsbuild.z)(context, globTestSourceEntries, setupFiles);
const { getRsbuildStats, closeServer } = await (0, rsbuild.r)({
name,
normalizedConfig: context.normalizedConfig,
globTestSourceEntries: 'watch' === command ? globTestSourceEntries : async ()=>{
if (entriesCache.has(name)) return entriesCache.get(name);
return globTestSourceEntries();
},
setupFiles,
rsbuildInstance,
rootPath
});
const recommendWorkerCount = 'watch' === command ? 1 / 0 : Array.from(entriesCache.values()).reduce((acc, entries)=>acc + Object.keys(entries).length, 0);
const pool = await (0, src_pool.K)({
context,
recommendWorkerCount
});
let testFileResult = [];
let buildHash;
const run = async ({ fileFilters } = {})=>{
const { entries, setupEntries, assetFiles, sourceMaps, getSourcemap, buildTime, hash } = await getRsbuildStats({
fileFilters
});
const testStart = Date.now();
const { results, testResults } = await pool.runTests({
entries,
sourceMaps,
setupEntries,
assetFiles,
updateSnapshot: snapshotManager.options.updateSnapshot
});
const actualBuildTime = buildHash === hash ? 0 : buildTime;
const testTime = Date.now() - testStart;
const duration = {
totalTime: testTime + actualBuildTime,
buildTime: actualBuildTime,
testTime
};
buildHash = hash;
testFileResult = results;
if (results.some((r)=>'fail' === r.status)) process.exitCode = 1;
for (const reporter of reporters)await reporter.onTestRunEnd?.({
results,
testResults,
snapshotSummary: snapshotManager.summary,
duration,
getSourcemap
});
};
if ('watch' === command) {
const enableCliShortcuts = isCliShortcutsEnabled();
const afterTestsWatchRun = ()=>{
utils.kg.log(utils.$_.green(' Waiting for file changes...'));
if (enableCliShortcuts) if (snapshotManager.summary.unmatched) utils.kg.log(` ${utils.$_.dim('press')} ${utils.$_.yellow(utils.$_.bold('u'))} ${utils.$_.dim('to update snapshot')}${utils.$_.dim(', press')} ${utils.$_.bold('h')} ${utils.$_.dim('to show help')}\n`);
else utils.kg.log(` ${utils.$_.dim('press')} ${utils.$_.bold('h')} ${utils.$_.dim('to show help')}${utils.$_.dim(', press')} ${utils.$_.bold('q')} ${utils.$_.dim('to quit')}\n`);
};
const clearLogs = ()=>{
console.clear();
};
rsbuildInstance.onDevCompileDone(async ({ isFirstCompile })=>{
if (!isFirstCompile) clearLogs();
snapshotManager.clear();
await run();
if (isFirstCompile && enableCliShortcuts) await setupCliShortcuts({
closeServer: async ()=>{
await pool.close();
await closeServer();
},
runAll: async ()=>{
clearLogs();
snapshotManager.clear();
await run();
afterTestsWatchRun();
},
runFailedTests: async ()=>{
const failedTests = testFileResult.filter((result)=>'fail' === result.status).map((r)=>r.testPath);
if (!failedTests.length) return void utils.kg.log(utils.$_.yellow('\nNo failed tests were found that needed to be rerun.'));
clearLogs();
snapshotManager.clear();
await run({
fileFilters: failedTests
});
afterTestsWatchRun();
},
updateSnapshot: async ()=>{
if (!snapshotManager.summary.unmatched) return void utils.kg.log(utils.$_.yellow('\nNo snapshots were found that needed to be updated.'));
clearLogs();
const originalUpdateSnapshot = snapshotManager.options.updateSnapshot;
snapshotManager.clear();
snapshotManager.options.updateSnapshot = 'all';
await run();
afterTestsWatchRun();
snapshotManager.options.updateSnapshot = originalUpdateSnapshot;
}
});
afterTestsWatchRun();
});
} else {
await run();
await pool.close();
await closeServer();
}
}
}
};