|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import ErrorStackParser from 'error-stack-parser'; |
| 11 | +import {isInternalModule} from '../moduleFilters'; |
| 12 | + |
| 13 | +import * as moduleOne from './__modules__/module-one'; |
| 14 | +import * as moduleTwo from './__modules__/module-two'; |
| 15 | + |
| 16 | +describe('isInternalModule', () => { |
| 17 | + let moduleSourceToRanges; |
| 18 | + |
| 19 | + function parseAndGetStackFrame(error, index = 0) { |
| 20 | + const stackFrame = ErrorStackParser.parse(error)[index]; |
| 21 | + |
| 22 | + // Strip out the portion of the file path that includes "react-devtools-scheduling-profiler" |
| 23 | + // or every frame will be flagged as internal. |
| 24 | + stackFrame.fileName = stackFrame.fileName.replace( |
| 25 | + /.+__tests__/, |
| 26 | + '../__tests__', |
| 27 | + ); |
| 28 | + |
| 29 | + return stackFrame; |
| 30 | + } |
| 31 | + |
| 32 | + function stackFrameToFlamechartStackFrame(stackFrame) { |
| 33 | + return { |
| 34 | + name: 'test', |
| 35 | + timestamp: 0, |
| 36 | + duration: 1, |
| 37 | + scriptUrl: stackFrame.fileName, |
| 38 | + locationLine: stackFrame.lineNumber, |
| 39 | + locationColumn: stackFrame.columnNumber, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + moduleSourceToRanges = new Map(); |
| 45 | + |
| 46 | + let startErrorStackFrame = parseAndGetStackFrame( |
| 47 | + moduleOne.moduleStartError, |
| 48 | + ); |
| 49 | + let stopErrorStackFrame = parseAndGetStackFrame(moduleOne.moduleStopError); |
| 50 | + moduleSourceToRanges.set(startErrorStackFrame.fileName, [ |
| 51 | + [startErrorStackFrame, stopErrorStackFrame], |
| 52 | + ]); |
| 53 | + |
| 54 | + const moduleTwoRanges = []; |
| 55 | + |
| 56 | + startErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleAStartError); |
| 57 | + stopErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleAStopError); |
| 58 | + moduleTwoRanges.push([startErrorStackFrame, stopErrorStackFrame]); |
| 59 | + |
| 60 | + startErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleBStartError); |
| 61 | + stopErrorStackFrame = parseAndGetStackFrame(moduleTwo.moduleBStopError); |
| 62 | + moduleTwoRanges.push([startErrorStackFrame, stopErrorStackFrame]); |
| 63 | + |
| 64 | + moduleSourceToRanges.set(startErrorStackFrame.fileName, moduleTwoRanges); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should properly identify stack frames within the provided module ranges', () => { |
| 68 | + let flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 69 | + parseAndGetStackFrame(moduleOne.innerError), |
| 70 | + ); |
| 71 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 72 | + true, |
| 73 | + ); |
| 74 | + |
| 75 | + flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 76 | + parseAndGetStackFrame(moduleTwo.innerErrorA), |
| 77 | + ); |
| 78 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 79 | + true, |
| 80 | + ); |
| 81 | + |
| 82 | + flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 83 | + parseAndGetStackFrame(moduleTwo.innerErrorB), |
| 84 | + ); |
| 85 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 86 | + true, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should properly identify stack frames outside of the provided module ranges', () => { |
| 91 | + let flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 92 | + parseAndGetStackFrame(moduleOne.outerErrorA), |
| 93 | + ); |
| 94 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 95 | + false, |
| 96 | + ); |
| 97 | + |
| 98 | + flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 99 | + parseAndGetStackFrame(moduleOne.outerErrorB), |
| 100 | + ); |
| 101 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 102 | + false, |
| 103 | + ); |
| 104 | + |
| 105 | + flamechartStackFrame = stackFrameToFlamechartStackFrame( |
| 106 | + parseAndGetStackFrame(moduleTwo.outerError), |
| 107 | + ); |
| 108 | + expect(isInternalModule(moduleSourceToRanges, flamechartStackFrame)).toBe( |
| 109 | + false, |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
0 commit comments