Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Yoshyn opened this issue Nov 21, 2024 · 0 comments
Open

Comments

@Yoshyn
Copy link

Yoshyn commented Nov 21, 2024

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?
@Yoshyn Yoshyn changed the title Question : How to make mock work with ESM and subdependencies ? Question : Mock a subdependencies with ESM. Is it the expected behaviour ? Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant