Skip to content

Commit

Permalink
Split up error and warn methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Mar 21, 2019
1 parent 3524e3f commit 1078cc9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
7 changes: 4 additions & 3 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
cloneElementWithValidation,
} from './ReactElementValidator';
import ReactSharedInternals from './ReactSharedInternals';
import warnWithComponentStack from './warnWithComponentStack';
import {error, warn} from './withComponentStack';
import {enableStableConcurrentModeAPIs} from 'shared/ReactFeatureFlags';

const React = {
Expand All @@ -66,6 +66,9 @@ const React = {
lazy,
memo,

error,
warn,

useCallback,
useContext,
useEffect,
Expand All @@ -91,8 +94,6 @@ const React = {

unstable_ConcurrentMode: REACT_CONCURRENT_MODE_TYPE,

unstable_warnWithComponentStack: warnWithComponentStack,

__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED: ReactSharedInternals,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ function normalizeCodeLocInfo(str) {

function expectWarningToMatch(expectedMessage, expectedStack) {
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledTimes(1);

const [actualMessage, actualStack] = console.error.calls.mostRecent().args;
let [actualMessage, actualStack] = console.error.calls.mostRecent().args;

expect(actualMessage).toBe(expectedMessage);
expect(actualMessage).toBe(`error: ${expectedMessage}`);
expect(normalizeCodeLocInfo(actualStack)).toBe(expectedStack);

[actualMessage, actualStack] = console.warn.calls.mostRecent().args;

expect(actualMessage).toBe(`warn: ${expectedMessage}`);
expect(normalizeCodeLocInfo(actualStack)).toBe(expectedStack);
}

describe('warnWithComponentStack', () => {
describe('withComponentStack', () => {
let React = null;
let ReactTestRenderer = null;
let error = null;
let scheduler = null;
let warnWithComponentStack = null;
let warn = null;

beforeEach(() => {
jest.resetModules();
Expand All @@ -36,22 +43,28 @@ describe('warnWithComponentStack', () => {
ReactTestRenderer = require('react-test-renderer');
scheduler = require('scheduler');

warnWithComponentStack = React.unstable_warnWithComponentStack;
error = React.error;
warn = React.warn;

spyOnDevAndProd(console, 'error');
spyOnDevAndProd(console, 'warn');
});

if (!__DEV__) {
it('passes warnings through to console.error in production mode', () => {
warnWithComponentStack('Warning logged in production mode');
expectWarningToMatch('Warning logged in production mode', undefined);
it('does nothing in production mode', () => {
error('error');
warn('warning');

expect(console.error).toHaveBeenCalledTimes(0);
expect(console.warn).toHaveBeenCalledTimes(0);
});
}

if (__DEV__) {
it('does not include component stack when called outside of render', () => {
warnWithComponentStack('Warning logged outside of render');
expectWarningToMatch('Warning logged outside of render', undefined);
error('error: logged outside of render');
warn('warn: logged outside of render');
expectWarningToMatch('logged outside of render', undefined);
});

it('includes component stack when called from a render method', () => {
Expand All @@ -62,14 +75,15 @@ describe('warnWithComponentStack', () => {
}

function Child() {
warnWithComponentStack('Warning logged in child render method');
error('error: logged in child render method');
warn('warn: logged in child render method');
return null;
}

ReactTestRenderer.create(<Parent />);

expectWarningToMatch(
'Warning logged in child render method',
'logged in child render method',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
Expand All @@ -81,7 +95,8 @@ describe('warnWithComponentStack', () => {

class Child extends React.Component {
UNSAFE_componentWillMount() {
warnWithComponentStack('Warning logged in child cWM lifecycle');
error('error: logged in child cWM lifecycle');
warn('warn: logged in child cWM lifecycle');
}
render() {
return null;
Expand All @@ -91,7 +106,7 @@ describe('warnWithComponentStack', () => {
ReactTestRenderer.create(<Parent />);

expectWarningToMatch(
'Warning logged in child cWM lifecycle',
'logged in child cWM lifecycle',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
Expand All @@ -103,7 +118,8 @@ describe('warnWithComponentStack', () => {

class Child extends React.Component {
componentDidMount() {
warnWithComponentStack('Warning logged in child cDM lifecycle');
error('error: logged in child cDM lifecycle');
warn('warn: logged in child cDM lifecycle');
}
render() {
return null;
Expand All @@ -113,7 +129,7 @@ describe('warnWithComponentStack', () => {
ReactTestRenderer.create(<Parent />);

expectWarningToMatch(
'Warning logged in child cDM lifecycle',
'logged in child cDM lifecycle',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
Expand All @@ -127,7 +143,8 @@ describe('warnWithComponentStack', () => {

function Child() {
React.useEffect(() => {
warnWithComponentStack('Warning logged in child render method');
error('error: logged in child render method');
warn('warn: logged in child render method');
});
return null;
}
Expand All @@ -137,7 +154,7 @@ describe('warnWithComponentStack', () => {
scheduler.flushAll(); // Flush passive effects

expectWarningToMatch(
'Warning logged in child render method',
'logged in child render method',
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@

import ReactSharedInternals from 'shared/ReactSharedInternals';

let warnWithComponentStack = (...args) => console.error(...args);
function noop() {}

let error = noop;
let warn = noop;
if (__DEV__) {
warnWithComponentStack = (...args) => {
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;

error = (...args) => {
const stack = ReactDebugCurrentFrame.getStackAddendum();
if (stack !== '') {
console.error(...args, stack);
} else {
console.error(...args);
}
};

warn = (...args) => {
const stack = ReactDebugCurrentFrame.getStackAddendum();
if (stack !== '') {
console.warn(...args, stack);
} else {
console.warn(...args);
}
};
}

export default warnWithComponentStack;
export {error, warn};

0 comments on commit 1078cc9

Please sign in to comment.