22
loading...
This website collects cookies to deliver better user experience
npm i express
# dev dependencies
npm i -D jest supertest
app.js
looks like this:const express = require("express");
const app = express();
app.use(express.json());
const fakeDB = [
{
id: Math.floor(Math.random() * 100),
email: "[email protected]",
},
];
app.get("/", (req, res) => {
return res.status(200).json({ data: fakeDB });
});
app.post("/send", (req, res) => {
fakeDB.push({
id: Math.floor(Math.random() * 100),
email: req.body.email,
});
return res.status(201).json({ data: fakeDB });
});
app.put("/update/:id", (req, res) => {
const obj = fakeDB.find((el) => el.id === Number(req.params.id));
obj.email = req.body.email;
return res.status(200).json({ data: fakeDB });
});
app.delete("/destroy/:id", (req, res) => {
const i = fakeDB.findIndex((el) => el.id === Number(req.params.id));
fakeDB.splice(i, 1);
return res.status(200).json({ data: fakeDB });
});
module.exports = app;
main.js
is the following:const app = require("./app");
const start = (port) => {
try {
app.listen(port, () => {
console.log(`Api running at http://localhost:${port}`);
});
} catch (err) {
console.error(err);
process.exit();
}
};
start(3333);
package.json
, in the scripts property, let's change the value of the test property. For the following:"scripts": {
"start": "node main",
"test": "jest"
},
app.test.js
, where we will perform all the tests we have in our app.js
module.app.js
module.const request = require("supertest");
const app = require("./app");
// More things come after this
describe()
, which groups together a set of individual tests related to it. test()
or it()
(both do the same, but to be more intuitive in this example I'm going to use test()
), which performs an individual test.const request = require("supertest");
const app = require("./app");
describe("Test example", () => {
// More things come here
});
"/"
) using the GET method, we get the data that is stored in our database. First let's create our individual test, giving it the name GET /
.describe("Test example", () => {
test("GET /", (done) => {
// Logic goes here
});
// More things come here
});
app.js
module in to be able to make a request, then we define the route, what is the content type of the response and the status code.describe("Test example", () => {
test("GET /", (done) => {
request(app)
.get("/")
.expect("Content-Type", /json/)
.expect(200)
// More logic goes here
});
// More things come here
});
1
and that the email of the first and only element is [email protected]
.describe("Test example", () => {
test("GET /", (done) => {
request(app)
.get("/")
.expect("Content-Type", /json/)
.expect(200)
.expect((res) => {
res.body.data.length = 1;
res.body.data[0].email = "[email protected]";
})
// Even more logic goes here
});
// More things come here
});
describe("Test example", () => {
test("GET /", (done) => {
request(app)
.get("/")
.expect("Content-Type", /json/)
.expect(200)
.expect((res) => {
res.body.data.length = 1;
res.body.data[0].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
// More things come here
});
POST /send
, but this time we're going to change the route as well as the method.describe("Test example", () => {
// Hidden for simplicity
test("POST /send", (done) => {
request(app)
.post("/send")
.expect("Content-Type", /json/)
// More logic goes here
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("POST /send", (done) => {
request(app)
.post("/send")
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(201)
// Even more logic goes here
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("POST /send", (done) => {
request(app)
.post("/send")
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(201)
.expect((res) => {
res.body.data.length = 2;
res.body.data[0].email = "[email protected]";
res.body.data[1].email = "[email protected]";
})
// Almost done
});
// More things come here
});
let elementId;
describe("Test example", () => {
// Hidden for simplicity
test("POST /send", (done) => {
request(app)
.post("/send")
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(201)
.expect((res) => {
res.body.data.length = 2;
res.body.data[0].email = "[email protected]";
res.body.data[1].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
elementId = res.body.data[1].id;
return done();
});
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("PUT /update/:id", (done) => {
request(app)
request(app)
.put(`/update/${elementId}`)
.expect("Content-Type", /json/)
// More logic goes here
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("PUT /update/:id", (done) => {
request(app)
request(app)
.put(`/update/${elementId}`)
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(200)
// Even more logic goes here
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("PUT /update/:id", (done) => {
request(app)
request(app)
.put(`/update/${elementId}`)
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(200)
.expect((res) => {
res.body.data.length = 2;
res.body.data[0].email = "[email protected]";
res.body.data[1].id = elementId;
res.body.data[1].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
// More things come here
});
describe("Test example", () => {
// Hidden for simplicity
test("DELETE /destroy/:id", (done) => {
request(app)
.delete(`/destroy/${elementId}`)
.expect("Content-Type", /json/)
.expect(200)
// More logic goes here
});
});
describe("Test example", () => {
// Hidden for simplicity
test("DELETE /destroy/:id", (done) => {
request(app)
.delete(`/destroy/${elementId}`)
.expect("Content-Type", /json/)
.expect(200)
.expect((res) => {
res.body.data.length = 1;
res.body.data[0].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
});
app.test.js
) should look like this:const request = require("supertest");
const app = require("./app");
let elementId;
describe("Test example", () => {
test("GET /", (done) => {
request(app)
.get("/")
.expect("Content-Type", /json/)
.expect(200)
.expect((res) => {
res.body.data.length = 1;
res.body.data[0].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
test("POST /send", (done) => {
request(app)
.post("/send")
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(201)
.expect((res) => {
res.body.data.length = 2;
res.body.data[0].email = "[email protected]";
res.body.data[1].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
elementId = res.body.data[1].id;
return done();
});
});
test("PUT /update/:id", (done) => {
request(app)
.put(`/update/${elementId}`)
.expect("Content-Type", /json/)
.send({
email: "[email protected]",
})
.expect(200)
.expect((res) => {
res.body.data.length = 2;
res.body.data[0].email = "[email protected]";
res.body.data[1].id = elementId;
res.body.data[1].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
test("DELETE /destroy/:id", (done) => {
request(app)
.delete(`/destroy/${elementId}`)
.expect("Content-Type", /json/)
.expect(200)
.expect((res) => {
res.body.data.length = 1;
res.body.data[0].email = "[email protected]";
})
.end((err, res) => {
if (err) return done(err);
return done();
});
});
});
npm test
command in the terminal, you should get a result similar to this: