Skip to content

Commit

Permalink
Provide non-standard stack with invalid type warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed May 13, 2017
1 parent 2bbe024 commit eff8399
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PROJECT_ROOT>/examples/.*
<PROJECT_ROOT>/fixtures/.*
<PROJECT_ROOT>/build/.*
<PROJECT_ROOT>/node_modules/chrome-devtools-frontend/.*
<PROJECT_ROOT>/.*/node_modules/chrome-devtools-frontend/.*
<PROJECT_ROOT>/.*/node_modules/y18n/.*
<PROJECT_ROOT>/.*/__mocks__/.*
<PROJECT_ROOT>/.*/__tests__/.*
Expand Down
10 changes: 10 additions & 0 deletions src/isomorphic/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ var ReactElementValidator = {

info += ReactComponentTreeHook.getCurrentStackAddendum();

var currentSource = props !== null &&
props !== undefined &&
props.__source !== undefined
? props.__source
: null;
ReactComponentTreeHook.pushNonStandardWarningStack(
true,
currentSource,
);
warning(
false,
'React.createElement: type is invalid -- expected a string (for ' +
Expand All @@ -231,6 +240,7 @@ var ReactElementValidator = {
type == null ? type : typeof type,
info,
);
ReactComponentTreeHook.popNonStandardWarningStack();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,49 @@ describe('ReactElementValidator', () => {
"component from the file it's defined in. Check your code at **.",
);
});

it('provides stack via non-standard console.stack for invalid types', () => {
spyOn(console, 'error');

function Foo() {
var Bad = undefined;
return React.createElement(Bad);
}

function App() {
return React.createElement(Foo);
}

try {
console.stack = jest.fn();
console.stackEnd = jest.fn();

expect(() => {
ReactTestUtils.renderIntoDocument(React.createElement(App));
}).toThrow(
'Element type is invalid: expected a string (for built-in components) ' +
'or a class/function (for composite components) but got: undefined. ' +
"You likely forgot to export your component from the file it's " +
'defined in. Check the render method of `Foo`.',
);

expect(console.stack.mock.calls.length).toBe(1);
expect(console.stackEnd.mock.calls.length).toBe(1);

var stack = console.stack.mock.calls[0][0];
expect(Array.isArray(stack)).toBe(true);
expect(stack.map(frame => frame.functionName)).toEqual([
'Foo',
'App',
null,
]);
expect(
stack.map(frame => frame.fileName && frame.fileName.slice(-8)),
).toEqual([null, null, null]);
expect(stack.map(frame => frame.lineNumber)).toEqual([null, null, null]);
} finally {
delete console.stack;
delete console.stackEnd;
}
});
});
51 changes: 51 additions & 0 deletions src/isomorphic/hooks/ReactComponentTreeHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,57 @@ var ReactComponentTreeHook = {

getRootIDs,
getRegisteredIDs: getItemIDs,

pushNonStandardWarningStack(
isCreatingElement: boolean,
currentSource: ?Source,
) {
if (typeof console.stack !== 'function') {
return;
}

var stack = [];
var currentOwner = ReactCurrentOwner.current;
var id = currentOwner && currentOwner._debugID;

try {
if (isCreatingElement) {
stack.push({
fileName: currentSource ? currentSource.fileName : null,
lineNumber: currentSource ? currentSource.lineNumber : null,
functionName: id ? ReactComponentTreeHook.getDisplayName(id) : null,
});
}

while (id) {
var element = ReactComponentTreeHook.getElement(id);
var ownerID = ReactComponentTreeHook.getOwnerID(id);
var ownerName = ownerID
? ReactComponentTreeHook.getDisplayName(ownerID)
: null;
var source = element && element._source;
stack.push({
fileName: source ? source.fileName : null,
lineNumber: source ? source.lineNumber : null,
functionName: ownerName,
});
// Owner stack is more useful for visual representation
id = ownerID || ReactComponentTreeHook.getParentID(id);
}
} catch (err) {
// Internal state is messed up.
// Stop building the stack (it's just a nice to have).
}

console.stack(stack);
},

popNonStandardWarningStack() {
if (typeof console.stackEnd !== 'function') {
return;
}
console.stackEnd();
},
};

module.exports = ReactComponentTreeHook;
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,53 @@ describe('ReactJSXElementValidator', () => {
' Use a static property named `defaultProps` instead.',
);
});

it('provides stack via non-standard console.stack for invalid types', () => {
spyOn(console, 'error');

function Foo() {
var Bad = undefined;
return <Bad />;
}

function App() {
return <Foo />;
}

try {
console.stack = jest.fn();
console.stackEnd = jest.fn();

expect(() => {
ReactTestUtils.renderIntoDocument(<App />);
}).toThrow(
'Element type is invalid: expected a string (for built-in components) ' +
'or a class/function (for composite components) but got: undefined. ' +
"You likely forgot to export your component from the file it's " +
'defined in. Check the render method of `Foo`.',
);

expect(console.stack.mock.calls.length).toBe(1);
expect(console.stackEnd.mock.calls.length).toBe(1);

var stack = console.stack.mock.calls[0][0];
expect(Array.isArray(stack)).toBe(true);
expect(stack.map(frame => frame.functionName)).toEqual([
'Foo',
'App',
null,
]);
expect(
stack.map(frame => frame.fileName && frame.fileName.slice(-8)),
).toEqual(['-test.js', '-test.js', '-test.js']);
expect(stack.map(frame => typeof frame.lineNumber)).toEqual([
'number',
'number',
'number',
]);
} finally {
delete console.stack;
delete console.stackEnd;
}
});
});

0 comments on commit eff8399

Please sign in to comment.