supertest

Is done required in async Jest tests?

半世苍凉 提交于 2021-02-20 09:42:07
问题 I'm having an argument with a co-worker about done() in Jest tests. He's orders of magnitude more experienced with JavaScript than I am, and I came in after async / await was generally accepted, plus came from a .NET environment, so I was used to it. I write my tests like this: it("should return 200 OK for POST method", async () => { await request(app).post("SOMEENDPOINT") .attach("file", "file") .expect(200); }); He's more used to promises so will write his tests like this: it("should return

Is it possible to use supertest with hapi?

こ雲淡風輕ζ 提交于 2021-02-08 15:48:13
问题 I'm using hapi, not express. Should supertest still work? If so, is there a quick way to change my code to get it to run? My test looks like this, based on the documentation: import tape = require('tape'); const supertest = require('supertest'); const app = require('../../../../src/app'); tape('creates new user in database', function (assert) { supertest(app) .get('/ekapi/v1/signup') .expect(200) ...... }); but it gives me this error: dist/server/app/node_modules/supertest/lib/test.js:55 var

Multiple server instance attempts while running jest

夙愿已清 提交于 2021-01-29 06:33:15
问题 Okay, So I've been writing tests for my node.js app using jest and supertest, for every test suite after the first, I'm getting an error Error: listen EADDRINUSE: address already in use :::3000 , I believe this is because it attempts to start the server on every test file (I have multiple test files *.test.js in /tests ) The top part before tests are described in each test file looks something like this const request = require("supertest"); const app = require("../index.js"); // the express

Jest test are run before DB is ready

二次信任 提交于 2021-01-28 06:52:16
问题 I am using Jest to test my express API with SQLite database, but the following problem appeared - test are run before DB is ready and tables are created. I connect to DB with following code: const connectToDatabase = () => { let db; if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'production') { db = new sqlite3.Database('task_list'); } else if (process.env.NODE_ENV === 'test') { db = new sqlite3.Database(':memory:'); } else { throw new Error('Environment is not defined

The difference between axios and supertest in NodeJS

倾然丶 夕夏残阳落幕 提交于 2021-01-27 19:40:38
问题 Both Axios and Supertest can send HTTP requests to a server. But why is Supertest used for testing while Axios is used for practice API calls? 回答1: There are two reasons to use Supertest rather than a vanilla request library like Axios (or Superagent, which Supertest wraps): It manages starting and binding the app for you, making it available to receive the requests: You may pass an http.Server , or a Function to request() - if the server is not already listening for connections then it is

The difference between axios and supertest in NodeJS

孤街醉人 提交于 2021-01-27 19:17:07
问题 Both Axios and Supertest can send HTTP requests to a server. But why is Supertest used for testing while Axios is used for practice API calls? 回答1: There are two reasons to use Supertest rather than a vanilla request library like Axios (or Superagent, which Supertest wraps): It manages starting and binding the app for you, making it available to receive the requests: You may pass an http.Server , or a Function to request() - if the server is not already listening for connections then it is

Extending SuperTest

对着背影说爱祢 提交于 2021-01-27 13:37:31
问题 I like to use SuperTest to work with my auth system like this: const request = require('./valid_access_token')(require('supertest')); request(app) .get('/v1/bots') .valid_token() .expect('Content-Type', /json/) ... valid_token() would insert a valid access-token module.exports = function (Request) { const Token = require('../../../app/v1/models/loader')('token'); const UserFactory = require('../fixtures/user'); Request.prototype.valid_token = function() { return UserFactory.createAsync('user'

Does mocha/supertest create express server for each test suite?

夙愿已清 提交于 2021-01-25 04:09:47
问题 I have been using mocha , supertest and proxyquire since last few days. I am able to do integration test with no problem. But I have some questions. This is one test suite from my project. const expect = require('chai').expect const request = require('supertest') const _ = require('lodash') const sinon = require('sinon') const faker = require('faker') describe('ComboController /api/v1/combos', function () { const app = require('../src/app') it('should GET combo of given id: getComboById',

Does mocha/supertest create express server for each test suite?

我的未来我决定 提交于 2021-01-25 04:06:20
问题 I have been using mocha , supertest and proxyquire since last few days. I am able to do integration test with no problem. But I have some questions. This is one test suite from my project. const expect = require('chai').expect const request = require('supertest') const _ = require('lodash') const sinon = require('sinon') const faker = require('faker') describe('ComboController /api/v1/combos', function () { const app = require('../src/app') it('should GET combo of given id: getComboById',

Why is my sinon stub acting like it's calling the real function?

你离开我真会死。 提交于 2020-08-20 10:33:05
问题 I'm trying to follow this example: https://www.alexjamesbrown.com/blog/development/stubbing-middleware-testing-express-supertest/ but the sinon stub doesn't seem to be executing the wrapped code. I've seen lots of stackoverflow posts regarding this issue but none of the answers have helped me figure out what I'm doing wrong. Whenever I run my test I get the following error: 1) should return a list of sites 0 passing (42ms) 1 failing GET /api/config/buildPro/sites should return a list of sites