]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: use __TEST__ flag
authorEvan You <yyx990803@gmail.com>
Mon, 4 Nov 2019 16:24:22 +0000 (11:24 -0500)
committerEvan You <yyx990803@gmail.com>
Mon, 4 Nov 2019 19:28:01 +0000 (14:28 -0500)
jest.config.js
packages/global.d.ts
packages/runtime-core/__tests__/errorHandling.spec.ts
packages/runtime-core/src/errorHandling.ts
packages/runtime-core/src/warning.ts
rollup.config.js

index 343a47e7808ab4a11e161afb02e83a1779cbaa59..25c2b0f03a7225477032e9e3a3c25f7f91f9768e 100644 (file)
@@ -4,9 +4,9 @@ module.exports = {
   preset: 'ts-jest',
   globals: {
     __DEV__: true,
+    __TEST__: true,
     __VERSION__: lernaJson.version,
     __BROWSER__: false,
-    __JSDOM__: true,
     __RUNTIME_COMPILE__: true,
     __FEATURE_OPTIONS__: true,
     __FEATURE_SUSPENSE__: true
index d84d24ac08b34a020d790460bd38b73753c982a9..5ddded48131b244084a7a8c3d08fd63223bfe9bc 100644 (file)
@@ -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
index feb2b30c8793e57d36436b8b73c91530e41f45ee..77ddc8dcf482701755edf2f9e44482640ea2a892 100644 (file)
@@ -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
index cc5e42c4a2a3306d43ff66b1d17a2153aa1116f2..88d5af350e5c7bffea1908433d4642b6559c06ca 100644 (file)
@@ -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)
index 0c6d2bda936773b7be4b67c8c1b4a84537b3b1eb..8e5a84d69121c90e63c27619111c08292d15e82e 100644 (file)
@@ -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))
     }
index 66b8ddcce608a4376c1d7523246e61cd7d7d3e0f..e689068f281be18e280df66155185771f2db0e3f 100644 (file)
@@ -147,9 +147,11 @@ function createReplacePlugin(
     __VERSION__: `"${lernaJson.version}"`,
     __DEV__: isBundlerESMBuild
       ? // preserve to be handled by bundlers
-        `process.env.NODE_ENV !== 'production'`
+        `(process.env.NODE_ENV !== 'production')`
       : // hard coded dev/prod builds
         !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)
     __BROWSER__: isBrowserBuild,
     // support compile in browser?
@@ -157,9 +159,7 @@ function createReplacePlugin(
     // support options?
     // the lean build drops options related code with buildOptions.lean: true
     __FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
-    __FEATURE_SUSPENSE__: true,
-    // this is only used during tests
-    __JSDOM__: false
+    __FEATURE_SUSPENSE__: true
   })
 }