vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.57k stars 1.12k forks source link

vitest with mount from @vue/test-utils SyntaxError (0.20.3) #1793

Closed michaelmano closed 2 years ago

michaelmano commented 2 years ago

Describe the bug

Vitest version 0.19.1 is fine with this, However after upgrading to 0.20.3 when mount from "@vue/test-utils": "^2.0.2", causes the following error

 FAIL  src/components/HelloWorld.test.ts [ src/components/HelloWorld.test.ts ]
SyntaxError: Named export 'BaseTransition' not found. The requested module 'vue' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'vue';
const { transformVNodeArgs, Transition, BaseTransition, defineComponent, h, TransitionGroup, Teleport, nextTick, setDevtoolsHook, shallowReactive, reactive, isRef, createApp, computed } = pkg;

Reproduction

Create a new vite project, install viteest 0.20.3 and @vue/test-utils any version.

Then create a new test file with the following

import { mount } from '@vue/test-utils';
import {
  expect, describe, it,
} from 'vitest';
import HelloWorld from './HelloWorld.vue';

describe('HelloWorld.vue', () => {
  it('should exist', async () => {
    expect(HelloWorld).toBeTruthy();
  });

  it('should display header text', async () => {
    const msg = 'HelloWorld';
    const wrapper = mount(HelloWorld, {
      props: {
        msg,
      },
    });
    expect(wrapper.find('h1').text()).toEqual(msg);
  });
});

This will cause the error.

Revert vitest to 0.19.1 and its working as expected.

System Info

System:
    OS: macOS 11.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 977.92 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Firefox: 91.0.2
    Safari: 14.1.1
  npmPackages:
    @vitejs/plugin-vue: ^3.0.0 => 3.0.1
    vite: ^3.0.0 => 3.0.4
    vitest: ^0.19.1 => 0.19.1

Used Package Manager

npm

Validations

sheremet-va commented 2 years ago

Duplicate of https://github.com/vitest-dev/vitest/issues/1753 See workaround there.