Skip to content

Add Deno.test() stack cleaning #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import escapeStringRegexp from 'escape-string-regexp';
import getHomeDirectory from '#home-directory';

const extractPathRegex = /\s+at.*[(\s](.*)\)?/;
const pathRegex = /^(?:(?:(?:node|node:[\w/]+|(?:(?:node:)?internal\/[\w/]*|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.js)?:\d+:\d+)|native)/;
const pathRegex = /^(?:(?:node|node:[\w\/]+|(?:(?:node:)?internal\/[\w\/]*|ext:[\w\/]+|https:\/\/jsr\.io\/[\w\/@.]+|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.[jt]s)?:\d+:\d+|native)/;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't make unrelated changes (the \)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more readable:

Suggested change
const pathRegex = /^(?:(?:node|node:[\w\/]+|(?:(?:node:)?internal\/[\w\/]*|ext:[\w\/]+|https:\/\/jsr\.io\/[\w\/@.]+|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.[jt]s)?:\d+:\d+|native)/;
const pathRegex = /^(?:(?:node|node:[\w\/]+|(?:(?:node:)?internal\/[\w\/]*|ext:[\w\/]+|https:\/\/jsr\.io\/[\w\/@.]+|.*node_modules\/(?:babel-polyfill|pirates)\/.*)?\w+)(?:\.(?:js|ts))?:\d+:\d+|native)/;


export default function cleanStack(stack, {pretty = false, basePath, pathFilter} = {}) {
const basePathRegex = basePath && new RegExp(`(file://)?${escapeStringRegexp(basePath.replace(/\\/g, '/'))}/?`, 'g');
Expand Down
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,48 @@ test('new stack format on Node.js 15 and later', t => {
t.is(cleanStack(stack), expected);
});

test('Deno.test() stack', t => {
const expected = `Error: message
at /home/dandv/deno-clean-stack.test.ts:4:13`;

const stack = `${expected}
at innerWrapped (ext:cli/40_test.js:191:11)
at exitSanitizer (ext:cli/40_test.js:107:33)
at outerWrapped (ext:cli/40_test.js:134:20)`;
t.is(cleanStack(stack), expected);
});

test('Deno JSR imports via BDD testing stack from https://github.com/denoland/deno/issues/24002#issuecomment-2561927997', t => {
const stack = `error: AssertionError
throw new AssertionError(msg);
^
at assert (https://jsr.io/@std/assert/1.0.10/assert.ts:21:11)
at Object.<anonymous> (/home/dandv/THIS/IS/MY/CODE/THAT/I/CARE/ABOUT/foo.test.ts:372:28)
at eventLoopTick (ext:core/01_core.js:175:7)
at async Function.runTest (https://jsr.io/@std/testing/1.0.8/_test_suite.ts:428:7)
at async Function.runTest (https://jsr.io/@std/testing/1.0.8/_test_suite.ts:416:9)
at async fn (https://jsr.io/@std/testing/1.0.8/_test_suite.ts:377:13)`;

const expected = `error: AssertionError
throw new AssertionError(msg);
^
at Object.<anonymous> (/home/dandv/THIS/IS/MY/CODE/THAT/I/CARE/ABOUT/foo.test.ts:372:28)`;

t.is(cleanStack(stack), expected);
});

test('Deno stack from https://github.com/denoland/deno/issues/27553#issuecomment-2590573359', t => {
const expected = `Error: message
at /home/dandv/deno-clean-stack.test.ts:4:13`;

const stack = `${expected}
at Object.runMicrotasks (ext:core/01_core.js:683:26)
at processTicksAndRejections (ext:deno_node/_next_tick.ts:59:10)
at runNextTicks (ext:deno_node/_next_tick.ts:76:3)
at eventLoopTick (ext:core/01_core.js:182:21)`;
t.is(cleanStack(stack), expected);
});

test('at async', t => {
const basePath = '/base';
const stack = `Error: test
Expand Down
Loading