PythonLearner
PythonLearner

Reputation: 1466

Expected '^169.254$' to be /^169.254$/ in jasmine unit testing

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.

Upvotes: 2

Views: 72

Answers (3)

PythonLearner
PythonLearner

Reputation: 1466

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 ...
}

Upvotes: 0

Andrei Sfia
Andrei Sfia

Reputation: 1

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, ''))

Upvotes: 0

alexhughesking
alexhughesking

Reputation: 2007

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

Upvotes: 1

Related Questions