question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Access vue 2 instance of component

See original GitHub issue

Hey there, is there currently a way to access eg. the data, methods or anything of a mounted component or in general the vue instance of the component in the tests?

Something like this (I know this would return the component, not the vue instance but just to illustrate it a bit):

Test.vue

<template>
  <div></div>
</template>

<script>
export default {
  data() {
    return {
      myValue: 'test',
    };
  },
  methods: {
    test() {
      return true;
    },
  }
}
</script>

test.spec.jsx

import {
  test,
  expect,
} from '@playwright/experimental-ct-vue2';
import Test from './Test.vue';

test('my test', async ({ mount }) => {
  const component = await mount(<Test />);
  console.log(component.myValue);
  expect(component.test()).toBeTruthy();
});

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sand4rtcommented, Aug 29, 2022

IMO exposing the component instance (for any framework) leads to testing implementation details. I would instead resemble the tests to the way the software is used. This will eventually lead to better tests that won’t break as quickly if you refactor something and helps preventing false negative and false positive test results. I highly recommend reading this article from the creator of testing library, where he explains this in more detail.

@oliver-freudrich-packwise Can you share the code of a real use case you are trying to test? Then i can help you with an example of what the test could look like?

1reaction
sand4rtcommented, Sep 8, 2022

Keep in mind that i’m not a core member of Playwright and i’m not sure if they are on the same page with this. Vue Test Utils works okay, they just added features that will send you down the wrong path if you’re not careful😕.

This is the current existing documentation on component testing: https://playwright.dev/docs/test-components. I agree that this is not very comprehensive yet. Not sure if possible but maybe you or debs-obrien can help out with this?

Would be good to read the other blog posts from Kent as well: https://kentcdodds.com/blog?q=testing. I find them very useful.

No problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - How to access a component's instance in an another ...
You can use vuex (vue store) for your app. So you can track all the information app-wide. Plan B should be use your...
Read more >
How to access Vue instance in components? - Get Help
Learn how to access Vue instances after having declared them and how to change data properties, invoke methods, etc. on already declared Vue...
Read more >
VueJS - Instances
To start with VueJS, we need to create the instance of Vue, which is called the root Vue Instance. Syntax. var app =...
Read more >
Creating our first Vue component - Learn web development
Now it's time to dive deeper into Vue, and create our own custom component — we'll start by creating a component to represent...
Read more >
Frequently Asked Questions - Vue Community
How do I access the Vuex store outside of Vue components or in hooks without access to instance? If you use the Vue...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found