0

I've seen loads of posts about the same error but I'm only getting the it when running tests with Jest:

    console.error node_modules/vue/dist/vue.runtime.common.dev.js:621
      [Vue warn]: Failed to mount component: template or render function not defined.

      found in

      ---> <Navbar>
             <Root>

Navbar.vue

<template>
...
  <div>
    <Image
      v-if="email"
    />
  </div>
</template>
<script>
module.exports = require("./Navbar.ts");
</script>

Navbar.ts

import Vue from "vue";
import Image from "@/components/Image/Image";

export default Vue.extend({
  name: "Navbar",
  components: {
    Image,
  },
  computed: {
    email() {
      return this.$store.getters.email;
    }
  }
});

Navbar.spec.ts example test

import { mount, createLocalVue } from "@vue/test-utils";
import Navbar from "../Navbar";
import Vuex from "vuex";

const localVue = createLocalVue();
localVue.use(Vuex);
let wrapper;
const state = {
  email: "[email protected]"
};
const getters = {
  email: state => state.email
};
const store = new Vuex.Store({ state, getters });

describe("Navbar", () => {
  beforeEach(() => {
    wrapper = mount(Navbar, {
      store,
      localVue
    });
  });

  it("returns correct email from the store", () => {
    expect(wrapper.vm.email).toBe("[email protected]");
  });
});

Tests itself pass. I assume I'm missing/set wrong some configuration but not sure exactly what. I'm also using Typescript.

Jest configuration:

module.exports = {
  displayName: "test",
  roots: ["<rootDir>/src"],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node", "vue"],
  transform: {
    ".*\\.(vue)$": "vue-jest",
    "^.+\\.tsx?$": "ts-jest"
  },
  testURL: "http://localhost/",
  snapshotSerializers: ["<rootDir>/node_modules/jest-serializer-vue"],
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1"
  }
};

I expect tests to still pass without the console error.

1 Answer 1

2

I resolved the warning by modifying the order of the extensions in moduleFileExtensions to:

moduleFileExtensions: ["js", "jsx", "json", "vue", "ts", "tsx", "node"],

I also needed to use shallowMount instead of mount as I have Image child component that is committing to the Vuex store which I don't want to be executed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.