Open voiddeveloper opened 4 years ago
Having the exact same issue...
@voiddeveloper Your custom .listen
method needs to return the *express application**.
im having the same issue but with e2e test... any news?
i have this issue with e2e test too!
@voiddeveloper I was also facing the same issue, this should work
posts.spec.ts
import * as request from "supertest";
import App from '../app';
import PostsRouter from '../routes/posts';
// This will return the express application
const api = request(new App([
new PostsRouter(),
],
3000).app);
describe("GET /posts", () => {
it("respond with json", async done => {
await api
.get("/posts")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200, done);
});
});
I have this same issue too..
@voiddeveloper Your custom
.listen
method needs to return the express application*.
Is there any method to use supertest without returning express application?
It can be fixed by giving "http.Server" type parameter to request function. therefore, app.ts should return http.Server to solve that problem. @voiddeveloper
I have the same issue because i forgot to mention module.exports = app
in app.js but my folder structure is different
here is my folder structure
app.js
const http = require("http");
const express = require("express");
const app = express();
const server = http.Server(app);
app.post("/example", function (request, response) {
// my router logic
});
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", () => {
console.log("Server running");
});
module.exports = app
test/app.test.js
const app = require("../app.js"); // runs app.js
const request = require("supertest"); // needed to make API requests
describe("When the example router is running", () => {
// Add individual test cases
});
To solve this you need to pass a string to the request method... something like this:
import * as request from "supertest";
import { server } from "../server";
const API = 'http://localhost:3000';
describe("GET /posts", () => {
it("respond with json", async done => {
await request(API)
.get("/posts")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200, done);
});
});
I created an express.js server with ts and tried to test it, but got an error. I can't find a solution no matter how I search.
server.ts
app.ts
posts.spec.ts