Questions tagged [supertest]

SuperTest is a module that provides high-level abstraction for testing HTTP in node.js, using low-level API provided by super-agent.

SuperTest is a module that provides high-level abstraction for testing HTTP in node.js, using low-level API provided by super-agent.

922 questions
7
votes
1 answer

How to send a formData object with supertest?

I just started to learn do test with supertest and mocha. I've read the api document of supertest and it said that supertest support all lower-level API provided by superagent. SuperAgent said we can send a formData object by:…
Feng Zhao
  • 71
  • 1
  • 3
7
votes
1 answer

Jest: Wait for an async test to finish before running the next one

Assuming I have these test cases (with jest and supertest): describe('Test actors', async () => { const params = { /* ... */ } let actorId test(`Create an actor ${actorsUrl}`, async () => { const response = await request(app.callback()) …
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
7
votes
1 answer

Close server and db connect after tests

I have koa server with mongo connection and use supertest to mock server and send requests, jest as test framework. const app = new Koa() ... export default app.listen(PORT, (err) => { if (err) console.log(err) if (!IS_TEST) { …
Khotey Vitaliy
  • 509
  • 1
  • 6
  • 18
7
votes
2 answers

How to trigger Express error middleware?

I'm trying to unit test this piece of code in Mocha: app.use(function (err, req, res, next) { console.error(err.stack) res.status(500).send('Something broke!') }) I don't know how to get my request inside a Mocha unit test to trigger it.
Eugene Fedotov
  • 344
  • 4
  • 13
7
votes
1 answer

Accessing req.session object when writing tests that use supertest

I have just started using supertest and nock to write unit tests for middleware in my express application. All requests handled by my router setup are first checked for the presence of a session property (I am using express-session). app.use('/api',…
mindparse
  • 6,115
  • 27
  • 90
  • 191
7
votes
1 answer

Supertest Expect Not Asserting Status Codes Correctly

I have a test that looks like this: it('should fail to get deleted customer', function(done) { request(app) .get('/customers/'+newCustomerId) .set('Authorization', 'Bearer ' + token) .set('Accept', 'application/json') …
SSH This
  • 1,864
  • 4
  • 23
  • 41
7
votes
1 answer

Is there any way to set defaults in supertest?

I'm using the supertest module to test my Rest API. my API sends JSON all the time. so I'm doing .expect('Content-Type', /json/) for all and each test! I'm repeating again and again! this is some of my code it('should list ALL permissions on…
asedsami
  • 609
  • 7
  • 26
7
votes
2 answers

Suppress REST Logging Calls from Supertest

I have started using MEAN stack and currently writing REST unit tests using Super Test I want to have a little bit more clarity in my log file so that I can easily see my successful and failed tests. I wish to suppress the console output for the…
David Cruwys
  • 6,262
  • 12
  • 45
  • 91
7
votes
2 answers

Getting "Error: the string "Not a valid BCrypt hash." was thrown, throw an Error :)" during Mocha ExpressJS testing

I have a MEAN stack app that is using Passport for authentication. I'm trying to write a unit test that logs in and checks whether you are redirected to the root (/). However, whenever I run Mocha I get the following error message: 1) POST /home…
FilmiHero
  • 2,306
  • 7
  • 31
  • 46
7
votes
1 answer

How to make authenticated requests in mocha test using supertest and agent?

I'm having trouble getting my authenticated tests to run (server returns 401 Unauthenticated) after logging in. var should = require('should'), _ = require('lodash'), app = require('../../../server'), mongoose = require('mongoose'), …
chovy
  • 72,281
  • 52
  • 227
  • 295
6
votes
1 answer

Express Jest and Supertest how to mock middleware with argument

I have been trying for several hours to test an endpoint of my rest api with Jest, Supertest and Express. This endpoint is protected by an authentication middleware named "advancedAuthGuard". So I'm trying to mock this middleware in order to skip…
Ecora
  • 1,043
  • 5
  • 10
6
votes
2 answers

Jest test failing with 'connect ECONNREFUSED 127.0.0.1:80' when app is listening on port 3000 (express and supertest)

I am struggling to resolve a bug in my jest test using supertest, I think it is an issue with my test rather than my code. My issue I am trying to create a back end API using node.js and express, using TDD. I have the following files to set up the…
Tom
  • 93
  • 1
  • 1
  • 8
6
votes
0 answers

Jest, TypeScript and ts-jest: SyntaxError: Cannot use import statement outside a module

I'm working on testing an Apollo Server using Jest and SuperTest. The server is written in TypeScript, so I'm using ts-jest. I need to bind SuperTest to a particular port. The SuperTest docs say You may pass an http.Server, or a Function to…
Cerulean
  • 5,543
  • 9
  • 59
  • 111
6
votes
1 answer

Supertest giving MaxListenersExceedingWarning error with my Jest testing in Node

I'm working on setting up some tests using Jest and Supertest with my Node Express app. I'm also using Sentry to report my errors. I have the following lines of code in my app.js file to setup Sentry: app.js Sentry.init({ environment: "development",…
speck1990
  • 127
  • 1
  • 12
6
votes
0 answers

Jest doesn't wait for async beforeAll to finish

Im trying to test getting all users from my REST API. describe('GET', () => { let userId; // Setup create the mock user beforeAll(async () => { //Create the user return await request .post(routes.users.create) …
Olga Kedel
  • 101
  • 1
  • 6