vuejs / core

🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
https://vuejs.org/
MIT License
47.6k stars 8.32k forks source link

Importing types doesn't work in jest environment #8301

Open vojvodics opened 1 year ago

vojvodics commented 1 year ago

Vue version

3.3.2

Link to minimal reproduction

https://github.com/vojvodics/vue3.3.1-jest-reproduction-example

Steps to reproduce

Clone reproduction repository and run npm run test

What is expected?

Tests should pass

What is actually happening?

Tests are failing with the following error:

    [@vue/compiler-sfc] No fs option provided to `compileScript` in non-Node environment. File system access is required for resolving imported types.

System Info

No response

Any additional comments?

A hacky solution is provided in the repo example. It is based on these files: https://github.com/vuejs/core/blob/v3.3.2/packages/vue/compiler-sfc/index.js https://github.com/vuejs/core/blob/v3.3.2/packages/vue/compiler-sfc/register-ts.js

Also related:

*initially it was v3.3.1, but I updated it to v3.3.2 and it is still present

yyx990803 commented 1 year ago

This is something that needs to be fixed in https://github.com/vuejs/vue-jest I believe. /cc @lmiller1990

It seems TypeScript does not populate ts.sys when loaded in Jest - I'm not sure if passing the Jest-mocked fs into compileScript would resolve this.

franklin-tina commented 1 year ago

I recently ran into the same issue, although I'm not using jest. Will put up a simple repro and link here Discussion on discord

mrc-bsllt commented 1 year ago

I have the same problem with version 3.2.2 and without jest. Any news?

franklin-tina commented 1 year ago

I have the same problem with version 3.2.2 and without jest. Any news?

I think this feature is available only from v3.3.x See here

mrc-bsllt commented 1 year ago

I have the same problem with version 3.2.2 and without jest. Any news?

I think this feature is available only from v3.3.x See here

My mistake, i'm using version 3.3.1

jenshoffmann1331 commented 1 year ago

Hi there! Had anyone of you time to investigate? 🙂 Using the new cool vue 3.3 interface features (that is importing interfaces from elsewhere) our jest suite is now broken too 🥲 Would really like to step forward with vue 3.3 🥳.

vojvodics commented 1 year ago

@jenshoffmann1331 Did you try the hacky solution? You basically point to a different transformer than vue-jest (https://github.com/vojvodics/vue3.3.1-jest-reproduction-example/blob/main/jest.config.js#L6) and in that file you call registerTs before exporting vue-jest (https://github.com/vojvodics/vue3.3.1-jest-reproduction-example/blob/main/vue3JestHack.js)

jenshoffmann1331 commented 1 year ago

@vojvodics: Yeah! I tried your hack while you were writing your comment. Works well, thank you!

lmiller1990 commented 1 year ago

I don't think we've tried vue-jest with Vue 3.3 yet. We should add some tests in that code base and see if there's any bugs or issues.

It sounds like this isn't an issue in Vue core, though - perhaps we should move the issue to vue-jest?

person1123 commented 1 year ago

Looks like https://github.com/vuejs/core/commit/d2c3d8b70b2df6e16f053a7ac58e6b04e7b2078f broke the hack suggested here. To fix it, instead of passing in require('typescript') to registerTS(), you have to pass a function that lazily resolves to require('typescript'):

require("@vue/compiler-sfc").registerTS(() => require("typescript"));
module.exports = require("@vue/vue3-jest");