Best JavaScript code snippet using root
console-test.js
Source:console-test.js
1/**2 * Copyright (c) Facebook, Inc. and its affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 * @flow8 */9describe('console', () => {10 let React;11 let ReactDOM;12 let act;13 let fakeConsole;14 let mockError;15 let mockInfo;16 let mockLog;17 let mockWarn;18 let patchConsole;19 let unpatchConsole;20 beforeEach(() => {21 jest.resetModules();22 const Console = require('react-devtools-shared/src/backend/console');23 patchConsole = Console.patch;24 unpatchConsole = Console.unpatch;25 // Patch a fake console so we can verify with tests below.26 // Patching the real console is too complicated,27 // because Jest itself has hooks into it as does our test env setup.28 mockError = jest.fn();29 mockInfo = jest.fn();30 mockLog = jest.fn();31 mockWarn = jest.fn();32 fakeConsole = {33 error: mockError,34 info: mockInfo,35 log: mockLog,36 warn: mockWarn,37 };38 Console.dangerous_setTargetConsoleForTesting(fakeConsole);39 // Note the Console module only patches once,40 // so it's important to patch the test console before injection.41 patchConsole({42 appendComponentStack: true,43 breakOnWarn: false,44 });45 const inject = global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject;46 global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = internals => {47 inject(internals);48 Console.registerRenderer(internals);49 };50 React = require('react');51 ReactDOM = require('react-dom');52 const utils = require('./utils');53 act = utils.act;54 });55 function normalizeCodeLocInfo(str) {56 return (57 str &&58 str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {59 return '\n in ' + name + ' (at **)';60 })61 );62 }63 it('should not patch console methods that do not receive component stacks', () => {64 expect(fakeConsole.error).not.toBe(mockError);65 expect(fakeConsole.info).toBe(mockInfo);66 expect(fakeConsole.log).toBe(mockLog);67 expect(fakeConsole.warn).not.toBe(mockWarn);68 });69 it('should only patch the console once', () => {70 const {error, warn} = fakeConsole;71 patchConsole({72 appendComponentStack: true,73 breakOnWarn: false,74 });75 expect(fakeConsole.error).toBe(error);76 expect(fakeConsole.warn).toBe(warn);77 });78 it('should un-patch when requested', () => {79 expect(fakeConsole.error).not.toBe(mockError);80 expect(fakeConsole.warn).not.toBe(mockWarn);81 unpatchConsole();82 expect(fakeConsole.error).toBe(mockError);83 expect(fakeConsole.warn).toBe(mockWarn);84 });85 it('should pass through logs when there is no current fiber', () => {86 expect(mockLog).toHaveBeenCalledTimes(0);87 expect(mockWarn).toHaveBeenCalledTimes(0);88 expect(mockError).toHaveBeenCalledTimes(0);89 fakeConsole.log('log');90 fakeConsole.warn('warn');91 fakeConsole.error('error');92 expect(mockLog).toHaveBeenCalledTimes(1);93 expect(mockLog.mock.calls[0]).toHaveLength(1);94 expect(mockLog.mock.calls[0][0]).toBe('log');95 expect(mockWarn).toHaveBeenCalledTimes(1);96 expect(mockWarn.mock.calls[0]).toHaveLength(1);97 expect(mockWarn.mock.calls[0][0]).toBe('warn');98 expect(mockError).toHaveBeenCalledTimes(1);99 expect(mockError.mock.calls[0]).toHaveLength(1);100 expect(mockError.mock.calls[0][0]).toBe('error');101 });102 it('should not append multiple stacks', () => {103 const Child = ({children}) => {104 fakeConsole.warn('warn\n in Child (at fake.js:123)');105 fakeConsole.error('error', '\n in Child (at fake.js:123)');106 return null;107 };108 act(() => ReactDOM.render(<Child />, document.createElement('div')));109 expect(mockWarn).toHaveBeenCalledTimes(1);110 expect(mockWarn.mock.calls[0]).toHaveLength(1);111 expect(mockWarn.mock.calls[0][0]).toBe(112 'warn\n in Child (at fake.js:123)',113 );114 expect(mockError).toHaveBeenCalledTimes(1);115 expect(mockError.mock.calls[0]).toHaveLength(2);116 expect(mockError.mock.calls[0][0]).toBe('error');117 expect(mockError.mock.calls[0][1]).toBe('\n in Child (at fake.js:123)');118 });119 it('should append component stacks to errors and warnings logged during render', () => {120 const Intermediate = ({children}) => children;121 const Parent = ({children}) => (122 <Intermediate>123 <Child />124 </Intermediate>125 );126 const Child = ({children}) => {127 fakeConsole.error('error');128 fakeConsole.log('log');129 fakeConsole.warn('warn');130 return null;131 };132 act(() => ReactDOM.render(<Parent />, document.createElement('div')));133 expect(mockLog).toHaveBeenCalledTimes(1);134 expect(mockLog.mock.calls[0]).toHaveLength(1);135 expect(mockLog.mock.calls[0][0]).toBe('log');136 expect(mockWarn).toHaveBeenCalledTimes(1);137 expect(mockWarn.mock.calls[0]).toHaveLength(2);138 expect(mockWarn.mock.calls[0][0]).toBe('warn');139 expect(normalizeCodeLocInfo(mockWarn.mock.calls[0][1])).toEqual(140 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',141 );142 expect(mockError).toHaveBeenCalledTimes(1);143 expect(mockError.mock.calls[0]).toHaveLength(2);144 expect(mockError.mock.calls[0][0]).toBe('error');145 expect(normalizeCodeLocInfo(mockError.mock.calls[0][1])).toBe(146 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',147 );148 });149 it('should append component stacks to errors and warnings logged from effects', () => {150 const Intermediate = ({children}) => children;151 const Parent = ({children}) => (152 <Intermediate>153 <Child />154 </Intermediate>155 );156 const Child = ({children}) => {157 React.useLayoutEffect(() => {158 fakeConsole.error('active error');159 fakeConsole.log('active log');160 fakeConsole.warn('active warn');161 });162 React.useEffect(() => {163 fakeConsole.error('passive error');164 fakeConsole.log('passive log');165 fakeConsole.warn('passive warn');166 });167 return null;168 };169 act(() => ReactDOM.render(<Parent />, document.createElement('div')));170 expect(mockLog).toHaveBeenCalledTimes(2);171 expect(mockLog.mock.calls[0]).toHaveLength(1);172 expect(mockLog.mock.calls[0][0]).toBe('active log');173 expect(mockLog.mock.calls[1]).toHaveLength(1);174 expect(mockLog.mock.calls[1][0]).toBe('passive log');175 expect(mockWarn).toHaveBeenCalledTimes(2);176 expect(mockWarn.mock.calls[0]).toHaveLength(2);177 expect(mockWarn.mock.calls[0][0]).toBe('active warn');178 expect(normalizeCodeLocInfo(mockWarn.mock.calls[0][1])).toEqual(179 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',180 );181 expect(mockWarn.mock.calls[1]).toHaveLength(2);182 expect(mockWarn.mock.calls[1][0]).toBe('passive warn');183 expect(normalizeCodeLocInfo(mockWarn.mock.calls[1][1])).toEqual(184 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',185 );186 expect(mockError).toHaveBeenCalledTimes(2);187 expect(mockError.mock.calls[0]).toHaveLength(2);188 expect(mockError.mock.calls[0][0]).toBe('active error');189 expect(normalizeCodeLocInfo(mockError.mock.calls[0][1])).toBe(190 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',191 );192 expect(mockError.mock.calls[1]).toHaveLength(2);193 expect(mockError.mock.calls[1][0]).toBe('passive error');194 expect(normalizeCodeLocInfo(mockError.mock.calls[1][1])).toBe(195 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',196 );197 });198 it('should append component stacks to errors and warnings logged from commit hooks', () => {199 const Intermediate = ({children}) => children;200 const Parent = ({children}) => (201 <Intermediate>202 <Child />203 </Intermediate>204 );205 class Child extends React.Component<any> {206 componentDidMount() {207 fakeConsole.error('didMount error');208 fakeConsole.log('didMount log');209 fakeConsole.warn('didMount warn');210 }211 componentDidUpdate() {212 fakeConsole.error('didUpdate error');213 fakeConsole.log('didUpdate log');214 fakeConsole.warn('didUpdate warn');215 }216 render() {217 return null;218 }219 }220 const container = document.createElement('div');221 act(() => ReactDOM.render(<Parent />, container));222 act(() => ReactDOM.render(<Parent />, container));223 expect(mockLog).toHaveBeenCalledTimes(2);224 expect(mockLog.mock.calls[0]).toHaveLength(1);225 expect(mockLog.mock.calls[0][0]).toBe('didMount log');226 expect(mockLog.mock.calls[1]).toHaveLength(1);227 expect(mockLog.mock.calls[1][0]).toBe('didUpdate log');228 expect(mockWarn).toHaveBeenCalledTimes(2);229 expect(mockWarn.mock.calls[0]).toHaveLength(2);230 expect(mockWarn.mock.calls[0][0]).toBe('didMount warn');231 expect(normalizeCodeLocInfo(mockWarn.mock.calls[0][1])).toEqual(232 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',233 );234 expect(mockWarn.mock.calls[1]).toHaveLength(2);235 expect(mockWarn.mock.calls[1][0]).toBe('didUpdate warn');236 expect(normalizeCodeLocInfo(mockWarn.mock.calls[1][1])).toEqual(237 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',238 );239 expect(mockError).toHaveBeenCalledTimes(2);240 expect(mockError.mock.calls[0]).toHaveLength(2);241 expect(mockError.mock.calls[0][0]).toBe('didMount error');242 expect(normalizeCodeLocInfo(mockError.mock.calls[0][1])).toBe(243 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',244 );245 expect(mockError.mock.calls[1]).toHaveLength(2);246 expect(mockError.mock.calls[1][0]).toBe('didUpdate error');247 expect(normalizeCodeLocInfo(mockError.mock.calls[1][1])).toBe(248 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',249 );250 });251 it('should append component stacks to errors and warnings logged from gDSFP', () => {252 const Intermediate = ({children}) => children;253 const Parent = ({children}) => (254 <Intermediate>255 <Child />256 </Intermediate>257 );258 class Child extends React.Component<any, any> {259 state = {};260 static getDerivedStateFromProps() {261 fakeConsole.error('error');262 fakeConsole.log('log');263 fakeConsole.warn('warn');264 return null;265 }266 render() {267 return null;268 }269 }270 act(() => ReactDOM.render(<Parent />, document.createElement('div')));271 expect(mockLog).toHaveBeenCalledTimes(1);272 expect(mockLog.mock.calls[0]).toHaveLength(1);273 expect(mockLog.mock.calls[0][0]).toBe('log');274 expect(mockWarn).toHaveBeenCalledTimes(1);275 expect(mockWarn.mock.calls[0]).toHaveLength(2);276 expect(mockWarn.mock.calls[0][0]).toBe('warn');277 expect(normalizeCodeLocInfo(mockWarn.mock.calls[0][1])).toEqual(278 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',279 );280 expect(mockError).toHaveBeenCalledTimes(1);281 expect(mockError.mock.calls[0]).toHaveLength(2);282 expect(mockError.mock.calls[0][0]).toBe('error');283 expect(normalizeCodeLocInfo(mockError.mock.calls[0][1])).toBe(284 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',285 );286 });287 it('should append stacks after being uninstalled and reinstalled', () => {288 const Child = ({children}) => {289 fakeConsole.warn('warn');290 fakeConsole.error('error');291 return null;292 };293 unpatchConsole();294 act(() => ReactDOM.render(<Child />, document.createElement('div')));295 expect(mockWarn).toHaveBeenCalledTimes(1);296 expect(mockWarn.mock.calls[0]).toHaveLength(1);297 expect(mockWarn.mock.calls[0][0]).toBe('warn');298 expect(mockError).toHaveBeenCalledTimes(1);299 expect(mockError.mock.calls[0]).toHaveLength(1);300 expect(mockError.mock.calls[0][0]).toBe('error');301 patchConsole({302 appendComponentStack: true,303 breakOnWarn: false,304 });305 act(() => ReactDOM.render(<Child />, document.createElement('div')));306 expect(mockWarn).toHaveBeenCalledTimes(2);307 expect(mockWarn.mock.calls[1]).toHaveLength(2);308 expect(mockWarn.mock.calls[1][0]).toBe('warn');309 expect(normalizeCodeLocInfo(mockWarn.mock.calls[1][1])).toEqual(310 '\n in Child (at **)',311 );312 expect(mockError).toHaveBeenCalledTimes(2);313 expect(mockError.mock.calls[1]).toHaveLength(2);314 expect(mockError.mock.calls[1][0]).toBe('error');315 expect(normalizeCodeLocInfo(mockError.mock.calls[1][1])).toBe(316 '\n in Child (at **)',317 );318 });319 it('should be resilient to prepareStackTrace', () => {320 Error.prepareStackTrace = function(error, callsites) {321 const stack = ['An error occurred:', error.message];322 for (let i = 0; i < callsites.length; i++) {323 const callsite = callsites[i];324 stack.push(325 '\t' + callsite.getFunctionName(),326 '\t\tat ' + callsite.getFileName(),327 '\t\ton line ' + callsite.getLineNumber(),328 );329 }330 return stack.join('\n');331 };332 const Intermediate = ({children}) => children;333 const Parent = ({children}) => (334 <Intermediate>335 <Child />336 </Intermediate>337 );338 const Child = ({children}) => {339 fakeConsole.error('error');340 fakeConsole.log('log');341 fakeConsole.warn('warn');342 return null;343 };344 act(() => ReactDOM.render(<Parent />, document.createElement('div')));345 expect(mockLog).toHaveBeenCalledTimes(1);346 expect(mockLog.mock.calls[0]).toHaveLength(1);347 expect(mockLog.mock.calls[0][0]).toBe('log');348 expect(mockWarn).toHaveBeenCalledTimes(1);349 expect(mockWarn.mock.calls[0]).toHaveLength(2);350 expect(mockWarn.mock.calls[0][0]).toBe('warn');351 expect(normalizeCodeLocInfo(mockWarn.mock.calls[0][1])).toEqual(352 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',353 );354 expect(mockError).toHaveBeenCalledTimes(1);355 expect(mockError.mock.calls[0]).toHaveLength(2);356 expect(mockError.mock.calls[0][0]).toBe('error');357 expect(normalizeCodeLocInfo(mockError.mock.calls[0][1])).toBe(358 '\n in Child (at **)\n in Intermediate (at **)\n in Parent (at **)',359 );360 });...
Using AI Code Generation
1const root = require('./root');2root.mockError();3const root = require('./root');4root.mockError();5const root = require('./root');6root.mockError();7const root = require('./root');8root.mockError();9const root = require('./root');10root.mockError();11const root = require('./root');12root.mockError();13const root = require('./root');14root.mockError();15const root = require('./root');16root.mockError();
Using AI Code Generation
1var root = require('./');2root.mockError('test', new Error('test error'));3var root = require('./');4root.mockError('test', new Error('test error'));5var root = require('./');6root.mockError('test', new Error('test error'));7var root = require('./');8root.mockError('test', new Error('test error'));
Using AI Code Generation
1var root = require('root');2root.mockError('Test Error');3var module1 = require('module1');4module1.mockError('Test Error');5var module2 = require('module2');6module2.mockError('Test Error');7[MIT](LICENSE)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!