Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data not defined on instance in unit test Vue 3 #524

Open
Evertvdw opened this issue Apr 9, 2021 · 4 comments
Open

Data not defined on instance in unit test Vue 3 #524

Evertvdw opened this issue Apr 9, 2021 · 4 comments

Comments

@Evertvdw
Copy link

Evertvdw commented Apr 9, 2021

Hi there!

I started with migrating our codebase from Vue 2 with class components to Vue 3. The project doesn't run yet with Vue 3 but I'm already looking at our unit tests to see if I can get a setup with jest and vue-test-utils working. I'm running in a strange issue however, not sure how to proceed.

I have the following minimal component:

<template>
	<div>
		{{ text }}
	</div>
</template>

<script lang="ts">
import { Vue } from "vue-property-decorator";

export default class DefaultComponent extends Vue {
	text = "Hello";
}
</script>

And I created a unit test for that (file DefaultComponent.spec.ts):

import DefaultComponent from "../DefaultComponent.vue";
import { createWrapper } from "utils/wrapper";

describe("DefaultComponent", () => {
	it("dummy test as there are no reasonable things to unit test", async () => {
		console.log(DefaultComponent);
		const wrapper = await createWrapper(DefaultComponent, {});
		expect(wrapper).toBeDefined();
                console.log(wrapper.html());
	});
});

The createWrapper is a custom function in which I do some general setup and eventually do a return mount(DefaultComponent, {...options}). I have the following packages installed:

  • vue@3.0.11
  • jest@26.6.3
  • ts-jest@26.5.4
  • vue-class-component@8.0.0-rc.1
  • vue-jest@5.0.0-alpha.8
  • @vue/test-utils@2.0.0-rc.4

When I run this test it gives the following output:

// first console.log
{ render: [Function: render] }

[Vue warn]: Property "text" was accessed during render but is not defined on instance. 
  at <Anonymous ref="VTU_COMPONENT" > 
  at <VTUROOT>

// html()
<div></div>

Anyone have any idea why this is happening? If anyone has unit tests with class components working I would very much love some examples, because my search so far is turning up empty.

Thanks!

@Evertvdw
Copy link
Author

Ok, nobody does unit tests in combination with class components?

I also ran into another issue with Vue-class-components in another library. Maybe that is related? quasarframework/quasar#8915 (comment)

Before with class components the options were besides the render method on the class. But now they are under a subkey __c, maybe this trips up unit tests also. Anyone know why this is different going from Vue 2 -> Vue 3?

@Evertvdw
Copy link
Author

Evertvdw commented Apr 21, 2021

Ok, it seems importing Vue from vue-class-component instead of vue-property-decorator fixes this issue. However when you have a component which extends from a mixin, you get this issue again. Even if that mixin also imports Vue from vue class components.

@tuchuants
Copy link

I got similar issue when I try to write unit tests for my class-style code using vue-test-utils. You would find that when you use Vue.with() instead of Vue as the parent class the problem appears again.

@Evertvdw
Copy link
Author

Evertvdw commented May 3, 2021

Created an issue on vue-test-utils about this: vuejs/test-utils#544 (comment)

Although the issue seems to be originating from vue-jest :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants