Testing API using frisby.js/jest with Gauge

809 views Asked by At

I'm trying to get gauge-js to work with Frisby. We run Functional tests on our APIs as a black-box tests using Frisby. Recently upgrade Frisby to version 2.0.8 which now use Jest. All worked good. Now I want to add Gauge-js on top to add human-readable test-specs/scenarios/steps.

I'm testing on Windows 8.1 machine:
- Frisby
- Gauge 0.9.4
- gauge-js 2.0.3

To make it work I add Frisby as a dependency to gauge-js. Now it partially works. It actually execute test step, but failed with

 ReferenceError: expect is not defined
    at incrementAssertionCount (C:\Users\<USER>\AppData\Roaming\gauge\plugins\js\2.0.3\node_modules\frisby\src\frisby\expects.js:14:20)
    at FrisbySpec.status (C:\Users\<USER>\AppData\Roaming\gauge\plugins\js\2.0.3\node_modules\frisby\src\frisby\expects.js:23:5)
    at FrisbySpec._addExpect.e (C:\Users\<USER>\AppData\Roaming\gauge\plugins\js\2.0.3\node_modules\frisby\src\frisby\spec.js:396:23)
    at FrisbySpec._runExpects (C:\Users\<USER>\AppData\Roaming\gauge\plugins\js\2.0.3\node_modules\frisby\src\frisby\spec.js:288:24)
    at _fetch.fetch.then.then (C:\Users\<USER>\AppData\Roaming\gauge\plugins\js\2.0.3\node_modules\frisby\src\frisby\spec.js:142:14)
    at process._tickCallback (internal/process/next_tick.js:109:7)

Here is the actual test step:

/* globals gauge*/

"use strict";

var frisby = require('frisby');

// --------------------------
// Gauge step implementations
// --------------------------

step("Get responds with <state>.", function (state, doneFn) {

  frisby
    .timeout(1500)
    .get('http://localhost:8001/some/get/resource', {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Basic QWERTYASDFEDEFTGHYFVCCFRJgyuku'
      }
   })

    // .expect('status', 200)
    // .expect('header', 'Content-Type', 'application/json; charset=utf-8')
    // .expect('json', state)
    .done(doneFn).catch(error => {
      console.log(error);
    });
});

When commented out lines uncommented the error occur.

I think the problem is actually with how it's load dependencies, but my js knowledge is a bit fragmented and rusty. Any help would be appreciated.

2

There are 2 answers

0
0rangeBean On BEST ANSWER

I found better solution to the original problem. When I start implementing what @duyker suggested. I noticed (finally) that Frisby code has intention to ignore dependency on Jasmine if it's not there, but has a bug. So I submit a fix for it. And it was accepted.

Now problem solved and Frisby can work without Jasmine or Jest.

1
duyker On

The reason why the .expect(...) functions are undefined is that frisby is expecting that jasmine is the test runner and the expect function from jasmine is available for it to use.

All of frisby's included expect functions execute the below "incrementAssertionCount". Because it cannot find the expect in the if (_.isFunction(expect)), it is unable to provide the default expects that frisby would offer.

function incrementAssertionCount() {
  if (_.isFunction(expect)) { // FAILS HERE: 'expect' does not exist
    // Jasmine
    expect(true).toBe(true);
  }
}

There looks like there is a simple alternative, which is to copy the expect functions from frisby into your code and call the addExpectHandler method (even better, make an external package you can reference so you don't have to copy it into every project).

A simple example is shown below:

var frisby = require("frisby");
var assert = require("assert");

function statusHandler(response, statusCode) {
  assert.strictEqual(response.status, statusCode, `HTTP status ${statusCode} !== ${response.status}`);
}

beforeScenario(function () {
  frisby.addExpectHandler('status', statusHandler);
});

step("Call httpbin.org and get a teabot", function (done) {
  frisby.get('http://httpbin.org/status/418')
    .timeout(2000)
    .expect('status', 418)
    .catch(function (e) {
      done(e);
    })
    .done(done);
});