Skip to content

Commit 44daf8d

Browse files
authored
feat: support logHeapUsage (#666)
1 parent fa27aff commit 44daf8d

File tree

18 files changed

+144
-6
lines changed

18 files changed

+144
-6
lines changed

e2e/reporter/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,21 @@ describe.concurrent('reporters', () => {
9393
await cli.exec;
9494
expect(cli.stdout).not.toContain('✗ basic > b');
9595
});
96+
97+
it('logHeapUsage', async ({ onTestFinished }) => {
98+
const { cli, expectLog } = await runRstestCli({
99+
command: 'rstest',
100+
args: ['run', 'index', '--logHeapUsage'],
101+
onTestFinished,
102+
options: {
103+
nodeOptions: {
104+
cwd: __dirname,
105+
},
106+
},
107+
});
108+
109+
await cli.exec;
110+
expectLog(/fixtures\/index.test.ts.*\d+ MB heap used/);
111+
expectLog(/ basic > b.*\d+ MB heap used/);
112+
});
96113
});

packages/core/rslib.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineConfig, rspack } from '@rslib/core';
22
import { licensePlugin } from './licensePlugin';
3+
import { version } from './package.json';
34

45
const isBuildWatch = process.argv.includes('--watch');
56

@@ -75,7 +76,7 @@ export default defineConfig({
7576
worker: './src/runtime/worker/index.ts',
7677
},
7778
define: {
78-
RSTEST_VERSION: JSON.stringify(require('./package.json').version),
79+
RSTEST_VERSION: JSON.stringify(version),
7980
},
8081
},
8182
tools: {

packages/core/src/cli/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const applyCommonOptions = (cli: CAC) => {
4848
'Print console traces when calling any console method',
4949
)
5050
.option('--disableConsoleIntercept', 'Disable console intercept')
51+
.option('--logHeapUsage', 'Log heap usage after each test')
5152
.option(
5253
'--slowTestThreshold <value>',
5354
'The number of milliseconds after which a test or suite is considered slow',

packages/core/src/cli/init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type CommonOptions = {
2626
coverage?: boolean;
2727
passWithNoTests?: boolean;
2828
printConsoleTrace?: boolean;
29+
logHeapUsage?: boolean;
2930
disableConsoleIntercept?: boolean;
3031
update?: boolean;
3132
testNamePattern?: RegExp | string;
@@ -76,6 +77,7 @@ async function resolveConfig(
7677
'disableConsoleIntercept',
7778
'testEnvironment',
7879
'hideSkippedTests',
80+
'logHeapUsage',
7981
];
8082
for (const key of keys) {
8183
if (options[key] !== undefined) {

packages/core/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const createDefaultConfig = (): NormalizedConfig => ({
147147
snapshotFormat: {},
148148
env: {},
149149
hideSkippedTests: false,
150+
logHeapUsage: false,
150151
coverage: {
151152
exclude: [
152153
'**/node_modules/**',

packages/core/src/pool/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
5454
coverage,
5555
snapshotFormat,
5656
env,
57+
logHeapUsage,
5758
} = context.normalizedConfig;
5859

5960
return {
@@ -76,6 +77,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
7677
isolate,
7778
coverage,
7879
snapshotFormat,
80+
logHeapUsage,
7981
};
8082
};
8183

packages/core/src/reporter/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export const logCase = (
5151
const retry = result.retryCount
5252
? color.yellow(` (retry x${result.retryCount})`)
5353
: '';
54-
logger.log(` ${icon} ${nameStr}${color.gray(duration)}${retry}`);
54+
const heap = result.heap
55+
? ` ${color.magenta(formatHeapUsed(result.heap))}`
56+
: '';
57+
58+
logger.log(` ${icon} ${nameStr}${color.gray(duration)}${retry}${heap}`);
5559

5660
if (result.errors) {
5761
for (const error of result.errors) {
@@ -60,6 +64,10 @@ export const logCase = (
6064
}
6165
};
6266

67+
const formatHeapUsed = (heap: number) => {
68+
return `${Math.floor(heap / 1024 / 1024)} MB heap used`;
69+
};
70+
6371
export const logFileTitle = (
6472
test: TestFileResult,
6573
relativePath: string,
@@ -77,5 +85,9 @@ export const logFileTitle = (
7785
title += ` ${formatDuration(test.duration!)}`;
7886
}
7987

88+
if (test.heap) {
89+
title += ` ${color.magenta(formatHeapUsed(test.heap))}`;
90+
}
91+
8092
logger.log(title);
8193
};

packages/core/src/runtime/runner/runner.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ export class TestRunner {
349349

350350
result.duration = RealDate.now() - start;
351351
result.retryCount = retryCount - 1;
352+
result.heap = state.runtimeConfig.logHeapUsage
353+
? process.memoryUsage().heapUsed
354+
: undefined;
352355
hooks.onTestCaseResult?.(result);
353356
results.push(result);
354357
}
@@ -373,6 +376,9 @@ export class TestRunner {
373376
name: '',
374377
status: 'fail',
375378
results,
379+
heap: state.runtimeConfig.logHeapUsage
380+
? process.memoryUsage().heapUsed
381+
: undefined,
376382
errors: [
377383
{
378384
message: `No test suites found in file: ${testPath}`,
@@ -394,6 +400,9 @@ export class TestRunner {
394400
project,
395401
testPath,
396402
name: '',
403+
heap: state.runtimeConfig.logHeapUsage
404+
? process.memoryUsage().heapUsed
405+
: undefined,
397406
status: errors.length ? 'fail' : getTestStatus(results, defaultStatus),
398407
results,
399408
snapshotResult,

packages/core/src/types/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ export interface RstestConfig {
215215
*/
216216
maxConcurrency?: number;
217217

218+
/**
219+
* Log heap usage after each test
220+
* @default false
221+
*/
222+
logHeapUsage?: boolean;
223+
218224
/**
219225
* Custom handler for console log in tests
220226
*/

packages/core/src/types/testSuite.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export type TestResult = {
122122
errors?: FormattedError[];
123123
retryCount?: number;
124124
project: string;
125+
heap?: number;
125126
};
126127

127128
export type TestFileResult = TestResult & {

0 commit comments

Comments
 (0)