app.appContext.config.globalProperties undefined in unit tests using jest, vue 3 and quasar v2


I have the very simple test below:

import { describe, expect, it } from "@jest/globals";
import { installQuasarPlugin } from "@quasar/quasar-app-extension-testing-unit-jest";
import { mount, shallowMount } from "@vue/test-utils";
import App from "../../../src/App.vue";

installQuasarPlugin();

describe("App", () => {
  it("sets the correct default data", () => {
    const wrapper = shallowMount(App);

    const { vm } = wrapper;

    expect(typeof vm.$q).toBe("object");
    expect(typeof vm.app).toBe("object");
    // expect(vm.counter).toBe(0);
  });
});

And in my App component:

export default {
  name: "App",
  data() {
    return {};
  },
  setup() {
    const app = getCurrentInstance();
    const $q = useQuasar();
    if (
      app.appContext.config.globalProperties.$securityUtils.isAuthenticated()
    ) {
      const configuracoes = JSON.parse(store.getters["auth/configuracoes"]);
      app.appContext.config.globalProperties.$axios.defaults.headers.common.Authorization = `${app.appContext.config.globalProperties.$securityUtils.getTipoToken()} ${app.appContext.config.globalProperties.$securityUtils.getToken()}`;
      $q.dark.set(
        configuracoes.TEMA ==
          app.appContext.config.globalProperties.$enums.theme.DARK
      );
    }
  }
};
</script>

Apparently jest can’t understand how globalProperties are getCurrentInstance and returns the following error to me:

TypeError: Cannot read property 'isAuthenticated' of undefined

      20 |     const $q = useQuasar();
      21 |     if (
    > 22 |       app.appContext.config.globalProperties.$securityUtils.isAuthenticated()

I am sorry for my english.

Source: Vue – Stack Overflow

November 29, 2021
Category : News
Tags: jestjs | quasar-framework | Vue.js | vuejs3 | vuex

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.