refactor: use __TEST__ flag

This commit is contained in:
Evan You 2019-11-04 11:24:22 -05:00
parent fec47bb3ac
commit 300a705221
6 changed files with 23 additions and 15 deletions

View File

@ -4,9 +4,9 @@ module.exports = {
preset: 'ts-jest', preset: 'ts-jest',
globals: { globals: {
__DEV__: true, __DEV__: true,
__TEST__: true,
__VERSION__: lernaJson.version, __VERSION__: lernaJson.version,
__BROWSER__: false, __BROWSER__: false,
__JSDOM__: true,
__RUNTIME_COMPILE__: true, __RUNTIME_COMPILE__: true,
__FEATURE_OPTIONS__: true, __FEATURE_OPTIONS__: true,
__FEATURE_SUSPENSE__: true __FEATURE_SUSPENSE__: true

View File

@ -1,6 +1,6 @@
// Global compile-time constants // Global compile-time constants
declare var __DEV__: boolean declare var __DEV__: boolean
declare var __JSDOM__: boolean declare var __TEST__: boolean
declare var __BROWSER__: boolean declare var __BROWSER__: boolean
declare var __RUNTIME_COMPILE__: boolean declare var __RUNTIME_COMPILE__: boolean
declare var __COMMIT__: string declare var __COMMIT__: string

View File

@ -10,10 +10,19 @@ import {
mockWarn, mockWarn,
createComponent createComponent
} from '@vue/runtime-test' } from '@vue/runtime-test'
import { setErrorRecovery } from '../src/errorHandling'
describe('error handling', () => { describe('error handling', () => {
mockWarn() mockWarn()
beforeEach(() => {
setErrorRecovery(true)
})
afterEach(() => {
setErrorRecovery(false)
})
test('propagation', () => { test('propagation', () => {
const err = new Error('foo') const err = new Error('foo')
const fn = jest.fn() const fn = jest.fn()
@ -384,9 +393,6 @@ describe('error handling', () => {
}) })
it('should warn unhandled', () => { it('should warn unhandled', () => {
// temporarily simulate non-test env
process.env.NODE_ENV = 'dev'
const onError = jest.spyOn(console, 'error') const onError = jest.spyOn(console, 'error')
onError.mockImplementation(() => {}) onError.mockImplementation(() => {})
const groupCollapsed = jest.spyOn(console, 'groupCollapsed') const groupCollapsed = jest.spyOn(console, 'groupCollapsed')
@ -423,7 +429,6 @@ describe('error handling', () => {
onError.mockRestore() onError.mockRestore()
groupCollapsed.mockRestore() groupCollapsed.mockRestore()
log.mockRestore() log.mockRestore()
process.env.NODE_ENV = 'test'
}) })
// native event handler handling should be tested in respective renderers // native event handler handling should be tested in respective renderers

View File

@ -126,12 +126,15 @@ export function handleError(
logError(err, type, contextVNode) 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) { function logError(err: Error, type: ErrorTypes, contextVNode: VNode | null) {
// default behavior is crash in prod & test, recover in dev. // default behavior is crash in prod & test, recover in dev.
if ( if (__DEV__ && (forceRecover || !__TEST__)) {
__DEV__ &&
!(typeof process !== 'undefined' && process.env.NODE_ENV === 'test')
) {
const info = ErrorTypeStrings[type] const info = ErrorTypeStrings[type]
if (contextVNode) { if (contextVNode) {
pushWarningContext(contextVNode) pushWarningContext(contextVNode)

View File

@ -53,7 +53,7 @@ export function warn(msg: string, ...args: any[]) {
if ( if (
trace.length && trace.length &&
// avoid spamming console during tests // avoid spamming console during tests
(typeof process === 'undefined' || process.env.NODE_ENV !== 'test') !__TEST__
) { ) {
warnArgs.push(`\n`, ...formatTrace(trace)) warnArgs.push(`\n`, ...formatTrace(trace))
} }

View File

@ -147,9 +147,11 @@ function createReplacePlugin(
__VERSION__: `"${lernaJson.version}"`, __VERSION__: `"${lernaJson.version}"`,
__DEV__: isBundlerESMBuild __DEV__: isBundlerESMBuild
? // preserve to be handled by bundlers ? // preserve to be handled by bundlers
`process.env.NODE_ENV !== 'production'` `(process.env.NODE_ENV !== 'production')`
: // hard coded dev/prod builds : // hard coded dev/prod builds
!isProduction, !isProduction,
// this is only used during tests
__TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
// If the build is expected to run directly in the browser (global / esm-browser builds) // If the build is expected to run directly in the browser (global / esm-browser builds)
__BROWSER__: isBrowserBuild, __BROWSER__: isBrowserBuild,
// support compile in browser? // support compile in browser?
@ -157,9 +159,7 @@ function createReplacePlugin(
// support options? // support options?
// the lean build drops options related code with buildOptions.lean: true // the lean build drops options related code with buildOptions.lean: true
__FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN, __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
__FEATURE_SUSPENSE__: true, __FEATURE_SUSPENSE__: true
// this is only used during tests
__JSDOM__: false
}) })
} }