-
Notifications
You must be signed in to change notification settings - Fork 25
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
Don't replace globally #39
Comments
As a matter of good TDD / mocking practice, even though quibble can fake out 3rd party modules it doesn't mean you necessarily should. In general, my approach is to write custom little adapters to third party dependencies so I have some scar tissue indirection with an API that I control such that I can make it conform to the style/shape of the rest of my app's code. (You can take or leave this design and testing advice but [here's a talk about mocking fwiw](Write a test-scoped wrapper of https://blog.testdouble.com/talks/2018-03-06-please-dont-mock-me/)) The reason I mention this is you could always have a module like If you don't want to do that, then you can always have your test-scoped support code require node-fetch in advance of any |
Cool I'll watch that video thanks. A few points:
In light of all that:
Thanks for explaining your thoughts and sharing that vid! |
Sorry, I only ever use quibble via td.js so I should have expressed it in terms of quibble's API. For ESM specific things I need to rely on @giltayar to chime in |
@umaar not sure I understand what the problem you have is: if I understand correctly, while running your app code in a test, that app code uses If my description of the problem is correct, then there's not a lot I can do: between the time you mock a module using Wait, maybe you can use |
Hey your understanding is correct @giltayar But anyway it seems one of the big issues is this
So importing is cached! That would explain the problems I've been having, and why Oh and here's an example file which demonstrates the problem: https://github.com/umaar/postcode-checker/blob/85e4e57b1aa9b29316812e7eca72398b916a9559/test/unit/test.js - there you'll notice One workaround I'm thinking is to mock Update: Searching around, I happened to stumble across this deno related comment by @kitsonk:
ha so maybe I've misunderstood and quibble does indeed attempt to fix this already! But then it wouldn't explain why mocking |
@umaar as you surmised, caching is not the problem. I use various magic tricks in the quibble ESM loader to deal with his (read all about it: https://dev.to/giltayar/mock-all-you-want-supporting-es-modules-in-the-testdouble-js-mocking-library-3gh1). Did you read my previous comment, and try out |
@giltayar sorry I didn't see any docs for Here's the issue I'm seeing: node --loader=quibble ./node_modules/.bin/ava test.js import quibble from 'quibble';
import test from 'ava';
test('test 1', async t => {
await quibble.esm('node-fetch', null, 'mock v1');
const {default: fn} = await import('fn.js');
quibble.reset();
});
test('test 2', async t => {
await quibble.esm('node-fetch', null, 'mock v2');
const {default: fn} = await import('fn.js');
quibble.reset();
});
test('test 3', async t => {
await quibble.esm('node-fetch', null, 'mock v3');
const {default: fn} = await import('fn.js');
quibble.reset();
}); I put a console.log in the The issue is: it's only ever the last quibble mock that seems to take effect. If I log the value of It sounds like you've already dealt with caching well. So now I'm thinking maybe it's something about how AVA, the test runner I'm using, runs tests. Maybe something to do with running tests in parallel. Whatever it is, putting only 1 test per file helps me avoid this problem. |
@umaar I'll try ava and see what i can figure out. I'll also try the same in Mocha to see whether it's specifically Ava. |
Thanks, I guess ava runs tests concurrently so maybe that has an effect. Also heads up, I tried running the tests for that repo in windows, and I'm seeing some funny behaviour - quibble doesn't seem to be working as expected. Haven't looked in detail though! |
Hey! This project is great and minimal, nice work!
I noticed it doesn't support mocking a dep, without affecting global use of such a module. E.g. my app source uses
node-fetch
which I want to mock out for an integration test. But interestingly my test code also usesnode-fetch
, butquibble
doesn't seem to support this use case. Any workarounds?.reset()
doesn't help here.The text was updated successfully, but these errors were encountered: