2
votes

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.

3
There's a difference between "starts with" and "is exactly". And you have to escape special characters in the string (why is it a string at all?) that you pass to new RegExp().Andreas
Sir, this code ipAddress.startsWith(CommonConstants.BAD_IP_ADDRESS) is written by very senior JS developer, I have to fix only unit test case part.PythonLearner
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 new RegExp().PythonLearner
new RegExp() === new RegExp() is false. You should remove the call to new RegExp()Alex K
So how should I fix it, please suggest? My job is to fix only the unit test case only.PythonLearner

3 Answers

1
votes

You need to remove the call to new RegExp(). That's going to return a new RegExp object each time, causing the test to fail.

To fix the compilation error, you need to correct the return type of ipAddressPattern. You're not returning a RegExp object, you're returning a string.

So the signature needs to be: public ipAddressPattern(ipAddress: string): string

0
votes

In javascript, the String.prototype.startsWith method (@line 3 in your CommonUtil class) is expecting a raw string, not a regex (in string format).

expect(component.ipAddressPattern("169.254.11.11").toString()).toBe(new RegExp(CommonConstants.BAD_IP_ADDRESS_PATTERN).toString().replace(/^\/|\/$/g, ''))

0
votes

When I added "@types/jasmine": "^2.8.7",, in devDependencies, the problem got solved. So I did like this.

"devDependencies": {
    "@angular/compiler-cli": "^7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/core-js": "^0.9.46",
    "@types/jasmine": "^2.8.7",
    ... other modules ...
}