Open JCQuintas opened 1 week ago
// test file vi.mock('date-fns/addHours', ()=>({addHours})) vi.mock('date-fns/parse', ()=>({parse})) /// SyntaxError: The requested module '/node_modules/.vite/deps/date-fns_parse.js?v=16a839a0' does not provide an export named 'parse'
This mocking error looks like a bug and not really related to npm:...
alias dependency. Based on your reproduction, I made something simpler just installing "date-fns": "2.30.0"
and the error is same https://github.com/hi-ogawa/reproductions/tree/main/vitest-6483-browser-mock-date-fns
What looks odd to me is I see these two requests on devtools and probably the 2nd one again exporting addHours
is wrong:
// http://localhost:5173/node_modules/.vite/deps/date-fns_addHours.js?t=1726706677742&v=fa62991a
const module = globalThis["__vitest_mocker__"].getFactoryModule("/node_modules/.vite/deps/date-fns_addHours.js");
export const addHours = module["addHours"];
// http://localhost:5173/node_modules/.vite/deps/date-fns_parse.js?t=1726706677742&v=fa62991a
const module = globalThis["__vitest_mocker__"].getFactoryModule("/node_modules/.vite/deps/date-fns_parse.js");
export const addHours = module["addHours"];
Describe the bug
Hello, we are trying to understand what would take to migrate into vitest on our repositories at MUI, both for browser and jsdom testing.
And we encountered an issue in regard to aliased dependencies.
We currently have the following in our package
This is due to the different signatures between these two versions. In the final code, this is a peer dependency, so inside our code we only always use
import from 'date-fns'
.But when testing, we want to test the actual library, which requires some aliasing.
I've tried with both
config.alias
andvi.mock
and they both work on jsdom, but not on browser.Reproduction
https://github.com/JCQuintas/vitest-browser-alias-mock
Important Files
Ensure you uncomment one of the comments in these files to test
Running
System Info
Used Package Manager
pnpm
Validations