Amateur node.js express developer seeks help on chai

78 views
Skip to first unread message

Arka Halder

unread,
Apr 1, 2018, 12:04:33 PM4/1/18
to Chai.js
Hello everyone,

I am completely novice in the npm world; so, this thread may seem silly for the respected experienced members. I would like to request you to help me about the snaps I've attached with this thread.
I am developing a simple web application where the user performs some registration into it. When there is some anomaly in the data provided with the post request from the client side, the server responds back with status code 400 - Bad Request along with a status text.

My server responds perfectly, however, chai seems to treat the response status as 200. Please find the snapshot of it in the attachments. Please also find the code I'm running while performing my tests. Please let me know if I am doing something wrong.

Thanks and Regards,
Arka Halder
snap1.jpg
snap2.jpg

Robin

unread,
Apr 2, 2018, 6:51:57 AM4/2/18
to Chai.js
I think the problem must be with your code in `../app`. If you look at the example below, you'll see your same test code working correctly with a simple server. Without being to see your app code it's hard to see where the problem actually is...

Example code based on your test:
```javascript
'use strict';

const chai = require('chai');
const chaiHttp = require('chai-http');
const assert = chai.assert;

chai.use(chaiHttp);

const server = require('express')()
.use(require('body-parser').json())
.post('/', (req, res, next) => {
if (req.body.valid) {
res.status(200);
return res.send('OK');
}

res.status(400);
next(new Error('User error'));
});

describe('test', () => {
it('should do a 200 properly', (done) => {
chai.request(server)
.post('/')
.send({
valid: true
})
.end((err, res) => {
assert.equal(res.status, 200);
assert.equal(res.text, 'OK');
done();
});
});
it('should do a 400 properly', (done) => {
chai.request(server)
.post('/')
.send({
valid: false
})
.end((err, res) => {
assert.equal(res.status, 400);
assert.equal(/User error/.test(res.text), true);
done();
});
});
});
```

Output:
```bash
$ mocha js/chaiHTTP.js --reporter mochawesome


  test
    ✓ should do a 200 properly (73ms)
    ✓ should do a 400 properly
Error: User error
    at require.use.post (/home/robin/dev/workspace/Sandbox/js/chaiHTTP.js:18:10)
    at Layer.handle [as handle_request] (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/layer.js:95:5)
    at /home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/index.js:335:12)
    at next (/home/robin/dev/workspace/Sandbox/node_modules/express/lib/router/index.js:275:10)
    at /home/robin/dev/workspace/Sandbox/node_modules/body-parser/lib/read.js:130:5
    at invokeCallback (/home/robin/dev/workspace/Sandbox/node_modules/raw-body/index.js:224:16)
    at done (/home/robin/dev/workspace/Sandbox/node_modules/raw-body/index.js:213:7)
    at IncomingMessage.onEnd (/home/robin/dev/workspace/Sandbox/node_modules/raw-body/index.js:273:7)
    at emitNone (events.js:105:13)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)


  2 passing (114ms)

[mochawesome] Report JSON saved to /home/robin/dev/workspace/Sandbox/mochawesome-report/mochawesome.json

[mochawesome] Report HTML saved to /home/robin/dev/workspace/Sandbox/mochawesome-report/mochawesome.html
```

Arka Halder

unread,
Apr 2, 2018, 5:19:04 PM4/2/18
to Chai.js
Hello Robin,

Thank you for your prompt response! I have debugged my problem myself and now it's working fine. The problem was not with Chai, it was with me! :-D
I am a novice developer and never have worked with Promises. Bluebird is the answer. My server was actually returning a 200 as the execution was asynchronous.

Thanks,
Arka

Robin

unread,
Apr 3, 2018, 6:35:38 AM4/3/18
to Chai.js
Hi Arka. Yes promises can be a bit tricky the first few times you use them, but they make a lot more sense in the long run, so well worth getting to grips with them!
Here's a promisy version of the previous code, which also uses the more BDD-style descriptive `expect` language chaining that is (IMHO) the real joy of using Chai! It also makes use of the `status` method that `chai-http` gives you as a handy shortcut for checking the response code:

```javascript
'use strict';

const chai = require('chai');
const chaiHttp = require('chai-http');
const expect = chai.expect;


chai.use(chaiHttp);

const server = require('express')()
.use(require('body-parser').json())
.post('/', (req, res, next) => {
if (req.body.valid) {
res.status(200);
return res.send('OK');
}

res.status(400);
next(new Error('User error'));
});

describe('test', () => {
  it('should do a 200 properly', () => chai.request(server)

.post('/')
.send({ valid: true })
    .then((res) => expect(res).to.have.status(200).and.property('text', 'OK'))
);
it('should do a 400 properly', () => chai.request(server)

.post('/')
.send({ valid: false })
    .then((res) => expect(res).to.have.status(400).and.to.have.property('text').which.contains('User error'))
);
});
```
Reply all
Reply to author
Forward
0 new messages