-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix: normalize test body invocationDetails from stack traces
#32699
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
base: develop
Are you sure you want to change the base?
Changes from 9 commits
3bb1471
b6bc0f5
926e1fc
49165cf
61311ee
0a8de00
a1480d8
36064a6
65d656c
c0fb620
96a9a10
d0dfb88
5d5cc2d
79c51da
a7e5bb2
7bb0f76
42d6495
1929cba
ec9dad3
85756f0
1bc1d68
4a53ed1
cde2e2c
badf190
d7f64f0
76e2319
31af0fb
dbe8c6f
e9d72cf
4df6616
1dfe50c
6fa202f
50418a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,53 @@ const stackWithLinesRemoved = (stack, cb) => { | |
| return unsplitStack(messageLines, remainingStackLines) | ||
| } | ||
|
|
||
| const stackWithWrappingLinesRemoved = (stack) => { | ||
| const modifiedStack = stackWithLinesRemoved(stack, (lines) => { | ||
| if (Cypress.isBrowser('chrome')) { | ||
| // There are cases where there are other lines in the stack trace before the invocation (eg. `context.it.only`, `createRunnable`, etc) | ||
| // Remove lines from the start until the top line starts with 'at eval' or 'at Suite.eval' so that we only keep the actual invocation line. | ||
astone123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while ( | ||
|
||
| lines.length > 0 && | ||
| !( | ||
| lines[0].trim().startsWith('at eval') || | ||
| lines[0].trim().startsWith('at Suite.eval') | ||
| ) | ||
| ) { | ||
| lines.shift() | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else if (Cypress.isBrowser('firefox')) { | ||
| const isTestInvocationLine = (line: string) => { | ||
| const splitAtAt = line.split('@') | ||
|
|
||
| // firefox stacks traces look like: | ||
| // functionName@https://aicotravel.com/__cypress/tests?p=cypress/support/e2e.js:444:14 | ||
astone123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // @https://aicotravel.com/__cypress/tests?p=cypress/e2e/spec.cy.js:43:3 | ||
| // @https://aicotravel.com/__cypress/tests?p=cypress/e2e/spec.cy.js:45:12 | ||
| // evalScripts/<@cypress:///../driver/src/cypress/script_utils.ts:38:23 | ||
| // | ||
| // the actual invocation details will be at the first line with no function name | ||
| return splitAtAt.length > 1 && splitAtAt[0].trim().length === 0 | ||
| } | ||
|
|
||
| while ( | ||
|
||
| lines.length > 0 && | ||
| !isTestInvocationLine(lines[0]) | ||
| ) { | ||
| lines.shift() | ||
| } | ||
| } | ||
|
|
||
| return lines | ||
| }) | ||
|
|
||
| // if we removed all the lines then something went wrong. return the original stack instead | ||
| if (modifiedStack.length === 0) { | ||
|
||
| return stack | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return modifiedStack | ||
| } | ||
astone123 marked this conversation as resolved.
Show resolved
Hide resolved
astone123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const stackWithLinesDroppedFromMarker = (stack, marker, includeLast = false) => { | ||
| return stackWithLinesRemoved(stack, (lines) => { | ||
| // drop lines above the marker | ||
|
|
@@ -124,7 +171,7 @@ type InvocationDetails = { | |
| } | ||
|
|
||
| // used to determine codeframes for hook/test/etc definitions rather than command invocations | ||
| const getInvocationDetails = (specWindow, sourceMapProjectRoot: string): InvocationDetails | undefined => { | ||
| const getInvocationDetails = (specWindow, sourceMapProjectRoot: string, type?: 'test-body'): InvocationDetails | undefined => { | ||
| if (specWindow.Error) { | ||
| let stack = (new specWindow.Error()).stack | ||
|
|
||
|
|
@@ -146,6 +193,11 @@ const getInvocationDetails = (specWindow, sourceMapProjectRoot: string): Invocat | |
| } | ||
| } | ||
|
|
||
| // if the hook is the test body, we will try to remove the lines that are not the actual invocation of the test | ||
| if (type === 'test-body') { | ||
| stack = stackWithWrappingLinesRemoved(stack) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Prevent errors from missing Cypress object.Accessing |
||
|
|
||
| const details: Omit<InvocationDetails, 'stack'> = getSourceDetailsForFirstLine(stack, sourceMapProjectRoot) || {} | ||
|
|
||
| ;(details as any).stack = stack | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ describe('stack_utils', () => { | |||||||||||||||||||
| // @ts-expect-error | ||||||||||||||||||||
| global.Cypress = { | ||||||||||||||||||||
| config: vi.fn(), | ||||||||||||||||||||
| isBrowser: vi.fn(() => true), | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| vi.resetAllMocks() | ||||||||||||||||||||
|
|
@@ -37,7 +38,7 @@ describe('stack_utils', () => { | |||||||||||||||||||
| return stack | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const config = () => projectRoot | ||||||||||||||||||||
| const config = projectRoot | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for (const scenario of scenarios) { | ||||||||||||||||||||
| const { browser, build, specFrame, stack: scenarioStack } = scenario | ||||||||||||||||||||
|
|
@@ -64,6 +65,80 @@ describe('stack_utils', () => { | |||||||||||||||||||
| }) | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| it('returns the correct invocation details for a grep stack trace', () => { | ||||||||||||||||||||
|
||||||||||||||||||||
| it('returns the correct invocation details for a grep stack trace', () => { | |
| it('returns the correct invocation details for a test with a stack that needs to be trimmed', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const stack = `Error\n at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14)\n | |
| at eval (http://localhost:3000/__cypress/tests?p=cypress/e2e/spec.cy.js:14:1)\n | |
| at eval (http://localhost:3000/__cypress/tests?p=cypress/e2e/spec.cy.js:18:12)\n | |
| at eval (<anonymous>)\n | |
| const stack = `Error | |
| at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14) | |
| at eval (http://localhost:3000/__cypress/tests?p=cypress/e2e/spec.cy.js:14:1) | |
| at eval (http://localhost:3000/__cypress/tests?p=cypress/e2e/spec.cy.js:18:12) | |
| at eval (<anonymous>) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't look like this has been updated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be updated now
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Error should be on it's own line:
| const stack = `Error at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14) | |
| const stack = `Error | |
| at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| it('returns the original stack if it cannot be normalized for a test body', () => { | |
| it('returns the original stack if it cannot be normalized for a test', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Error should be on it's own line:
| const stack = `Error at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14) | |
| const stack = `Error | |
| at itGrep (http://localhost:3000/__cypress/tests?p=cypress/support/e2e.js:444:14) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| function myIt (name, fn) { | ||
| it(name, fn) | ||
| } | ||
|
|
||
| myIt('test 1', () => { | ||
| cy.log('testBody 1') | ||
| }) | ||
|
|
||
| myIt('test 2', () => { | ||
| cy.log('testBody 2') | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to test the new logic because I don't think it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right - removed this test