Skip to content

[Bug]: jsdom breaks module mock #1064

@escaton

Description

@escaton

Version

System:
    OS: macOS 15.7.2
    CPU: (14) arm64 Apple M4 Pro
    Memory: 136.38 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 145.0.7632.160
    Firefox: 147.0.1
    Safari: 26.1
  npmPackages:
    @rstest/adapter-rsbuild: ^0.2.2 => 0.2.2 
    @rstest/core: ^0.9.2 => 0.9.2

Details

I tried to migrate from vite in my project, and found a strange issue with only one specific dependency vue-router@4.5.1. When I try to mock it, it seems that something is off with module resolution. I found that the bug appears only when I set testEnvironment to jsdom. Fixed by either disabling jsdom, OR changing the specifier on the mock decalration.

Reproduce link

Reproduce Steps

reproduces without a config file rstest --include test.ts --testEnvironment jsdom:

import {rs} from '@rstest/core'
import * as vueRouterActual from 'vue-router' with { rstest: 'importActual' }
import {
    useRoute,
} from 'vue-router'


rs.mock('vue-router', () => {
    return {
        useRoute: rs.fn(),
    }
})

console.log(vueRouterActual.useRoute === useRoute) // true, but expected false

If I change the import specifier to vue-router/dist/vue-router it works!

import {rs} from '@rstest/core'
import * as vueRouterActual from 'vue-router/dist/vue-router' with { rstest: 'importActual' }
import {
    useRoute,
} from 'vue-router'


rs.mock('vue-router/dist/vue-router', () => {
    return {
        useRoute: rs.fn(() => mockedRoute),
    }
})

console.log(vueRouterActual.useRoute === useRoute) // false as expected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions