After successfully creating my first server using Express in TypeScript, I decided to test the routes in the app.
import app from './Server'
const server = app.listen(8080, '0.0.0.0', () => {
console.log("Server is listening on standard port 80...");
});
export default server;
Now, let's test the routes:
import express from 'express';
import * as bodyParser from "body-parser";
const app = express();
app.use(bodyParser.json());
app.get("/", (req: express.Request, res: express.Response) => {
res.status(200).send("SUCCESS");
});
export default app;
I wrote a test script for this:
import * as chai from 'chai';
import chaiHttp = require('chai-http');
chai.use(chaiHttp);
import server from '../src';
describe("LogAPI", () => {
describe('Base express tests', () => {
it("Should return 'SUCCESS' if GET /", async () => {
return chai.request(server).get("/").then(res => {
chai.expect(res.body).to.equal("SUCCESS");
})
});
it("Should return status-code 200 by calling GET /", async () => {
return chai.request(server).get("/").then(res => {
chai.expect(res.status).to.equal(200);
})
});
});
});
However, when trying to run the test with the command:
mocha --require ts-node/register ./../test/**/*.ts
I encountered an error message that says:
/Users/.../NotificationService/src/Server/index.js:5 var app = express_1.default(); ^ TypeError: express_1.default is not a function at Object. (/Users/.../NotificationService/src/Server/inde> x.js:5:28)
Despite the successful functioning of the server, how can I fix this testing issue?
Update 1:
By removing the default()
method from the compiled code, I was able to resolve some issues.
Subsequently, I encountered another error:
/Users/.../NotificationService/test/node_modules/@types/chai-http/index.d.ts:13 import * as request from 'superagent'; SyntaxError: Unexpected token import
Update 2: Here is my ts-config.json file:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./../out“,
"strict": true,
"esModuleInterop": true
}
}