0

I'm lost I read documentation to be able to add fake store in shallowMount() function.

In the official doc : https://vue-test-utils.vuejs.org/guides/using-with-vuex.html, So I implement this sample

import { createLocalVue, shallowMount } from "@vue/test-utils";
import ProjectItem from "@/components/ProjectItem.vue";
import { state } from "@/data/fakeStore/projects";
import Vuex from "vuex";

describe("ProjectItem.vue", () => {
  const localVue = createLocalVue();
  localVue.use(Vuex);

  const fakeStore = new Vuex.Store({
    state: state,
    getters: {
      projects: jest.fn(),
      skills: jest.fn(),
      skillsFromOneProject: jest.fn()
    }
  });

  const wrapper = shallowMount(ProjectItem, {
    props: {
      index: 0
    },
    fakeStore,
    localVue
  });

  it("check initialization data", () => {
    expect(wrapper.vm.projects).toEqual(state.projects);
    expect(wrapper.vm.skills).toEqual(state.skills);
    expect(wrapper.vm.index).toEqual(0);
  });
});

But I can't use createLocalVue because I got

Module '"@vue/test-utils"' has no exported member 'createLocalVue'.

Moreover at the line shallowMount(ProjectItem,

I got the following error

No overload matches this call. The last overload gave the following error. Argument of type 'DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, ... 4 more ..., {}>' is not assignable to parameter of type 'ComponentOptionsWithObjectProps<Readonly<ComponentPropsOptions>, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ... 4 more ..., { ...; } | {}>'. Type 'DefineComponent<{}, {}, any, ComputedOptions, MethodOptions, ComponentOptionsMixin, ComponentOptionsMixin, ... 4 more ..., {}>' is not assignable to type 'ComponentOptionsBase<Readonly<(readonly unknown[] & { [x: number]: string; } & { [iterator]?: IterableIterator | undefined; length?: number | undefined; concat?: string[] | undefined; join?: string | undefined; ... 19 more ...; toLocaleString?: string | undefined; }) | ({ ...; } & ... 1 more ... & { ...; })>...'. Types of property 'setup' are incompatible. Type '((this: void, props: Readonly<LooseRequired<Readonly<{} & {} & {}>>>, ctx: SetupContext<{}>) => void | {} | RenderFunction | Promise<...>) | undefined' is not assignable to type '((this: void, props: Readonly<LooseRequired<(Readonly<(readonly unknown[] & { [x: number]: string; } & { [iterator]?: IterableIterator | undefined; length?: number | undefined; concat?: string[] | undefined; ... 20 more ...; toLocaleString?: string | undefined; }) | ({ ...; } & ... 1 more ... & { ...; })> & ...'. Type '(this: void, props: Readonly<LooseRequired<Readonly<{} & {} & {}>>>, ctx: SetupContext<{}>) => void | {} | RenderFunction | Promise<...>' is not assignable to type '(this: void, props: Readonly<LooseRequired<(Readonly<(readonly unknown[] & { [x: number]: string; } & { [iterator]?: IterableIterator | undefined; length?: number | undefined; concat?: string[] | undefined; ... 20 more ...; toLocaleString?: string | undefined; }) | ({ ...; } & ... 1 more ... & { ...; })> & {...'. Types of parameters 'ctx' and 'ctx' are incompatible. Type 'SetupContext<string[]>' is not assignable to type 'SetupContext<{}>'. Type '{}' is missing the following properties from type 'string[]': length, pop, push, concat, and 28 more.ts(2769) mount.d.ts(22, 25): The last overload is declared here.

So I decided to try another thing, thank's to https://vue-test-utils.vuejs.org/api/options.html#context I saw mocks option, so I replace wrapper by the following code

const wrapper = shallowMount(ProjectItem, {
    props: {
      index: 0
    },
    mocks: {
      $store: fakeStore
    }
});

I still have the error No overload matches this call ... as before.

package.json

"dependencies": {
    "axios": "^0.21.4",
    "core-js": "^3.6.5",
    "pixi.js": "^6.1.3",
    "v-smooth-scroll": "^2.0.0-beta.1",
    "vue": "^3.0.0",
    "vue-class-component": "^8.0.0-0",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-0"
  },
  "devDependencies": {
    "@types/jest": "^24.0.19",
    "@typescript-eslint/eslint-plugin": "^4.18.0",
    "@typescript-eslint/parser": "^4.18.0",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-typescript": "~4.5.0",
    "@vue/cli-plugin-unit-jest": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "@vue/eslint-config-airbnb": "^5.0.2",
    "@vue/eslint-config-typescript": "^7.0.0",
    "@vue/test-utils": "^2.0.0-0",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-vue": "^7.0.0",
    "node-sass": "^4.12.0",
    "sass-loader": "^8.0.2",
    "typescript": "~4.1.5",
    "vue-jest": "^5.0.0-0"
  }

Can you show me unit test sample that add store to shallowMount() ?

UPDATE

Store code used in the component :

import { Project } from "@/domain/store/Project";
import { ProjectsGetters } from "@/store/projects/getters";
import { Options, Vue } from "vue-class-component";
import { useStore } from "vuex";

@Options({
  props: {
    index: String
  }
})
export default class ProjectItem extends Vue {
  store = useStore();
  index!: string;
  projects: Array<Project> = this.store.getters[ProjectsGetters.projects];
  skills: string = this.store.getters[ProjectsGetters.skillsFromOneProject](
    this.index
  );

  projectOver(index: number) {
    this.projects[index].classname = "line-right";
  }

  projectLeave(index: number) {
    this.projects[index].classname = "line-left";
  }
}
0

1 Answer 1

0

SOLUTION

I used documentation for vuejs 2, so the documentation for vuejs 3 is here : https://next.vue-test-utils.vuejs.org/guide/advanced/vuex.html#testing-with-a-real-vuex-store

Here is my test

import { shallowMount } from "@vue/test-utils";
import ProjectItem from "@/components/ProjectItem.vue";
import { projects } from "@/store/projects";
import { state } from "@/data/fakeStore/projects";
import Vuex from "vuex";

describe("ProjectItem.vue", () => {
  const store = new Vuex.Store({
    modules: {
      projects: {
        namespaced: true,
        state,
        getters: projects.getters
      }
    }
  });

  const wrapper = shallowMount(ProjectItem, {
    props: {
      index: 0
    },
    global: {
      plugins: [store]
    }
  });

  wrapper.vm.projects = state.projects;
  wrapper.vm.skills = state.skills;

  it("check initialization data", () => {
    expect(wrapper.vm.projects).toEqual(state.projects);
    expect(wrapper.vm.skills).toEqual(state.skills);
    expect(wrapper.vm.index).toEqual(0);
  });
});
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.