question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.expect() with custom assertion method always succeeds

See original GitHub issue
it('Should have response, time, code', function(done){
        apis.get('/')
            .expect('Content-Type', /json/)
            .expect(406)
            .expect(function(res){
                return true
                /*
                if(!('responses' in res.body)) return "Missing response key"
                if(!('timed' in res.body)) return "Missing time key"
                if(!('codes' in res.body)) return "Missing code key"*/
            })
            .end(done)
    })

My test is always successful even when returning true from the method - which should fail. My other attempts at tackling these are commented in the code.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23

github_iconTop GitHub Comments

3reactions
finferflucommented, May 24, 2017

This seems to be still an issue:

const request = require('supertest')
const app = require('../app')
const server = request.agent(app)

describe('GET /', function () {
  it('returns 200', function (done) {
    server
    .get('/')
    .expect(500, done)
  })
})

I wrote the above to intentionally fail the test, however it passes.

However, if I use a promise, it fails as expected:

describe('GET /', function () {
  it('returns 200', function (done) {
    server
    .get('/')
    .expect(500)
    .then(function () {
      done()
    })
    .catch(err => {
      done.fail(err)
    })
  })

Output:

Failures:
1) GET / returns 200
  Message:
    Failed: expected 500 "Internal Server Error", got 200 "OK"
3reactions
freak4pccommented, Jul 5, 2015

PS, throwing an exception does produce the right result.

e.g.

it('Should have response, time, code', function(done){
        apis.get('/')
            .expect('Content-Type', /json/)
            .expect(406)
            .expect(function(res){
                if(!('response' in res.body))   throw new Error("Missing response key")
                if(!('time' in res.body))       throw new Error("Missing time key")
                if(!('code' in res.body))       throw new Error("Missing code key")
            })
            .end(done)
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

expect() with custom assertion method always succeeds #253
AlexZeitler commented on Jul 12, 2015 ... Just tried this and it works: var request = require('supertest'); var app = require('../app.js'); var ...
Read more >
Java - TestNG : Why does my Assertion always passes when ...
@Test (expected = NotFoundException.class) public void example2() throws ... That's why your method always will be finished with success.
Read more >
Assertions Reference | GoogleTest
SUCCEED. SUCCEED(). Generates a success. This does not make the overall test succeed. A test is considered successful only if none of its...
Read more >
Assertions - Go Packages
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
Read more >
Expect / Should - Chai Assertion Library
The BDD styles are expect and should . Both use the same chainable language to construct assertions, but they differ in the way...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found