I am new to Angular, Karma, Jasmine test framework. I am getting the following error. It was working fine. After upgrade to angular 7, I am getting the below error.
[1A[2K[31mElectron 2.0.2 (Node 8.9.3) HostComponent should call ipAddressPattern and check IP bad IP FAILED[39m Expected '^169.254$' to be /^169.254$/. at at UserContext.it (karma-test-shim.js:298475:34418) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (karma-test-shim.js:295054:26) at ProxyZoneSpec../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (karma-test-shim.js:294539:39) at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (karma-test-shim.js:295053:52) at Zone../node_modules/zone.js/dist/zone.js.Zone.run (karma-test-shim.js:294813:43) at runInTestZone (karma-test-shim.js:294104:34) at UserContext. (karma-test-shim.js:294119:20) at
My code is given below.
In unit test
it("should call ipAddressPattern and check IP bad IP", () => {
expect(component.ipAddressPattern("169.254.11.11")).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN));
});
Earlier code was like this expect(component.ipAddressPattern("169.254.11.11")).toBe(CommonConstants.BAD_IP_ADDRESS_PATTERN);
and it was working. After upgrade it gave compilation issue. So I changed to expect(component.ipAddressPattern("169.254.11.11")).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN))
In the common constant class, the code is given below.
public static readonly BAD_IP_ADDRESS_PATTERN: string = "^169\.254$";
In other classes, the code is given below.
public ipAddressPattern(ipAddress: string): RegExp {
return CommonUtil.isBadIPAddress(ipAddress);
}
In CommonUtil class, the code is given below.
public static isBadIPAddress(ipAddress: string): any {
if (ipAddress) {
if (ipAddress.startsWith(CommonConstants.BAD_IP_ADDRESS)) {
return CommonConstants.BAD_IP_ADDRESS_PATTERN;
} else {
return ValidationPatterns.IP_ADDRESS;
}
}
}
Please suggest me how to fix this issue.
new RegExp()
. – AndreasipAddress.startsWith(CommonConstants.BAD_IP_ADDRESS)
is written by very senior JS developer, I have to fix only unit test case part. – PythonLearnerexpect(component.ipAddressPattern("169.254.11.11")).toBe(CommonConstants.BAD_IP_ADDRESS_PATTERN);
and it was working. After upgrade it gave compilation issue. So I changed tonew RegExp()
. – PythonLearnernew RegExp() === new RegExp()
isfalse
. You should remove the call tonew RegExp()
– Alex K