MCPcopy Create free account
hub / github.com/vuejs/test-utils / beforeCreate

Function beforeCreate

src/createInstance.ts:229–265  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

227 if (global?.mocks) {
228 const mixin: ComponentOptions = {
229 beforeCreate() {
230 // we need to differentiate components that are or not not `script setup`
231 // otherwise we run into a proxy set error
232 // due to https://github.com/vuejs/core/commit/f73925d76a76ee259749b8b48cb68895f539a00f#diff-ea4d1ddabb7e22e17e80ada458eef70679af4005df2a1a6b73418fec897603ceR404
233 // introduced in Vue v3.2.45
234 // Also ensures not to include option API components in this block
235 // since they can also have setup state but need to be patched using
236 // the regular method.
237 if (isScriptSetup(this)) {
238 // add the mocks to setupState
239 for (const [k, v] of Object.entries(
240 global.mocks as { [key: string]: any }
241 )) {
242 // we do this in a try/catch, as some properties might be read-only
243 try {
244 this.$.setupState[k] = v
245 // eslint-disable-next-line no-empty
246 } catch (e) {}
247 }
248 // also intercept the proxy calls to make the mocks available on the instance
249 // (useful when a template access a global function like $t and the developer wants to mock it)
250 ;(this.$ as any).proxy = new Proxy((this.$ as any).proxy, {
251 get(target, key) {
252 if (key in global.mocks) {
253 return global.mocks[key as string]
254 }
255 return target[key]
256 }
257 })
258 } else {
259 for (const [k, v] of Object.entries(
260 global.mocks as { [key: string]: any }
261 )) {
262 ;(this as any)[k] = v
263 }
264 }
265 }
266 }
267
268 app.mixin(mixin)

Callers

nothing calls this directly

Calls 1

isScriptSetupFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…