-
Notifications
You must be signed in to change notification settings - Fork 344
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
Failed to mount when returning render function from setup() while testing with Jest and vue-jest #151
Comments
I just posted a question on stackoverflow for exacltly this. Here's a copy paste version of itI am trying to test a jsx component by passing props to it and asserting that it renders them correctly. This fails though for a reason I cannot understand. Here's the component // components/toast.tsx
import { createComponent } from "@vue/composition-api";
import style from "./toast.module.scss";
export default createComponent({
name: "Toast",
props: {
message: String
},
setup(props) {
return () => (
<div class={style.toast}>
<p>{props.message}</p>
</div>
);
}
}); Here's the snippet of the unit test that is failing: // tests/unit/components/toast.spec.ts
import Toast from "@/components/toast";
import { shallowMount } from "@vue/test-utils";
// ... a describe block
it("should render props correctly", () => {
const msg = "hello";
const wrapper = shallowMount(Toast, {
propsData: {
message: msg
}
});
expect(wrapper.isVueInstance()).toBeTruthy();
expect(wrapper.text()).toMatch(msg);
}); And the output from the failing test is here:
Everything works! except for this one part. If I use a single file component ( |
I have the same problem on using tsx with composition api in jest and if I log the wrapper html it is just an empty string while a |
I've gone back to using |
I was able to use this plugin with Vue Test Utils fine - I suspect it's related to tsx. Simple demo of VTU + composition API here. |
@lmiller1990 The render function not playing nicely. Which also suggests that this issue should be closed and opened somewhere else |
Does this render in a browser? Or does the error only happen in testing? Could be related to Jest not knowing how to read TSX. |
It only happens in tests. That also makes sense. I tried this with the simplest HelloWorld component. Maybe my project might not be configured corectly. I wish there was a reference sample should this be a non issue |
I have had the problem before too. Did you make your project with Vue cli? Last time I did TS + TSX, I chose "include babel alonside TypeScript" option, and installed Vue TSX support with Anyway I think this is not a problem with this repo... try the above, you can open an issue in Vue Test Utils if you are still stuck and I can try to help there. |
Update: I managed to run tests using configurations like so, the component uses jsx
|
I tried vue-tsx-support before, jest is worked as expected but composition api is not. |
Any solution found? :/ |
Can someone post a repo that reproduces the problem? |
I've attempted this yesterday, Here's a little example. I did this is a rush because I'm still at work but I hope it provides some context. Here's my test it("renders message", () => {
const localVue = createLocalVue();
localVue.use(VueCompositionApi);
const msg = "Hello, world";
const wrapper = shallowMount(HelloWorld, { localVue });
expect(wrapper.isVueInstance()).toBe(true);
expect(wrapper.vm.$el.textContent).toMatch(msg);
}); Here's what passes export default createComponent({
name: "HelloWorld",
props: {
msg: String
},
setup() {},
render() {
return <div>Hello, world</div>;
}
}) as VueConstructor; Here's what doesn't export default createComponent({
name: "HelloWorld",
props: {
msg: String
},
setup() {
return () => <div>Hello, world</div>;
}
}) as VueConstructor; NB: I'm using TSX here, |
I mean, can you post an entire repository someone can pull down a run? There are so many plugins in these examples (composition api, jsx, typescript, jest vue transformer, babel) it's highly likely this is a configuration problem. |
I hope this helps https://github.com/sduduzog/vue-composition-api-tsx-example |
I'm also having this issue in a repo using TypeScript, but without TSX. Here’s a summary of the setup function throwing the warning in Jest: setup() {
return () => createElement('div', slots.default());
}, Let me know if you want me to setup another reproduction repo. |
I've investigated a bit and I found some stuff but not really the origin of the problem. Line 172 in 3b7eadf
In the In the browser it properly returns the component setup Function: function () {
return Object(_vue_composition_api__WEBPACK_IMPORTED_MODULE_0__["createElement"])('div', slots["default"] && slots["default"]());
} While being rendered by Vue Test Utils, binding value is an Object: { length: 0, name: '' } This result in the following block being ignored and therefor the render method assignment skipped: Lines 176 to 185 in 3b7eadf
I'm not sure where to look at from here… |
@sduduzog I cannot install your repo; I get
It doesn't look like returning JSX from Has anyone else got that reproduction repo to run? Maybe it's a problem on my end. |
@lmiller1990 I was able to make the repo works with npm, I can setup a simplier reproduction without JSX if you like, since it's probably not the problem here. |
Sorry @lmiller1990 please delete the yarn lock file or package.json-lock file then rebuild. I have a private npm registry that we use at work |
any update? |
Yes, read more details here: vuejs/vue-test-utils#1484. Basically it's a conflict between VTU and composition API, both override the
Here is the start of a fix vuejs/vue-test-utils#1512 however this makes any tests calling If you are working on a small/medium app, or just trying out the composition API and liked it, you can probably upgrade to Vue 3 and VTU next fairly easily to have everything work as expected. Plus all the goodness of Vue 3 like Suspense, Portal etc. I am not sure if JSX works for Vue 3 yet, though. |
Code snippet:
which returns the following error
I'm currently using jest 24.9.0, @vue/test-utils and vue-jest 3.0.5. Other tests work fine with templated SFCs using composition API
The text was updated successfully, but these errors were encountered: