Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

supertest not found error testing express endpoint

I tried to set up jest, supertest, and express but failed. I have these 2 simple file

index.js

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => res.send("Hello World!"));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

and index.test.js

const express = require("express");
const app = express();
const request = require("supertest");

describe("/", () => {
  test("it says hello world", done => {
    request(app)
      .get("/")
      .expect(200)
      .end(function(err, res) {
        console.log("err", err);
      });
  });
});

when I run the test I'm getting this error. err Error: expected 200 "OK", got 404 "Not Found"

What's wrong?

I visit localhost:3000 in my browser I can see 'Hello World!'

like image 352
Hanz Avatar asked May 14 '19 03:05

Hanz


People also ask

How do I use Supertest to test endpoints in express?

Note: Here’s an article on Async/await in JavaScript if you don’t know how to use it. You can use Supertest to test endpoints. First, you need to install Supertest. Before you can test endpoints, you need to setup the server so Supertest can use it in your tests. Most tutorials teach you to listen to the Express app in the server file, like this:

How do I use Supertest to test get/API/users?

Let's create a new test case called GET /api/users. Here, we're adding a new document to our database so that we won't get an empty response. Then, we send a GET request using SuperTest to the /api/posts endpoint and expect the response status to be 200, which means success. Finally, we check if the response matches the data in the database.

How to write tests for the endpoints?

Now, let's begin writing tests for the endpoints. Create a file named routes.test.js inside the tests directory The describe function is used for grouping together related tests The it is an alias of test function which runs the actual test. The expect function tests a value using a set of matcher functions.

How to write the test for this route in out apitest?

Ok, how we write the test for this route in out apiTest.js using supertest. Here describe method is coming from mocha test framework. We simply specify the endpoint name as the first argument and then the function to write the test case. Here Mocha describe () is for grouping the test cases while it () is used to write the real test cases.


1 Answers

you should refactor index.js and create app.js

app.js

const express = require("express");
const app = express();
app.get("/", (req, res) => res.send("Hello World!"));

index.js

const app = require('./app')
const port = process.env.PORT
app.listen(port, () => { console.log(`listening on ${port}) . })

the reason why we restructure the code like this is we need to access to express app() but we do not want "listen" to be called.

in your test file

const request = require("supertest");
const app = require("../src/app");

describe("/", () => {
  test("it says hello world", done => {
    request(app)
      .get("/")
      .expect(200)
      .end(function(err, res) {
        console.log("err", err);
      });
  });
});
like image 144
Yilmaz Avatar answered Oct 10 '22 12:10

Yilmaz