-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alternative stack trace approach (#2119)
* reimpl getDefinitionLineAndUri * disable current stack trace filtering * filter stack traces without modifying error * rename file * rename file * add advice about source maps * WIP feature for transpiled stack traces * improve feature for transpiled stack traces * skip source-mapping scenarios when instrumenting with nyc * update CHANGELOG.md
- Loading branch information
1 parent
e88c691
commit 4ea3227
Showing
15 changed files
with
187 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
@spawn | ||
@source-mapping | ||
Feature: Stack traces | ||
Background: | ||
Given a file named "features/a.feature" with: | ||
""" | ||
Feature: some feature | ||
Scenario: some scenario | ||
Given a passing step | ||
And a failing step | ||
""" | ||
|
||
Rule: Source maps are respected when dealing with transpiled support code | ||
|
||
Just-in-time transpilers like `@babel/register` and `ts-node` emit source maps with | ||
the transpiled code. Cucumber users expect stack traces to point to the line and column | ||
in the original source file when there is an error. | ||
|
||
Background: | ||
Given a file named "features/steps.ts" with: | ||
""" | ||
import { Given } from '@cucumber/cucumber' | ||
interface Something { | ||
field1: string | ||
field2: string | ||
} | ||
Given('a passing step', function() {}) | ||
Given('a failing step', function() { | ||
throw new Error('boom') | ||
}) | ||
""" | ||
|
||
Scenario: commonjs | ||
When I run cucumber-js with `--require-module ts-node/register --require features/steps.ts` | ||
Then the output contains the text: | ||
""" | ||
/features/steps.ts:11:9 | ||
""" | ||
And it fails | ||
|
||
Scenario: esm | ||
Given my env includes "{\"NODE_OPTIONS\":\"--loader ts-node/esm\"}" | ||
When I run cucumber-js with `--import features/steps.ts` | ||
Then the output contains the text: | ||
""" | ||
/features/steps.ts:11:9 | ||
""" | ||
And it fails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import path from 'path' | ||
import { valueOrDefault } from './value_checker' | ||
import { StackFrame } from 'error-stack-parser' | ||
|
||
const projectRootPath = path.join(__dirname, '..') | ||
const projectChildDirs = ['src', 'lib', 'node_modules'] | ||
|
||
export function isFileNameInCucumber(fileName: string): boolean { | ||
return projectChildDirs.some((dir) => | ||
fileName.startsWith(path.join(projectRootPath, dir)) | ||
) | ||
} | ||
|
||
export function filterStackTrace(frames: StackFrame[]): StackFrame[] { | ||
if (isErrorInCucumber(frames)) { | ||
return frames | ||
} | ||
const index = frames.findIndex((x) => isFrameInCucumber(x)) | ||
if (index === -1) { | ||
return frames | ||
} | ||
return frames.slice(0, index) | ||
} | ||
|
||
function isErrorInCucumber(frames: StackFrame[]): boolean { | ||
const filteredFrames = frames.filter((x) => !isFrameInNode(x)) | ||
return filteredFrames.length > 0 && isFrameInCucumber(filteredFrames[0]) | ||
} | ||
|
||
function isFrameInCucumber(frame: StackFrame): boolean { | ||
const fileName = valueOrDefault(frame.getFileName(), '') | ||
return isFileNameInCucumber(fileName) | ||
} | ||
|
||
function isFrameInNode(frame: StackFrame): boolean { | ||
const fileName = valueOrDefault(frame.getFileName(), '') | ||
return !fileName.includes(path.sep) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { format } from 'assertion-error-formatter' | ||
import errorStackParser from 'error-stack-parser' | ||
import { filterStackTrace } from '../filter_stack_trace' | ||
|
||
export function formatError(error: Error, filterStackTraces: boolean): string { | ||
let filteredStack: string | ||
if (filterStackTraces) { | ||
try { | ||
filteredStack = filterStackTrace(errorStackParser.parse(error)) | ||
.map((f) => f.source) | ||
.join('\n') | ||
} catch { | ||
// if we weren't able to parse and filter, we'll settle for the original | ||
} | ||
} | ||
return format(error, { | ||
colorFns: { | ||
errorStack: (stack: string) => | ||
filteredStack ? `\n${filteredStack}` : stack, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.