Skip to content

Question : Mock a subdependencies with ESM. Is it the expected behaviour ? #117

@Yoshyn

Description

@Yoshyn

Hi, I am trying to use quibble to mock an ESM module and its subdependencies on a really simple exemple, but I seem to be missing something or encountering unexpected behavior.

Here's my setup:

// src/subdep.mjs
console.log('Load src/subdep.mjs');
export function myFunction() {
    return "real subdep function is called...";
}


// src/utils.mjs
console.log('Load src/utils.mjs');
import { myFunction } from './subDep.mjs'
export function fetchData() {
    const res = myFunction();
    return `real utils.fetchData function is called with subdep : ${res}`;
}

// ___test__/test.mjs
import quibble from 'quibble'

await quibble.esm('../src/subdep.mjs', { myFunction:() => { return 'mocked subdep ! Yeah !' } }, { mocked : 'replacement universe' });
const subdep = await import('../src/subdep.mjs');
console.log(subdep.myFunction()); // OK => mocked subdep ! Yeah !

console.log('-------------------');

const subject = await import('../src/utils.mjs');
const result = subject.fetchData()
console.log(result); // FAIL => real subdep function is called...

Command and Output : (node version = v22.4.1)

node __test__/test.mjs

Actual Output :

mocked subdep ! Yeah !
-------------------
Load src/subdep.mjs                   <--- I did not expect that
Load src/utils.mjs
real utils.fetchData function is called with subdep : real subdep function is called... <--- make sence but not expected

Expected Output :

mocked subdep ! Yeah !
-------------------
Load src/utils.mjs
real utils.fetchData function is called with subdep : mocked subdep ! Yeah !

Questions :

  • Is this the expected behavior, or am I missing something in how quibble works with ESM modules and subdependencies?
  • If this is a limitation, are there workarounds or other strategies to mock ESM subdependencies in this context?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions