Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: ShadowRoot is not defined Jest and Vue3

I'm setting up Vue3 and Unit Test with Jest.

I already had to fight to make it work, and I already tried a lot of different configurations.

This is my last "working" configuration, and with this I get this error:

ReferenceError: ShadowRoot is not defined

I have actually no more ideas.

My configuration:

jest.config.js

    module.exports = {
        transform: {
            "^.+\\.vue$": "vue-jest",
            "^.+\\.js$": "babel-jest",
            '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)(\\?inline)?$': 'jest-transform-stub',
        },
        moduleFileExtensions: ['vue', 'js', 'json', 'node'],
        transformIgnorePatterns: [
            "node_modules/(?!@jamescoyle/vue-icon)"
        ],
        moduleNameMapper: {
            "@/(.*)$": "<rootDir>/src/$1"
        }
    }

header.test.js


    import { mount } from '@vue/test-utils'
    import Header from "./Header.vue";
    
    test('Header', () => {
        const wrapper = mount(Header)
        console.log(wrapper.html())
    })

The complete error:


    > vue-cli-service test:unit
    
     FAIL  src/views/components/layout/Header/Header.test.js
      ✕ Header (11ms)
    
      ● Header
    
        ReferenceError: ShadowRoot is not defined
    
          3 |
          4 | test('Header', () => {
        > 5 |     const wrapper = mount(Header)
            |                     ^
          6 |     
          7 |     console.log(wrapper.html())
          8 | })
    
          at normalizeContainer (node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:1260:30)
          at Object.app.mount (node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:1211:27)
          at mount (node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:2288:18)
          at Object.<anonymous> (src/views/components/layout/Header/Header.test.js:5:21)
    
    Test Suites: 1 failed, 1 total
    Tests:       1 failed, 1 total
    Snapshots:   0 total
    Time:        1.664s
    Ran all test suites.

like image 448
Danny_DD Avatar asked Nov 19 '25 07:11

Danny_DD


1 Answers

It looks like the Vue team are aware of this and a code fix is already merged in.

References

https://github.com/vuejs/vue-test-utils-next/issues/293

https://github.com/vuejs/vue-next/pull/2943

Vue 3.0.6 when it is released should resolve this.

like image 106
newton101 Avatar answered Nov 21 '25 21:11

newton101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!