Bmoe
Bmoe

Reputation: 978

How to Require ALL of Jasmine Library with CucumberJS?

I want to unit test using Jasmine and CucumberJS for my Angular v9 application. Following the tutorial from cucumber.io, I've setup cucumber as the default runner. Unable to use jasmine methods though. Figured out how to use expect method but need access to the rest of the Jasmine library like spyOn and createSpyObj. When I execute line: expect(this.actualAnswer).toBe(expectedAnswer); error is thrown saying TypeError: Cannot read property 'toBe' of undefined at World.<anonymous> (C:\Users\User\cukejas\features\step_definitions\stepdefs.js:24:28)

Here's what I have so far. Can someone help please?

stepdefs.js

const assert = require('assert');
const { Given, When, Then } = require('cucumber');
const expect = require('C:\\Users\\User\\cukejas\\node_modules\\jasmine\\lib\\jasmine.js');

function isItFriday(today) {
  if (today === "Friday") {
    return "TGIF";
  } else {
    return "Nope";
  }
}

Given('today is {string}', function (givenDay) {
  this.today = givenDay;
});

When('I ask whether it\'s Friday yet', function () {
  this.actualAnswer = isItFriday(this.today);
});

Then('I should be told {string}', function (expectedAnswer) {
  // assert.equal(this.actualAnswer, expectedAnswer);
  //expect(1); --> This line executes without error
  expect(this.actualAnswer).toBe(expectedAnswer); // error thrown here
});

package.json

{
  "name": "cukejas",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "./node_modules/.bin/cucumber-js -p default",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.1",
    "@angular/common": "~9.0.1",
    "@angular/compiler": "~9.0.1",
    "@angular/core": "~9.0.1",
    "@angular/forms": "~9.0.1",
    "@angular/platform-browser": "~9.0.1",
    "@angular/platform-browser-dynamic": "~9.0.1",
    "@angular/router": "~9.0.1",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.2",
    "@angular/cli": "~9.0.2",
    "@angular/compiler-cli": "~9.0.1",
    "@angular/language-service": "~9.0.1",
    "@types/cucumber": "^6.0.1",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "cucumber": "^6.0.5",
    "cucumber-pretty": "^6.0.0",
    "cucumber-tsflow": "^3.2.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~5.18.0",
    "typescript": "~3.7.5"
  }
}

Upvotes: 0

Views: 89

Answers (1)

Steven Hunt
Steven Hunt

Reputation: 2321

Jasmine and CucumberJS are both test runners, so unfortunately you can't use both frameworks at the same time. However, you can use the expect package (part of Jest) which has a similar API:

const expect = require('expect');

Online example: https://testjam.io/?p=D11o5cwOIQukGy1F6GRU

Alternatively, chai is another wonderful library which supports expressive assertions.

As far as spyOn, you may consider using a standalone library such as sinon.

Upvotes: 1

Related Questions