test: add unit tests for trace processing helpers - #2514
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Adds direct unit tests for the pure helpers exported from src/trace-processing/parse.ts: - traceResultIsSuccess: accepts real parsed trace results (with and without insights) and rejects TraceParseError objects, including a near-miss where the error message contains the discriminant key name. - parseRawTraceBuffer: covers the previously untested error branches for empty buffers, invalid JSON, and valid JSON that is not a trace. - getInsightOutput: covers the formatted output for a known insight and all three error branches (no insights, unknown insight set id, unknown insight name).
7e939f4 to
14ccacf
Compare
| if ('error' in insight) { | ||
| assert.fail(`Unexpected insight error: ${insight.error}`); | ||
| } |
There was a problem hiding this comment.
| if ('error' in insight) { | |
| assert.fail(`Unexpected insight error: ${insight.error}`); | |
| } | |
| assert(!('error' in insight`),Unexpected insight error: ${insight.error}`); |
| it('checks for the parsedTrace key, not the error message contents', () => { | ||
| const error: TraceParseError = {error: 'parsedTrace'}; | ||
| assert.strictEqual(traceResultIsSuccess(error), false); | ||
| }); |
There was a problem hiding this comment.
| it('checks for the parsedTrace key, not the error message contents', () => { | |
| const error: TraceParseError = {error: 'parsedTrace'}; | |
| assert.strictEqual(traceResultIsSuccess(error), false); | |
| }); |
| if (traceResultIsSuccess(result)) { | ||
| assert.fail('Expected a parse error for a non-trace JSON input.'); | ||
| } | ||
| assert.ok(result.error.length > 0); |
There was a problem hiding this comment.
| if (traceResultIsSuccess(result)) { | |
| assert.fail('Expected a parse error for a non-trace JSON input.'); | |
| } | |
| assert.ok(result.error.length > 0); | |
| assert('error' in result); | |
| assert.ok(result.error.length > 0); |
| if (traceResultIsSuccess(result)) { | ||
| assert.fail('Expected a parse error for invalid JSON input.'); | ||
| } | ||
| assert.match(result.error, /JSON/); |
There was a problem hiding this comment.
| if (traceResultIsSuccess(result)) { | |
| assert.fail('Expected a parse error for invalid JSON input.'); | |
| } | |
| assert.match(result.error, /JSON/); | |
| assert('error' in result); | |
| assert.match(result.error, /JSON/); |
| if (!traceResultIsSuccess(result)) { | ||
| assert.fail(`Unexpected trace parse error: ${result.error}`); | ||
| } |
There was a problem hiding this comment.
| if (!traceResultIsSuccess(result)) { | |
| assert.fail(`Unexpected trace parse error: ${result.error}`); | |
| } | |
| assert(traceResultIsSuccess(result), `Unexpected trace parse error: ${result.error}`); |
| }); | ||
| }); | ||
|
|
||
| describe('traceResultIsSuccess', () => { |
There was a problem hiding this comment.
For this function we should just use a object that mimics the trace, so we can control it.
Also we can't use parseTrace as that itself include it.
| if ('error' in result) { | ||
| assert.fail(`Unexpected parse failure: ${result.error}`); | ||
| } |
There was a problem hiding this comment.
| assert(!('error' in result), `Unexpected parse failure: ${result.error}`); |
Why
src/trace-processing/parse.tsexports several pure, browser-free helpers, buttests/trace-processing/parse.test.tsonly exercised the happy parse path, the trace summary snapshot, and theundefined-buffer error. This PR adds direct unit tests for the remaining exported helpers and branches, so regressions in the discriminant logic or the error contracts are caught without needing a browser.Coverage added
traceResultIsSuccesstruefor the result of a real successful parse (fixture trace)truefor aTraceResultwhoseinsightsisnull(the guard must only depend onparsedTrace)falsefor the result of a failed parsefalsefor aTraceParseErrorobjectfalsefor a near-miss where the error message text is'parsedTrace'— the guard checks the key, not valuesparseRawTraceBuffer(previously untested error branches)'Decoding the trace buffer returned an empty string.'TraceParseErrorinstead of throwingTraceParseErrorinstead of throwinggetInsightOutput(previously untested)NAVIGATION_0/LCPBreakdownfrom theweb-dev-with-commitfixture)Tests only — no changes under
src/, and no new snapshots (the insight output is asserted with a targeted match so devtools-frontend formatter updates don't churn a snapshot).Testing
npm run build— cleannode scripts/test.js tests/trace-processing/parse.test.ts— 15/15 pass (3 suites)npm run test:no-build— full suite green (exit 0)npm run check-format— eslint + prettier clean🤖 Generated with Claude Code