Open gsedlacz opened 2 years ago
Did you find a solution for this. I'm experiencing the same issue. Not using TS though.
<script>
export const someVar = 'something'; // this is where the error is coming from
</script>
<script setup>
...
</script>
I am experiencing this too (very similar reproduction to the repo linked in the initial report), but with @vue/vue2-jest@28.0.1
and vue@2.7.8
- adding lang="ts"
to non-TS components seems to help but (a) not technically correct as those components aren't typed and/or will fail type checks; and (b) not an option for some third-party components
For me the issue turned out to be babel config not being used correctly. After digging through the code I found a way to make it work using a somwhat-undocumented config option to provide a custom babel transformer:
Jest config:
module.exports = {
// ... whatever else ...
globals: {
'vue-jest': {
transform: {
js: './babel-jest-transformer.js',
},
},
},
transform: {
'.*\\.js$': './babel-jest-transformer.js',
'.*\\.vue$': '@vue/vue2-jest',
},
};
babel-jest-transformer.js:
const babelConfig = {
// ... whatever else ...
plugins: [
'@babel/plugin-transform-modules-commonjs',
],
};
module.exports = require('babel-jest').createTransformer(babelConfig);
For me the issue turned out to be babel config not being used correctly. After digging through the code I found a way to make it work using a somwhat-undocumented config option to provide a custom babel transformer:
Jest config:
module.exports = { // ... whatever else ... globals: { 'vue-jest': { transform: { js: './babel-jest-transformer.js', }, }, }, transform: { '.*\\.js$': './babel-jest-transformer.js', '.*\\.vue$': '@vue/vue2-jest', }, };
babel-jest-transformer.js:
const babelConfig = { // ... whatever else ... plugins: [ '@babel/plugin-transform-modules-commonjs', ], }; module.exports = require('babel-jest').createTransformer(babelConfig);
For me it worked perfectly!
Btw I got this problem when moved my configs out of the root directory. In case it helps to fix.
When importing a javascript based vue component inside of typescript vue component I receive the following error:
By making
TestWorld
use ts (<script lang="ts"> ...
) the test is able to work as expected.Example Project: https://github.com/gsedlacz/vue3-jest-issue-example
Is there any known Workaround? I already found these isues which unfortunately did not work for me: