refactor: use __TEST__ flag
This commit is contained in:
2
packages/global.d.ts
vendored
2
packages/global.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
// Global compile-time constants
|
||||
declare var __DEV__: boolean
|
||||
declare var __JSDOM__: boolean
|
||||
declare var __TEST__: boolean
|
||||
declare var __BROWSER__: boolean
|
||||
declare var __RUNTIME_COMPILE__: boolean
|
||||
declare var __COMMIT__: string
|
||||
|
||||
@@ -10,10 +10,19 @@ import {
|
||||
mockWarn,
|
||||
createComponent
|
||||
} from '@vue/runtime-test'
|
||||
import { setErrorRecovery } from '../src/errorHandling'
|
||||
|
||||
describe('error handling', () => {
|
||||
mockWarn()
|
||||
|
||||
beforeEach(() => {
|
||||
setErrorRecovery(true)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
setErrorRecovery(false)
|
||||
})
|
||||
|
||||
test('propagation', () => {
|
||||
const err = new Error('foo')
|
||||
const fn = jest.fn()
|
||||
@@ -384,9 +393,6 @@ describe('error handling', () => {
|
||||
})
|
||||
|
||||
it('should warn unhandled', () => {
|
||||
// temporarily simulate non-test env
|
||||
process.env.NODE_ENV = 'dev'
|
||||
|
||||
const onError = jest.spyOn(console, 'error')
|
||||
onError.mockImplementation(() => {})
|
||||
const groupCollapsed = jest.spyOn(console, 'groupCollapsed')
|
||||
@@ -423,7 +429,6 @@ describe('error handling', () => {
|
||||
onError.mockRestore()
|
||||
groupCollapsed.mockRestore()
|
||||
log.mockRestore()
|
||||
process.env.NODE_ENV = 'test'
|
||||
})
|
||||
|
||||
// native event handler handling should be tested in respective renderers
|
||||
|
||||
@@ -126,12 +126,15 @@ export function handleError(
|
||||
logError(err, type, contextVNode)
|
||||
}
|
||||
|
||||
// Test-only toggle for testing the uhandled warning behavior
|
||||
let forceRecover = false
|
||||
export function setErrorRecovery(value: boolean) {
|
||||
forceRecover = value
|
||||
}
|
||||
|
||||
function logError(err: Error, type: ErrorTypes, contextVNode: VNode | null) {
|
||||
// default behavior is crash in prod & test, recover in dev.
|
||||
if (
|
||||
__DEV__ &&
|
||||
!(typeof process !== 'undefined' && process.env.NODE_ENV === 'test')
|
||||
) {
|
||||
if (__DEV__ && (forceRecover || !__TEST__)) {
|
||||
const info = ErrorTypeStrings[type]
|
||||
if (contextVNode) {
|
||||
pushWarningContext(contextVNode)
|
||||
|
||||
@@ -53,7 +53,7 @@ export function warn(msg: string, ...args: any[]) {
|
||||
if (
|
||||
trace.length &&
|
||||
// avoid spamming console during tests
|
||||
(typeof process === 'undefined' || process.env.NODE_ENV !== 'test')
|
||||
!__TEST__
|
||||
) {
|
||||
warnArgs.push(`\n`, ...formatTrace(trace))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user