question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mock module does not work when imported in vue sfc files in typescript

See original GitHub issue

Describe the bug

When importing the library plyr inside a Vue 3 SFC and mock it inside a test file, it will not be mocked. Instead the actual library is used inside the vue file.

Reproduction

Considering having a component as follows

<template>
  <div></div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import Plyr from 'plyr';

export default defineComponent({
  name: 'TestComponent',
  setup() {
    console.log(Plyr);
    return {};
  },
});
</script>

within the test the plyr library is mocked:

import { mount } from '@vue/test-utils';
import { vi } from 'vitest';
import TestComponent from './TestComponent.vue';

const onFunction = vi.fn();

vi.mock('plyr', () => {
  const plyrLib = vi.fn(() => ({
    on: onFunction,
  }));

  return {
    default: plyrLib,
  };
});

describe('TestComponent', () => {
  it('should register events', () => {
    mount(TestComponent);
    expect(onFunction).toHaveBeenCalled();
  });
});

When running the test, the actual Plyr class will be logged instead of the mocked library.

System Info

System:
    OS: macOS 12.4
    CPU: (10) x64 Apple M1 Pro
    Memory: 176.62 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
    npm: 6.14.10 - ~/.nvm/versions/node/v14.15.4/bin/npm
  Browsers:
    Chrome: 102.0.5005.115
    Chrome Canary: 105.0.5118.3
    Firefox: 101.0
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-legacy: ^1.8.1 => 1.8.2 
    @vitejs/plugin-vue: ^2.3.3 => 2.3.3 
    vite: ^2.9.12 => 2.9.12 
    vitest: ^0.14.2 => 0.14.2

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sheremet-vacommented, Jun 28, 2022

Yeah, the fix would only work if you were doing vi.mock inside a Vue file. You need to wait for https://github.com/vitejs/vite/issues/8659 to be fixed

1reaction
sheremet-vacommented, Jun 19, 2022

There is a bug/inconsistency in Vite (https://github.com/vitejs/vite/issues/8659), and because of that we resolved paths differently, when processing vi.mock and import.

This change should fix that on Vitest side: https://github.com/vitest-dev/vitest/pull/1506

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking a module imported in a vue component with Jest
It looks like there is an issue in with jest.mock (#4262) concerning moduleNameMapper for module resolvers, aliases, path, whatever you want ...
Read more >
cannot use import statement outside a module jest vue
Itried a lot of soltions, but still not working Here is my jest.config.js file: module.exports = { moduleFileExtensions: ['js', 'jsx', 'json', 'vue'], ...
Read more >
JavaScript/ES Modules and Imports - CodePen Blog
The Vue SFC Editor (Single File Component) not only supports the usage of ES Modules within the JavaScript <script> area, CodePen processes your...
Read more >
How To Create Reusable Blocks of Code with Vue Single-File ...
After following the Prerequisites section, you will have a new Vue project named sfc-project . In this section, you will import data into ......
Read more >
How We Refreshed Our Vue 2 JavaScript Codebase ... - Medium
Consequently, Vue SFC will have the JavaScript rules applied and the TypeScript linting will be applied to TypeScript files only.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found