-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createLocalVue errorHandler Option (#1670)
* refactor(createlocalvue): move createLocalVue to shared utils * refactor(createlocalvue): rename createLocalVue to _createLocalVue Rename createLocalVue to _createLocalVue to indicate private use * improvement(components): add Sync and Async components for testing * improvement(flow): add VueConfig to Flow * improvement(index): export new createLocalVue as default in Index * improvement(_createlocalvue): allow registration of user defined config Allow VueConfig to be passed in via createLocalVue to be registered on the created vue instance * improvement(find): add findAllParentInstances to the find API add findAllParentInstances method to traverse a component's parent to find globally registered properties via createLocalVue * improvement(mount): pass localVue into mounted createLocalVue Pass localVue into mounted createLocalVue to register localVue properties on component * improvement(error): call user defined errorHandler if defined Call the user defined errorHandler created on the localVue instance via createLocalVue. This is called in the VTU global error handler * improvement(createlocalvue): add tests to createLocalVue errorHandler add tests to createLocalVue errorHandler to test invocation on sync and async throws * docs(createlocalvue): document public createLocalVue API * docs(createlocalvue): document the createLocalVue internal API * docs(createlocalvue): document the errorHandler option in createLocalVue * fix(createlocalvue tests): wrap createLocalVue async test in try/finally Wrap async error test for createLocalVue errorHandler in try/finally to prevent global errors * fix(createlocalvue): skip async component throws for vue < 2.6 * improvement(find and error): add additional type safety to find & error Add additional type safety to find and error for older versions of vue that might propagate a vm value of null/undefined * fix(createlocalvue): only run sync error tests for vue versions < 2.4
- Loading branch information
1 parent
085ef2a
commit e91effe
Showing
14 changed files
with
309 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import cloneDeep from 'lodash/cloneDeep' | ||
|
||
/** | ||
* Used internally by vue-server-test-utils and test-utils to propagate/create vue instances. | ||
* This method is wrapped by createLocalVue in test-utils to provide a different public API signature | ||
* @param {Component} _Vue | ||
* @param {VueConfig} config | ||
* @returns {Component} | ||
*/ | ||
function _createLocalVue( | ||
_Vue: Component = Vue, | ||
config: VueConfig = {} | ||
): Component { | ||
const instance = _Vue.extend() | ||
|
||
// clone global APIs | ||
Object.keys(_Vue).forEach(key => { | ||
if (!instance.hasOwnProperty(key)) { | ||
const original = _Vue[key] | ||
// cloneDeep can fail when cloning Vue instances | ||
// cloneDeep checks that the instance has a Symbol | ||
// which errors in Vue < 2.17 (https://github.com/vuejs/vue/pull/7878) | ||
try { | ||
instance[key] = | ||
typeof original === 'object' ? cloneDeep(original) : original | ||
} catch (e) { | ||
instance[key] = original | ||
} | ||
} | ||
}) | ||
|
||
// config is not enumerable | ||
instance.config = cloneDeep(Vue.config) | ||
|
||
// if a user defined errorHandler is defined by a localVue instance via createLocalVue, register it | ||
instance.config.errorHandler = config.errorHandler || Vue.config.errorHandler | ||
|
||
// option merge strategies need to be exposed by reference | ||
// so that merge strats registered by plugins can work properly | ||
instance.config.optionMergeStrategies = Vue.config.optionMergeStrategies | ||
|
||
// make sure all extends are based on this instance. | ||
// this is important so that global components registered by plugins, | ||
// e.g. router-link are created using the correct base constructor | ||
instance.options._base = instance | ||
|
||
// compat for vue-router < 2.7.1 where it does not allow multiple installs | ||
if (instance._installedPlugins && instance._installedPlugins.length) { | ||
instance._installedPlugins.length = 0 | ||
} | ||
const use = instance.use | ||
instance.use = (plugin, ...rest) => { | ||
if (plugin.installed === true) { | ||
plugin.installed = false | ||
} | ||
if (plugin.install && plugin.install.installed === true) { | ||
plugin.install.installed = false | ||
} | ||
use.call(instance, plugin, ...rest) | ||
} | ||
return instance | ||
} | ||
|
||
export default _createLocalVue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,14 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import cloneDeep from 'lodash/cloneDeep' | ||
|
||
function createLocalVue(_Vue: Component = Vue): Component { | ||
const instance = _Vue.extend() | ||
|
||
// clone global APIs | ||
Object.keys(_Vue).forEach(key => { | ||
if (!instance.hasOwnProperty(key)) { | ||
const original = _Vue[key] | ||
// cloneDeep can fail when cloning Vue instances | ||
// cloneDeep checks that the instance has a Symbol | ||
// which errors in Vue < 2.17 (https://github.com/vuejs/vue/pull/7878) | ||
try { | ||
instance[key] = | ||
typeof original === 'object' ? cloneDeep(original) : original | ||
} catch (e) { | ||
instance[key] = original | ||
} | ||
} | ||
}) | ||
|
||
// config is not enumerable | ||
instance.config = cloneDeep(Vue.config) | ||
|
||
instance.config.errorHandler = Vue.config.errorHandler | ||
|
||
// option merge strategies need to be exposed by reference | ||
// so that merge strats registered by plugins can work properly | ||
instance.config.optionMergeStrategies = Vue.config.optionMergeStrategies | ||
|
||
// make sure all extends are based on this instance. | ||
// this is important so that global components registered by plugins, | ||
// e.g. router-link are created using the correct base constructor | ||
instance.options._base = instance | ||
|
||
// compat for vue-router < 2.7.1 where it does not allow multiple installs | ||
if (instance._installedPlugins && instance._installedPlugins.length) { | ||
instance._installedPlugins.length = 0 | ||
} | ||
const use = instance.use | ||
instance.use = (plugin, ...rest) => { | ||
if (plugin.installed === true) { | ||
plugin.installed = false | ||
} | ||
if (plugin.install && plugin.install.installed === true) { | ||
plugin.install.installed = false | ||
} | ||
use.call(instance, plugin, ...rest) | ||
} | ||
return instance | ||
import _createLocalVue from 'shared/create-local-vue' | ||
|
||
/** | ||
* Returns a local vue instance to add components, mixins and install plugins without polluting the global Vue class | ||
* @param {VueConfig} config | ||
* @returns {Component} | ||
*/ | ||
function createLocalVue(config: VueConfig = {}): Component { | ||
return _createLocalVue(undefined, config) | ||
} | ||
|
||
export default createLocalVue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.