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.

Provide options to adapt the document before mount

See original GitHub issue

Describe the feature We are using Vuetify as component library in one of our projects. I would like to introduce vue-testing-library and came up with some setup troubles on the way. Vuetify requires a wrapper element containing the attribute data-app. This element is used to append components like dialog to the root DOM level. The required setup can be done using jest setup files. Thought, the dialog content can not be found when using the exposed queries of the vue-testing-libraries render() method.

To Reproduce Steps to reproduce the behavior:

  • Init a vue app with vuetify
  • Create a basic dialog
  • write a test which should validate that the dialog content is visible when a button is clicked
  • Add Vuetify and <div data-app></div> to your test DOM instance before mount

=> queries like getByText will not query the dialog content

I tried to create a codesandbox, but failed. Is there a working example for vue-testing-library? https://codesandbox.io/s/vuetify-with-vue-testing-library-bhu8f?previewwindow=tests

Expected behavior I expect a configuration option like it exists for react:

  • baseElement: Specify the base element for the queries.
  • container: Specify the surrounding DOM node

Related information:

  • @testing-library/vue version: 3.0.0
  • Vue version: 2.5.18
  • node version: 8.12.0
  • yarn version: 1.17.3
  • Vuetify version: 1.2.10

Relevant code or config (if any)

import CreateLorem from "./CreateLorem";
import { render } from "@testing-library/vue";
import Vuetify from "vuetify";
import Vue from "vue";

var app = document.createElement("div");
app.setAttribute("data-app", true);
document.body.appendChild(app);
Vue.use(Vuetify);

it("should create a lorem", () => {
  const { getByText } = render(CreateLorem);
  getByText("open");
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
bennettdamscommented, Nov 13, 2019

The example above works only for Vuetify components that don’t use the $vuetify instance property

(e. g. <v-menu> will always throw an undefined error in the test).

You can bypass this by using { vuetify: new Vuetify(), ...options },.

For any future readers, here is the complete setup:

Maybe interesting for the documentation/examples?

test-setup.js executed via Jest’s setupFiles in the jest.config.js

import Vue from "vue";
import Vuetify from "vuetify";

// We need to use a global Vue instance, otherwise Vuetify will complain about
// read-only attributes.
// More info: https://github.com/vuetifyjs/vuetify/issues/4068
//            https://vuetifyjs.com/en/getting-started/unit-testing
Vue.use(Vuetify);

test-helper.js used in the tests instead of VTL’s render

import { render as VTLRender } from "@testing-library/vue";
import Vuetify from "vuetify";

/**
 * Custom render wrapper to integrate Vuetify with Vue Testing Library.
 *
 * Vuetify requires you to wrap your app with a `v-app` component that provides
 * a `<div data-app="true">` node.
 *
 * More info:
 *    https://github.com/vuetifyjs/vuetify/issues/4068
 *    https://vuetifyjs.com/en/getting-started/unit-testing
 *
 * @param {*}           component Component from test to render
 * @param {*}           options Render options
 * @param {() => void}  callback Render callback
 * @return {*}          Render function of VTL including Vuetify `data-app`
 */
export const renderWithVuetify = (component, options, callback) => {
  return VTLRender(
    // anonymous component
    {
      // Vue's render function
      render(createElement) {
        // wrap the component with a <div data-app="true"> node and render the test component
        return createElement("div", { attrs: { "data-app": true } }, [
          createElement(component)
        ]);
      }
    },
    { vuetify: new Vuetify(), ...options },
    callback
  );
};

Actual component test

import { renderWithVuetify } from "../test-helper";
import MyComponent from "../../src/views/MyComponent.vue";

describe("MyComponent.vue", () => {
  test("Some test...", () => {
    const { getByText } = renderWithVuetify(MyComponent);

    expect(getByText(/some text/));
  });
});

1reaction
afontcucommented, Nov 13, 2019

Hi! I can’t think of any downsides of the top of my head, as long as Vuetify issue with multiple Vue instances is still a thing (I guess it is!). This is something that should be fixed on their own, so I’d say having a comfortable workaround should do the trick 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Adaptation Principles: 6 Ways to Build Resilience to ...
The report “The Adaptation Principles: A Guide for Designing Strategies for Climate Change Adaptation and Resilience” lays out 6 principles ...
Read more >
Climate Change 2022: Impacts, Adaptation and Vulnerability
Chapter 3 provides a global assessment of climate change impacts and risks to ocean and coastal ecosystems and their services as well as...
Read more >
Adaptation Strategies: ERIT - Environmental Resilience Institute
Area of Interest Adaptation Strategy Adaptatio... Ecosystem Protection Preserve Habitat Retreat fr... Stormwater Management and Water Quality Apply Green Infrastructure Strategies Stormwater Management and Water Quality...
Read more >
API Reference - Vue.js
API Reference · Global API · Composition API · Options API · Built-ins · Single-File Component · Advanced APIs ...
Read more >
Glossary of climate change acronyms and terms - UNFCCC
The Adaptation Committee was established by the Conference of the Parties as ... Pre-session documents are available before a meeting, often in all...
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