Never done a Test Driven Development and only Read about it. I thought its about time to learn and try this so I created an empty GitHub Repo node-testing and initialized that as a empty node project commit.
In Test Driven Development We must write the Tests first and then later we start making the app which start fixing the test fails.
// Importing superTest to make Request to the Express App const { expect, assert } = require("chai"); let request = require("supertest"); let {app,server} = require("../index"); // to Have a express app copy.
describe("Index Test", function(){ it("Test The Get Route", asyncfunction(){ let res = awaitrequest(app).get("/"); assert.strictEqual(res.status, 200, 'Status is not 200'); assert.strictEqual(res.text, "URL Shortner UP", 'Body Should be `"URL Shortner UP"`'); }) it("Try Adding a New URL and Test for if That short URL is Redirecting", asyncfunction(){ let res = awaitrequest(app).post("/").send({"url":"http://shubhkumar.in"}); assert.strictEqual(res.status, 200, 'Status is not 200'); assert.hasAllKeys(res.body, ["url","shortURL"], 'Body Should have `url` and `shortURL` in Response'); // Check if that Short URL is Redirecting
res = awaitrequest(app).get(`/${res.body.shortURL}`); assert.strictEqual(res.status, 302, 'Status is not 302'); assert.equal(res.status, 302, 'Status is not 302'); }) })
and updated package json to contain
1 2 3 4
"scripts": { "test": "mocha --exit" } }
and running npm test we get error for the implementation