supertest

Supertest fails to test repeated post request

╄→尐↘猪︶ㄣ 提交于 2019-12-24 10:14:19
问题 I'm testing an api that creates users. The api does not allow the creation of users with the same login value. So I wrote the tests below: const app = require('../config/express'); //exports a configured express app const request = require('supertest'); const {populateUsers} = require('../seeders/users.seed'); beforeEach(populateUsers);//drop and populate database with some seeders describe('POST /v1/users', () => { it('#Post a new user - 201 status code', (done) => { request(app) .post('/v1

Difference between supertest's expect and then?

筅森魡賤 提交于 2019-12-23 15:29:38
问题 When using supertest for testing async HTTP requests in JavaScript, what's the difference between these two snippets? Is one of them correct and the other one wrong? request('http://localhost:8080/').get('/api/people') .expect(res => res.body.should.have.length(5)) vs. request('http://localhost:8080/').get('/api/people') .then(res => res.body.should.have.length(5)) The only differences I could notice were: expect returns a Test object and, when test fails, prints a large stack trace then

sails.js + mocha + supertest + sinon: how to stub sails.js controller function

痞子三分冷 提交于 2019-12-22 08:35:08
问题 I am trying to stub a sails controller function, but I don't know which object to stub. using sinon.stub(object,'funcname', function()... This is probably related to the way sails bind controller functions... Here is some code to give example Controller file api/controllers/PersonController.js var fs = require('fs'); // // I want to stub retrieveData function when testing // function retreiveData(cb) { fs.readFile('./filedata', function (err, data) { if (err) throw err; cb(data.toString()); }

NodeJS supertest access to session object

余生颓废 提交于 2019-12-21 03:58:12
问题 I'm testing my Node.js application with supertest. In my controller I access the session object. In order to make a valid request this session object needs to be filled with some data. Controller // determine whether it is user's own profile or not var ownProfile = userId == req.session.user._id ? true : false; Test it('profile', function (done) { testUserOne.save(function(error, user){ request .agent(server) .get('/profile?userId=' + user._id) .expect('Content-Type', /html/) .expect(200)

Read response output buffer/stream with supertest/superagent on node.js server

廉价感情. 提交于 2019-12-20 11:32:07
问题 I am trying to write a test that checks whether an API route outputs a ZIP file with the correct contents. I am using mocha and supertest for testing, and I would like to actually read the output stream/buffer, read the zip file contents and see if the contents are correct. Any ideas how should I do it? When I try to read res.body , it's just an empty object. request(app) .get( "/api/v1/orders/download?id[]=1&id=2" ) .set( "Authorization", authData ) .expect( 200 ) .expect( 'Content-Type',

NodeJS HTTPS API testing with mocha and super test -“DEPTH_ZERO_SELF_SIGNED_CERT”

送分小仙女□ 提交于 2019-12-18 14:56:14
问题 I need to test an API served via HTTPS with mocha and super test (the certificate are not expired) This is a gist of the server : ... var app = express(); var _options = { key: fs.readFileSync('my-key.pem');, cert: fs.readFileSync('my-cert.pem') }; // Start HTTPS server https.createServer(_options, app).listen(app.get('port'), app.get('ip'), function () { // ok or not logs }); and this is the route to be tested app.get('/hello',function (req, res) { res.json(200); }); I'm trying to test with

Access to “req” Object in Supertest After a Response

筅森魡賤 提交于 2019-12-18 07:44:43
问题 Is there any way to directly access the req object in supertest , while/after the request is being tested? I want to test my passport strategies, so I want to check req.user , req.session , and perhaps others. I know I can test page redirects or flash, as those are what my strategies do, but it seems useful to see if there is a user on the req object, as well. If I do this, I can also check how many users there are at any one time. I will sign users up with the "local-signup" strategy, which

Access to “req” Object in Supertest After a Response

喜欢而已 提交于 2019-12-18 07:44:39
问题 Is there any way to directly access the req object in supertest , while/after the request is being tested? I want to test my passport strategies, so I want to check req.user , req.session , and perhaps others. I know I can test page redirects or flash, as those are what my strategies do, but it seems useful to see if there is a user on the req object, as well. If I do this, I can also check how many users there are at any one time. I will sign users up with the "local-signup" strategy, which

How to start Locomotive server/app for integration testing?

我与影子孤独终老i 提交于 2019-12-13 07:23:27
问题 I am converting an Express app to Locomotive and I cannot figure out how to migrate my tests. In Express, I simply did this in my /test/test.api.organization.js file: var app = require("../app").app, request = require("supertest"); should = require("should"); describe("Organization API", function() { it( "GET /api/v1/organizations should return status 200 and Content-Type: application/json.", function (done) { postReq.done( function () { request(app) .get( "/api/v1/organizations" ) .set(

Testing with Mocha and Super test: Uncaught error outside test suite

本秂侑毒 提交于 2019-12-13 02:58:29
问题 While testing my api endpoints using mocha and supertest I got some of my test passing and some are not. The error message I got was uncaught error outside test suite: Uncaught error: listen EADDRINUSE:::5000 回答1: Even I have faced same issue. This is because supertest keep on listening port even after completing the execution of test cases. So, run the mocha command with --exit flag. Before running npm test make sure there is no service running on the specified port ... "scripts": { "start":