-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Required validator on FormControl not working in unit tests when using jest #88
Comments
Ok, I did a bit of investigation, and I think that it is because jest is using the Object.defineProperty(FormControl.prototype, "validator", {
get: function () {
return _super.prototype.validator;
},
set: function (validator) {
_super.prototype.validator = validator;
},
enumerable: false,
configurable: true
}); As you can see, the For comparison, the other functions are defined like this: FormControl.prototype.reset = function (formState, options) {
_super.prototype.reset.call(this, formState, options);
}; Note the use of I will submit a PR to fix this. |
The bug was introduced in this angular commit: angular/angular@ad7046b Because the Abstract control now uses property getters and setters for |
As a temporary workaround, you can add this code to your import { FormArray, FormControl, FormGroup } from '@ngneat/reactive-forms';
// Temporary fix for bug in @ngneat/[email protected]
delete FormControl.prototype.validator;
delete FormControl.prototype.asyncValidator;
delete FormGroup.prototype.validator;
delete FormGroup.prototype.asyncValidator;
delete FormArray.prototype.validator;
delete FormArray.prototype.asyncValidator; |
Hello, @markwhitfeld thank you so much for the work you've done to investigate this and fix it. I got as far as figuring out the UMD bundle was the problem so then tried to get jest working with ECMAScript Modules instead. Thanks again, I really appreciate it 😄 |
Hi @Ben348, did you come right with the change to the dist that jest uses? |
PS. This ES5 downleveling issue was logged in Typescript 7 years ago (and 44000 issues ago) and unfortunately did not have a reasonable solution. It is just broken and that is the way it will always be. See: microsoft/TypeScript#338 My PR fixes the issue for this lib though by removing the passthrough properties in the subclass at runtime. This fixes the issue without the performance hit that the babel transpilation approach suffers from, and retains the typedef information that was intended with these property overrides. |
…ombined validators function (#37881) This commit refactors the way we store validators in AbstractControl-based classes: in addition to the combined validators function that we have, we also store the original list of validators. This is needed to have an ability to clean them up later at destroy time (currently it's problematic since they are combined in a single function). The change preserves backwards compatibility by making sure public APIs stay the same. The only public API update is the change to the `AbstractControl` class constructor to extend the set of possible types that it can accept and process (which should not be breaking). PR Close #37881
@markwhitfeld thanks! I appreciated your work on this issue. I think we can get around this by using the code from this PR: https://github.com/ngneat/reactive-forms/pull/90/files WDYT? |
Yes, I think that is a good alternative approach, as I mentioned in my PR. |
Yes, I agree about the coupling, but we are all coupled to Angular anyway :) |
Thanks for the effort. |
PS. there was almost a carbon copy of this issue here: angular/angular#40327 |
Thanks for getting this merged and released so quickly 🎉 🚀 |
@markwhitfeld Sorry for the late reply. I was trying to get it working but not had much luck so far. If I get it working I'll be sure to let you know. You need to be using the next version of jest (27) and the next version of jest-preset-angular https://thymikee.github.io/jest-preset-angular/docs/next/getting-started/presets#the-presets Thank you both for the investigation and fixes on this issue here |
@Ben348 It is fixed in the latest release, so no workaround needed! 👍 |
Hello, I have tried to use version 1.7.3 in angular 9.1 and it does not work.
|
I'm submitting a...
Current behavior
When running unit tests in a jest environment they fail when using the required validator on a FormControl because it always returns true even when the value is invalid. Using the FormControl from the
@angular/forms
package works fine.This only happens in a jest environment, the actual functionality in a browser or when using jasmine for the unit tests works as expected. I'm sorry if I've raised this bug in the wrong place, it could be a problem with jest or jsdom. I'm going to keep trying to debug and see if I can find out the reason why.
The following issue might be relevant from what I found when searching the closed issues:
#86
Expected behavior
When the FormControl is
invalid
then it should returnfalse
when checking thevalid
property for unit tests running with jest.Minimal reproduction of the problem with instructions
I've attached a zip file to this ticket with a sample angular project that shows the tests passing when using jasmine and failing when using jest.
typed-forms.zip
To run the jasmine unit tests that work use this command:
npm run test
To run the jest unit tests that fail use this command:
npm run test:jest
What is the motivation / use case for changing the behavior?
Unit tests pass when using jest and this project.
Environment
The text was updated successfully, but these errors were encountered: