-
Notifications
You must be signed in to change notification settings - Fork 142
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
td.replace doesn't work with Jest #331
Comments
This is the issue described here testdouble/quibble#23 , I think this is causes because jest „beautifies” the stack for nicer error messaging, which makes it impossible for quibble to use it’s hack to find the function caller. I half-hacked that and there were still problems so I’m not sure if that’s the only reason though. |
Hey @lgandecki thanks for the simple explanation. I wonder if there's a way for us to pre-empt the replacement of printStackTrace() by grabbing a reference to the original function at require-time and then holding it, and using that instead? |
I suspect this is related to the fact Jest spawns VMs for its tests. Here's a Jest stack trace as observed in @twilczek's very handy example:
And here's the same test run through Mocha:
|
(This issue just became my war-room for documenting what seems to be going on, so feel free to mute notifications on it if you feel spammed) This code is what we quibble relies on to implement Oddly enough, node-sandboxed-module accomplishes |
I suspect our best bet is to create a I tried to read their code but had no luck understanding what it's doing. |
I can also confirm that simply fixing the error trace is not sufficient to making quibble work with Jest. Other aspects of the way Jest runs tests are also preventing us from mocking require as you'd hope. The following patch does not fix it for me:
|
Okay, job's done. I just released testdouble-jest and testdouble@3.4.0. This should work now if you use #337 should add a check to see if we're inside a jest run from the |
Fantastic! Thanks @searls |
Heads up that today I published testdouble-jest@1.0.1 and testdouble@3.5.0. In concert, setting up testdouble-jest should make |
Works like a charm, I've added some quick tests to jest-runner-standard (https://github.com/TheBrainFamily/jest-runner-standard/blob/bce5e28b6d958b7228baab1b6125ae63e736fec6/run.spec.js - relatively low value, but with the fantastic td syntax it was like 30 minutes total :) worth it.) Meanwhile I realized that I could just use: jest.mock('./helpers/getLocalStandard', () => ({getLocalStandard: td.func()}))
const { getLocalStandard } = require('./helpers/getLocalStandard') but the consistency with other testing frameworks, and also the much cleaner/concise syntax is great, this: const {getLocalStandard} = td.replace('./helpers/getLocalStandard', {getLocalStandard: td.func()}) hands down! Thanks again :) It was a smart call to use the jest internals instead of trying to hack the quibble into submission. |
Description
td.replace doesn't work with jest. I created a small GIF showing the error:
http://g.recordit.co/DONLJhs7LD.gif
In the GIF you can see I am running the same test using Mocha and Jest.
Unfortunately td.replace fails to replace required function when I run the spec with Jest.
I have created the repo where you can reproduce the issue:
git clone https://github.com/twilczek/jestFailRepro.git
npm install
npm run test:jest (fails)
npm run test:mocha (works)
Environment
tested on node version 8.3.0 and 9.3.0
The text was updated successfully, but these errors were encountered: