-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix: mocking of getters/setters on automatically mocked classes #13398
Conversation
…ed classes (jestjs#13145)"" This reverts commit 52d45be. # Conflicts: # CHANGELOG.md
… 'get'/'set' functions being mocked as accessor objects. Added extra tests for this, and for some other conditions. Corrected expected test error messages due to the refactored code for the fix to this bug.
jest.config.mjs
Outdated
'/packages/jest-mock/src/__tests__/class-mocks-types.ts', | ||
'/packages/jest-mock/src/__tests__/TestClass.ts', | ||
'/packages/jest-mock/src/__tests__/SuperTestClass.ts', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor detail. Could you move these files to /packages/jest-mock/src/__tests__/__fixtures__/
? Recently I added /__fixtures__/
(see above) to this list to make it less noisy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, though I cannot see the /packages/jest-mock/src/__tests__/__fixtures__/
folder. The packages/jest-mock/__typetests__/__fixtures__/
folder does not exist either though I would have expected this to be a candidate for this reorg. Have I missed a commit/made a bad merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no folder at the moment. Just create it (;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to say that if you create src/__tests__/__fixtures__/
folder it will get ignored, because __fixtures__
was recently added to the ignore list. All would work without tweaking Jest config. That was the idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I interpreted your initial comment as being that you had already done so, somewhere.
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome, thanks!
Glad to help dude |
Hello! @SimenB This introduced a breaking change unfortunately... we had some named exports working since 2 hours ago, then we updated to
The line is:
The import we have is:
If I log the value of I see that in this PR there are some changes that could have caused this. Moreover, if we pin |
Hi @nuragic, apologies that the change caused this issue. I will assume responsibility and try to determine the cause and a fix. Are you able to provide source code for the object you are trying to mock? The full type hierarchy/prototype chain would be the best. There are tests for the general case of function mocking so the specific objects you are using would be essential to determine the case for the failure. Please provide anonymous code that also recreates the issue if you cannot provide the original source. |
@nuragic I have tried the following, which worked okay. class-mocks-types
class-mocks.test
|
Hi all, same here. Here you can find and example failing. I hope it helps |
@devcorraliza thanks! that's great. Recreated the error. This is specific to module imports where the exports are exposed as accessor properties and retrieved through getters. In module mocking So I was able to recreate the error in your jestspyonerror repo. However, when I copied your code to the jest repo it passed without any changes, as my similar code did previously (see this commit). In this case the exports are just data properties, not accessors, so the normal function mocking works. So, with my limited experience I can only assume this is a node/Javascript versioning difference/config issueto do with how module contents are exported. I couldn't see any immediate package settings that differed so I will have to leave it to another person to provide an explanation of the root cause. This also leaves the issue of verifying the change in the jest repo. Help here would be appreciated. I have applied the fix to the jest repo in this commit. As I could not immediately recreate the issue in the jest repo to verify the changes I tried to use an existing test that brought this case to my attention for automatic mocks ( Calling on @SimenB , @mrazauskas for assistance. |
Hi @staplespeter, maybe the difference is at the transpiler used. In my repo I'm using swc and additionally a plugin to make properties configurable (jest_workaround). Anyway it is working perfectly with 29.1.2 Just to add more information (although i think is not relevant) my node version is 14.19.3 Thanks for all the explanation and commit, just a remainder for people in charge that tests are broken and it is not easy to fix because jest_mock v29.2.0 is automatically installed although you set jest 29.1.2 at package.json. I would recommend to set a fix revision for jest_mock to restore last state till we have a fix for that. Thx in advance |
@devcorraliza thanks for the extra info. |
Happy to take a PR with a fix 🙂 |
@staplespeter this is the transpiled code which cannot be spied upon (passed through prettier) 'use strict';
Object.defineProperty(exports, '__esModule', {
value: true,
});
function _export(target, all) {
for (const name in all)
Object.defineProperty(target, name, {
enumerable: true,
get: all[name],
configurable: true,
});
}
_export(exports, {
f1: () => f1,
f2: () => f2,
});
const f1 = () => {
console.log('Im f1 calling f2');
(0, exports.f2)();
};
const f2 = () => console.log('Im f2'); |
@staplespeter Hey, you don't have to apologise at all. Thanks for your contributions. We're all here to help. 🤗 @devcorraliza You can temporarily workaround this issue by adding this to your
Happy to provide more info/help on this. In our case it also has to do with transpiled code (by TypeScript) which is (expanding on the above example): var init_stuff_1 = require("./init-stuff");
Object.defineProperty(exports, “initStuff”, { enumerable: true, get: function () { return init_stuff_1.initStuff; } }); Inside export function initStuff () { /* … */ } |
); | ||
} | ||
} else if (typeof descriptor.value !== 'function') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@staplespeter What I found by quickly debugging the issue is that, the check we have here is overlooking descriptor.get
which in our case is a function; descriptor.value
is undefined
. I guess we should check both.
Thanks @nuragic. I've added a pull request for the fix and actually realised that restore functionality for accessor properties was also missing, so have added that too. One test is incomplete, so if it is okay i will continue discussion on the new pull request. |
…es (jestjs#13398)" This reverts commit f126336.
I've released https://github.com/facebook/jest/releases/tag/v29.2.1 reverting most of this PR (modulo a bunch of tests that still pass) |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This is a new pull request to complete #13145, which was prematurely closed.