Skip to content

Commit

Permalink
stack-wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 31, 2020
1 parent 7a518cc commit 9ead626
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
17 changes: 11 additions & 6 deletions packages/enzyme-adapter-react-17/src/ReactSeventeenAdapter.js
Expand Up @@ -273,7 +273,10 @@ function toTree(vnode) {
case FiberTags.Mode:
case FiberTags.ContextProvider:
case FiberTags.ContextConsumer:
case FiberTags.Lazy:
case FiberTags.OffscreenComponent: {
return childrenToTree(node.child);
}
case FiberTags.Profiler:
case FiberTags.ForwardRef: {
return {
Expand All @@ -297,11 +300,6 @@ function toTree(vnode) {
rendered: childrenToTree(node.child),
};
}
case FiberTags.Lazy:
return childrenToTree(node.child);
case FiberTags.OffscreenComponent: {
return toTree(node.child);
}
default: {
throw new Error(`Enzyme Internal Error: unknown node with tag ${node.tag}`);
}
Expand Down Expand Up @@ -504,6 +502,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter {
nodeTypeFromType,
adapter.displayNameOfNode,
catchingType,
true,
);
},
simulateEvent(node, event, mock) {
Expand Down Expand Up @@ -708,6 +707,7 @@ class ReactSeventeenAdapter extends EnzymeAdapter {
nodeTypeFromType,
adapter.displayNameOfNode,
cachedNode.type,
true,
);
},
simulateEvent(node, event, ...args) {
Expand All @@ -732,7 +732,12 @@ class ReactSeventeenAdapter extends EnzymeAdapter {
values,
location,
displayNameOfNode(cachedNode),
() => getComponentStack(hierarchy.concat([cachedNode])),
() => getComponentStack(
hierarchy.concat([cachedNode]),
undefined,
undefined,
true,
),
);
},
};
Expand Down
7 changes: 6 additions & 1 deletion packages/enzyme-adapter-utils/src/Utils.js
Expand Up @@ -274,6 +274,7 @@ export function getComponentStack(
hierarchy,
getNodeType = nodeTypeFromType,
getDisplayName = displayNameOfNode,
useAtStyleStack = false,
) {
const tuples = hierarchy.filter((node) => node.type !== RootFinder).map((x) => [
getNodeType(x.type),
Expand All @@ -286,6 +287,9 @@ export function getComponentStack(
// TODO: create proper component stack for react 17
return tuples.map(([, name], i, arr) => {
const [, closestComponent] = arr.slice(i + 1).find(([nodeType]) => nodeType !== 'host') || [];
if (useAtStyleStack) {
return `\n at ${name}${closestComponent ? ' ($FILENAME:$LINE:$COL)' : ''}`;
}
return `\n in ${name}${closestComponent ? ` (created by ${closestComponent})` : ''}`;
}).join('');
}
Expand All @@ -298,6 +302,7 @@ export function simulateError(
getNodeType = nodeTypeFromType,
getDisplayName = displayNameOfNode,
catchingType = {},
useAtStyleStack = false,
) {
const instance = catchingInstance || {};

Expand All @@ -315,7 +320,7 @@ export function simulateError(
}

if (componentDidCatch) {
const componentStack = getComponentStack(hierarchy, getNodeType, getDisplayName);
const componentStack = getComponentStack(hierarchy, getNodeType, getDisplayName, useAtStyleStack);
componentDidCatch.call(instance, error, { componentStack });
}
}
Expand Down
Expand Up @@ -114,11 +114,11 @@ export default function describeCDC({
expect(info).to.deep.equal({
componentStack: is('>= 17')
? `
at Thrower (${__filename}:2369:29)
at span
at div
at ErrorBoundary (${__filename}:2386:13)
at WrapperComponent (${path.join(path.dirname(__filename), '../../enzyme-adapter-utils/build/createMountWrapper.js')}:114:7)`
at Thrower (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:2369:29`})
at span (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:2369:29`})
at div (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:2369:29`})
at ErrorBoundary (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:2369:29`})
at WrapperComponent`
: `
in Thrower (created by ErrorBoundary)
in span (created by ErrorBoundary)${hasFragments ? '' : `
Expand Down Expand Up @@ -190,7 +190,14 @@ export default function describeCDC({
const [[actualError, info]] = spy.args;
expect(actualError).to.satisfy(properErrorMessage);
expect(info).to.deep.equal({
componentStack: `
componentStack: is('>= 17')
? `
at Thrower (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:75:27`})
at span
at div
at ErrorBoundary (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:92:11`})
at WrapperComponent (${isShallow ? '$FILENAME:$LINE:$COL' : `${path.join(__dirname, '../../../../enzyme-adapter-utils/build/createMountWrapper.js')}:114:7`})`
: `
in Thrower (created by ErrorBoundary)
in span (created by ErrorBoundary)${hasFragments ? '' : `
in main (created by ErrorBoundary)`}
Expand All @@ -216,12 +223,12 @@ export default function describeCDC({
expect(info).to.deep.equal({
componentStack: is('>= 17')
? `
at Thrower (${__filename}:2369:29)
at Thrower (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:75:27`})
at span
at div
at ErrorBoundary (${__filename}:2386:13)
at ErrorBoundary (${isShallow ? '$FILENAME:$LINE:$COL' : `${__filename}:92:11`})
at ErrorSFC
at WrapperComponent (${path.join(path.dirname(__filename), '../../enzyme-adapter-utils/build/createMountWrapper.js')}:114:7)`
at WrapperComponent (${isShallow ? '$FILENAME:$LINE:$COL' : `${path.join(__dirname, '../../../../enzyme-adapter-utils/build/createMountWrapper.js')}:114:7`})`
: `
in Thrower (created by ErrorBoundary)
in span (created by ErrorBoundary)${hasFragments ? '' : `
Expand Down
8 changes: 7 additions & 1 deletion packages/enzyme-test-suite/test/shared/lifecycles/misc.jsx
Expand Up @@ -354,7 +354,13 @@ export default function describeMisc({
|| error.message === reactError.message
);
const expectedInfo = {
componentStack: `
componentStack: is('>= 17')
? `
at Thrower ($FILENAME:$LINE:$COL)
at div ($FILENAME:$LINE:$COL)
at ErrorBoundary ($FILENAME:$LINE:$COL)
at WrapperComponent`
: `
in Thrower (created by ErrorBoundary)
in div (created by ErrorBoundary)
in ErrorBoundary (created by WrapperComponent)
Expand Down

0 comments on commit 9ead626

Please sign in to comment.