beeler
beeler2w ago

Basic Mocking example for `override global or imported objects` does not work

I am following the example https://docs.deno.com/examples/testing_tutorial/#basic-mocking and when run with deno test --no-check the example fails with:
getUserDetails formats the name correctly => ./service_test.ts:5:6
error: TypeError: Cannot assign to read only property 'getUserFromDB' of object '[object Module]'
db.getUserFromDB = async (id: string) => {
^
at file:///home/.../service_test.ts:10:19
getUserDetails formats the name correctly => ./service_test.ts:5:6
error: TypeError: Cannot assign to read only property 'getUserFromDB' of object '[object Module]'
db.getUserFromDB = async (id: string) => {
^
at file:///home/.../service_test.ts:10:19
I'm running with --no-checks to prove its not only a type issue, but the type checker does catch it if you remove the --no-check flag. Is there anyway to get this example to work or another way to isolate imports in Deno? I am using the latest Deno:
$ deno --version
deno 2.2.10 (stable, release, x86_64-unknown-linux-gnu)
v8 13.5.212.10-rusty
typescript 5.7.3
$ deno --version
deno 2.2.10 (stable, release, x86_64-unknown-linux-gnu)
v8 13.5.212.10-rusty
typescript 5.7.3
3 Replies
marvinh.
marvinh.2w ago
good catch, looks like this example in the documentation is wrong. Module exports cannot be mocked as they are not writable
marvinh.
marvinh.2w ago
GitHub
Bug: Basic mocking example doesn't work · Issue #1631 · denoland/...
The example for basic mocking doesn't work, see https://docs.deno.com/examples/testing_tutorial/#basic-mocking . Reason it doesn't work is that it tries to replace properties of a module re...
beeler
beelerOP2w ago
Thank you for opening the issue. I'll look around to see how people handle breaking dependencies.

Did you find this page helpful?