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.

Supertest fails on Content-Type

See original GitHub issue

Supertest says the content-type is text/plain, but when I use Postman or Chrome devtools and check the network tab, it correctly shows the content-type as text/html.

describe('Core controller unit tests:', function() {
    before(function(done) {
        request = request('http://localhost:3001');

        done();
    });

    describe('Loading the homepage', function() {
        it('should return 200 from GET /', function(done) {
            request
                .get('/')
                .expect('Content-Type', /html/)
                .expect(200, done);
        });
    });

    after(function(done) {
        done();
    });
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
trainiaccommented, Feb 1, 2015

+1 always getting “text/html; charset=utf-8” no matter what I do.

    it('should respond with a 200 if the posted body is valid', function(done) {
        request(app)
            .post('/user/', {
                json: true,
                body: helpers.getUser()
            })
            .set('Accept', 'application/json')
            .expect('Content-Type', /json/)
            .expect(function(response) {
                debugger;
                response.res.json.id.should.eql('123');
            })
            .expect(200)
            .end(done);
    });

app.post('/user/', validate({body: helpers.getUserSchema()}), function(req, res) {
   res.set('Content-Type', 'applicaton/json'); 
   res.json({
        id: '1234'
    });
});
3reactions
akotranzacommented, Oct 10, 2018

Had this error in an older project using https://www.npmjs.com/package/express-force-https … not a very popular package, but it will cause Express to always return 302 with html/text when using Supertest. With the tests expecting Content-Type of /json/ of course. Easy fix was to not use this package in test env. Commenting here in the off chance it saves someone else several hours of googling and debugging.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supertest fails on Content-Type · Issue #187 - GitHub
Supertest says the content-type is text/plain, but when I use Postman or Chrome devtools and check the network tab, it correctly shows the...
Read more >
Why my Supertest test that should be failing is passing?
The problem is that your test is asynchronous, but your code is synchronous. Specifically, the done() line is always called.
Read more >
Testing an Express App With Supertest and Jest
In this article, we're going to look at how to use TDD to test an express API using the supertest and jest frameworks....
Read more >
Testing Express Api with Jest and Supertest
First we have to pass our app.js module in to be able to make a request, then we define the route, what is...
Read more >
Supertest: How to Test APIs Like a Pro - Testim Blog
SuperTest is a Node.js testing library for HTTP APIs. ... You can try to expect() something different to see how the test fails....
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