Chai returning 426 when providing url

19 Views Asked by At

I'm trying to test my API with chai using chai-http. But, it only works if I pass the server as argument to request function, not if I pass the url. The seconds tests returns 426. What's the catch? It works through Postman of course.

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

chai.use(chaiHttp);

const server = require('../index')

const methodUrlBase = '/api/platforms'

describe('platforms', () => {
  it('Should return platforms - works', async () => {
    const res = await chai.request(server)
      .get(methodUrlBase)
    expect(res.status).to.equal(200)
  })

  it('Should return platforms - doesn\'t work', async () => {
    const res = await chai.request('http://localhost:3000')
      .get(methodUrlBase)
    expect(res.status).to.equal(200)
  })
})

EDIT: server code

const express = require('express');

const app = express();

app.use(express.json());

app.get('/api/platforms', (req, res) => {
    return res.send({ resp: 'hardcoded' });
});

const server = app.listen(3000, () => console.log('Server up'));

module.exports = server
1

There are 1 best solutions below

0
Gervasius Twinklewinkleson On

Ok, so there was something funky going on with ports 3000 and 3001. It works as expected when using port 3002