nuxt / test-utils

🧪 Test utilities for Nuxt
http://nuxt.com/docs/getting-started/testing
MIT License
287 stars 74 forks source link

Test fail if component contain const error = ref(false); #835

Closed xibman closed 1 month ago

xibman commented 1 month ago

Environment


Reproduction

Just create a simple component with :

<script lang="ts" setup>
const error = ref(false);
</script>

Simple test

it('mount', async () => {
    const component = await mountSuspended(TheFooter);
    expect(component.html()).toMatchSnapshot();
  });

Describe the bug

TypeError: 'set' on proxy: trap returned falsish for property 'error'
 ❯ Proxy.clonedComponent.render ../../../node_modules/@nuxt/test-utils/dist/runtime-utils/index.mjs:131:44
    129|                         renderContext[key] = passedProps[key];
    130|                       }
    131|                       return render.call(this, renderContext, ...args);
       |                                            ^
    132|                     } : void 0,
    133|                     setup: setup ? (props2) => wrappedSetup(props2, setupContext) : void 0
 ❯ renderComponentRoot ../../../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:885:16
 ❯ ReactiveEffect.componentUpdateFn [as fn] ../../../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5969:46
 ❯ ReactiveEffect.run ../../../node_modules/@vue/reactivity/dist/reactivity.cjs.js:181:19
 ❯ instance.update ../../../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6100:16
 ❯ setupRenderEffect ../../../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6110:5
 ❯ ../../../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:1639:9

Additional context

No response

Logs

No response

dosubot[bot] commented 1 month ago

The error you're encountering is likely due to Vue's reactivity system trying to track or modify a property on a reactive object that hasn't been properly initialized. Here's how you can address this issue:

  1. Ensure all reactive properties are initialized: Before modifying or tracking properties on a reactive object, make sure they are initialized. This is important because Vue 3's reactivity system, which relies on JavaScript Proxies, needs to be aware of these properties to track their changes.

  2. Handle dynamic properties correctly: If your component or test dynamically adds new properties to a reactive object, you must ensure these properties are made reactive. This can be achieved by reassigning the reactive object with a new object that includes the new property, using either the spread operator or Object.assign.

  3. Check your mountSuspended usage: Make sure that the component and any props passed to it are correctly set up. If your component's setup function or test setup involves dynamically adding properties to objects, these properties should be initialized beforehand.

  4. Debugging: If the problem persists, add debug statements to verify that all expected properties are present and reactive before any operations are performed on them.

Here's an example of how to handle dynamic properties in a reactive object within Vue 3:

// Assuming `reactiveObject` is a reactive object and you need to add a new property
const newProperty = { key: 'value' };
// Reassign the object to include the new property, maintaining reactivity
reactiveObject = reactive({ ...reactiveObject, ...newProperty });

This ensures Vue's reactivity system can track the new property, helping to avoid the error you're facing. For more detailed guidance, consider reviewing the source code of the @nuxt/test-utils package, especially the implementation of mountSuspended.

#### Details **Sources** - [src/runtime-utils/mount.ts](https://github.com/nuxt/test-utils/blob/main/src/runtime-utils/mount.ts) - [src/runtime-utils/render.ts](https://github.com/nuxt/test-utils/blob/main/src/runtime-utils/render.ts) - [src/config.ts](https://github.com/nuxt/test-utils/blob/main/src/config.ts) - [examples/i18n/test/index.spec.ts](https://github.com/nuxt/test-utils/blob/main/examples/i18n/test/index.spec.ts) - [src/module.ts](https://github.com/nuxt/test-utils/blob/main/src/module.ts) - [src/runtime/global-setup.ts](https://github.com/nuxt/test-utils/blob/main/src/runtime/global-setup.ts) - [src/runtime/nuxt-root.ts](https://github.com/nuxt/test-utils/blob/main/src/runtime/nuxt-root.ts)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

xibman commented 1 month ago

If the constant is renamed to formError for example the problem disappear !

lutejka commented 1 month ago

Seems like this is the same issue as in https://github.com/nuxt/test-utils/issues/684

xibman commented 1 month ago

@lutejka totally !

manniL commented 1 month ago

Closing in favor of #684