So basically, I have this rest api written off Node and Express using Typescript. I am trying to use chai, chai-http and mocha to get the api endpoints tested. But whichever test runs, I always get a 404 not found. Here is my code:
app.ts:
let mongodbURI;
if (process.env.NODE_ENV === "test") {
mongodbURI = process.env.MONGODB_TEST_URI;
} else {
mongodbURI = process.env.MONGODB_URI;
}
mongoose.Promise = global.Promise;
const mongodb = mongoose.connect(mongodbURI, { useMongoClient: true });
mongodb
.then(db => {
setRoutes(app);
if (!module.parent) {
app.listen(app.get("port"), () => { });
}
})
.catch(err => {
console.error(err);
});
export { app };
routes.ts:
export default function setRoutes(app) {
const router = express.Router();
const userCtrl = new UserCtrl();
router.post('/register', userCtrl.createUser);
{ .... }
app.use('/api/v1', router);
}
user.spec.ts:
const should = chai.use(chaiHttp).should();
describe("Users", () => {
it("should create new user", done => {
const user = new User({
name: "testuser",
email: "testuser@example.com",
mobile: "1234567890",
password: "test1234"
});
chai
.request(app)
.post("/api/v1/register")
.send(user)
.end((err, res) => {
res.should.have.status(200);
done();
});
});
});
Not alone this but any route I try, I get a 404 error. What am I doing wrong here?
EDIT: Hope it doesn't matter but I use mocha for running tests.