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

Mock content in browser mode contains wrong exports #6582

Closed
6 tasks done
Smrtnyk opened this issue Sep 26, 2024 · 2 comments
Closed
6 tasks done

Mock content in browser mode contains wrong exports #6582

Smrtnyk opened this issue Sep 26, 2024 · 2 comments

Comments

@Smrtnyk
Copy link

Smrtnyk commented Sep 26, 2024

Describe the bug

For some reason mocked module in browser mode returns wrong exports

Reproduction

I am trying to mock some modules in browser mode
like this

import type { Ref } from "vue";
import type { EventDoc, PlannedReservationDoc, ReservationDoc, User } from "@firetable/types";
import type { EventOwner } from "@firetable/backend";
import { installPinia } from "../../test-helpers/install-pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ReservationStatus, ReservationType } from "@firetable/types";
import { ref, shallowRef, toRef } from "vue";
import { FloorViewer, getTables, RectTable } from "@firetable/floor-creator";

const { addReservationSpy, deleteReservationSpy } = vi.hoisted(() => {
    return {
        addReservationSpy: vi.fn(),
        deleteReservationSpy: vi.fn(),
    };
});

vi.mock("vue-i18n", () => {
    return {
        useI18n: () => ({
            t: vi.fn().mockReturnValue("test"),
        }),
    };
});
vi.mock("../helpers/ui-helpers.js", () => {
    return {
        showErrorMessage: vi.fn(),
        tryCatchLoadingWrapper: vi.fn().mockResolvedValue(undefined),
        showConfirm: vi.fn().mockResolvedValue(true),
    };
});
vi.mock("@firetable/backend", () => {
    return {
        addReservation: addReservationSpy,
        updateReservationDoc: deleteReservationSpy,
    };
});

and my tests fail with error
SyntaxError: The requested module '/node_modules/.vite/deps/vue-i18n.js?t=1727382327247&v=ec0aa5b1' does not provide an export named 'useI18n'
which is strange because I am providing it in the mock above, and even if mock wouldn't work real import does have it.
I have opened the test in headed mode in ui and inspected the network tab.
Request to http://localhost:5173/node_modules/.vite/deps/vue-i18n.js?t=1727382794575&v=ec0aa5b1
contains this content

const module = globalThis["__vitest_mocker__"].getFactoryModule("/node_modules/.vite/deps/vue-i18n.js");
export const showErrorMessage = module["showErrorMessage"];
export const tryCatchLoadingWrapper = module["tryCatchLoadingWrapper"];
export const showConfirm = module["showConfirm"];

which is the content of the mock for ui-helpers
I am kinda puzzled here if I am doing something wrong with mocking?

System Info

System:
    OS: Linux 6.9 Pop!_OS 22.04 LTS
    CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
    Memory: 43.28 GB / 62.69 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    npm: 10.8.3 - ~/.nvm/versions/node/v20.11.0/bin/npm
    pnpm: 9.11.0 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
  npmPackages:
    @vitejs/plugin-vue: 5.1.4 => 5.1.4 
    @vitest/browser: catalog: => 2.1.1 
    @vitest/spy: catalog: => 2.1.1 
    @vitest/ui: catalog: => 2.1.1 
    vitest: catalog: => 2.1.1

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

This looks like a duplicate of #6483 and fixed by #6530
Please wait for the next release.

@Smrtnyk
Copy link
Author

Smrtnyk commented Sep 27, 2024

tnx @hi-ogawa
Do you know if there is a release planned any time soon?

@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants