sodatea / vite-jest

First-class Vite integration for Jest
MIT License
401 stars 51 forks source link

Cannot find module '../node_modules/.vite/@vue_test-utils.js' from 'src/App.spec.ts' #18

Closed daviddesmet closed 2 years ago

daviddesmet commented 2 years ago

First of all, thanks a lot for your hard work on supporting jest on vite.

When I first setup the project with vite-jest, my tests were working again, felt like magic, however, I noticed jest wasn't exiting and instead giving me this message:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Since this issue was getting the commits stuck due to a git hook that runs unit tests, I decided to investigate further in order to solve it.

I gave it a try with --detectOpenHandles but didn't got any additional output. I even tried passing --forceExit with no luck.

I excluded some tests and even wrote a simple one and still got the same issue.

After a lot of trial and error... I ended removing the node_modules directory and installed all packages all over again and now the unit tests are failing with the above-mentioned message.

This is my test file:

// App.spec.ts
import { mount, shallowMount } from "@vue/test-utils";
import App from "./App.vue";
import router from "./router";

test("uses mounts", () => {
  const wrapper = mount(App, {
    global: {
      plugins: [router]
    }
  });
  expect(wrapper.html()).toContain("Home");
  expect(wrapper.html()).not.toContain("Hello world");
});

test("uses shallowMount", () => {
  const wrapper = shallowMount(App, {
    global: {
      plugins: [router]
    }
  });
  expect(wrapper.html()).toContain("Home");
  expect(wrapper.html()).not.toContain("Hello world");
});

Any hints on what is causing it and how I can make it work again? And also fix the issue about jest not exiting.

mason-smith commented 2 years ago

I am encountering the same issue using react-app-ts example on Windows, running with npm

bitsmakerde commented 2 years ago

I have the same with a react and typescript app with vice 2.6.3

imomaliev commented 2 years ago
  1. This issue probably occurs because jest cannot resolve @/ path.
  2. Update jest.config.js to fix this.
    +  moduleNameMapper: {
    +    '^@/(.*)$': '<rootDir>/src/$1',
    +  },

I have written more about this in my article https://dev.to/imomaliev/creating-vite-vue-ts-template-setup-jest-5h1i

sodatea commented 2 years ago

I think this issue is fixed in v0.1.4 now. The issue existed because previously there was no reliable way to start the test till Vite optimizes all the dependencies, therefore tests may start too early with some dependencies still compiling. It's no longer an issue in the latest version.