-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: how to use with supertest #54
Labels
Comments
import { NestExpressApplication } from "@nestjs/platform-express";
import { Test, TestingModule } from "@nestjs/testing";
import { TaskModule } from "./task.module";
import { bootstrap } from "../bootstrap";
import request from "supertest";
import { BAD_REQUEST, MOVED_TEMPORARILY, OK } from "http-status-codes";
import { encodeRequestQueryParams } from "../helpers";
import { LOGIN_REQUEST_URL } from "../auth/LoginRequest";
import { MAIN_PAGE_URL } from "../../client/dto/MainPage";
import { renderInit } from "../render.module";
describe("TaskModule", () => {
let app: NestExpressApplication;
jest.setTimeout(30000);
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [renderInit(), TaskModule],
}).compile();
app = module.createNestApplication();
await bootstrap(app);
await app.init();
});
describe("main page", () => {
test("unauthorized", (done) => {
return request(app.getHttpServer()).get(MAIN_PAGE_URL).expect(MOVED_TEMPORARILY, done);
});
test("ok", (done) => {
const urlParams = encodeRequestQueryParams([
{ key: "code", value: "1" },
{ key: "state", value: "2" },
{ key: "domain", value: "3" },
{ key: "member_id", value: "4" },
{ key: "scope", value: "5" },
{ key: "server_domain", value: "6" },
]);
return request(app.getHttpServer())
.get(`${LOGIN_REQUEST_URL}${urlParams}`)
.expect(MOVED_TEMPORARILY)
.then((res) => {
const cookies = res.header["set-cookie"];
request(app.getHttpServer()).get(MAIN_PAGE_URL).set("Cookie", cookies).expect(OK, done);
});
});
});
}); |
import { RenderModule } from "nest-next";
import Next from "next";
import { resolve } from "path";
export const renderInit = () => {
const dev = process.env.NODE_ENV !== "production";
return RenderModule.forRootAsync(
Next({
dev,
dir: dev ? resolve(__dirname, "../client") : resolve(__dirname, "../../dist/client"),
}),
{
dev,
viewsDir: "",
},
);
}; |
module.exports = {
reactStrictMode: true,
distDir: process.env.NODE_ENV === "production" ? "../../dist/client/.next" : undefined,
}; |
Only works deprecated config
import { NestExpressApplication } from "@nestjs/platform-express";
import cookieParser from "cookie-parser";
import morgan from "morgan";
import helmet from "helmet";
import cors from "cors";
import csurf from "csurf";
import Next from "next";
import { RenderModule } from "nest-next";
import { resolve } from "path";
export const bootstrap = async (server: NestExpressApplication): Promise<void> => {
const dev = process.env.NODE_ENV !== "production";
const app = Next({
dev,
dir: dev ? resolve(__dirname, "../client") : resolve(__dirname, "../../dist/client"),
});
await app.prepare();
const renderer = server.get(RenderModule);
renderer.register(server, app);
server.use(cookieParser(process.env.COOKIE_SECRET || "COOKIE_SECRET"));
server.use(csurf({ cookie: true }));
switch (process.env.NODE_ENV) {
case "development":
server.use(morgan("dev"));
break;
case "test":
server.use(morgan("tiny"));
break;
case "production":
server.use(
morgan("combined", {
skip: (req, res) => res.statusCode < 400,
}),
);
server.use(helmet());
server.use(cors());
break;
}
}; import { NestExpressApplication } from "@nestjs/platform-express";
import { Test, TestingModule } from "@nestjs/testing";
import { TaskModule } from "./task.module";
import { bootstrap } from "../bootstrap";
import request from "supertest";
import { MOVED_TEMPORARILY, OK } from "http-status-codes";
import { encodeRequestQueryParams } from "../helpers";
import { LOGIN_REQUEST_URL } from "../auth/LoginRequest";
import { MAIN_PAGE_URL } from "../../client/dto/MainPage";
import { RenderModule } from "nest-next";
describe("TaskModule", () => {
let app: NestExpressApplication;
jest.setTimeout(30000);
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [RenderModule, TaskModule],
}).compile();
app = module.createNestApplication();
await bootstrap(app);
await app.init();
});
describe("main page", () => {
test("unauthorized", (done) => {
return request(app.getHttpServer()).get(MAIN_PAGE_URL).expect(MOVED_TEMPORARILY, done);
});
test("ok", (done) => {
const urlParams = encodeRequestQueryParams([
{ key: "code", value: "1" },
{ key: "state", value: "2" },
{ key: "domain", value: "3" },
{ key: "member_id", value: "4" },
{ key: "scope", value: "5" },
{ key: "server_domain", value: "6" },
]);
return request(app.getHttpServer())
.get(`${LOGIN_REQUEST_URL}${urlParams}`)
.expect(MOVED_TEMPORARILY)
.then((res) => {
const cookies = res.header["set-cookie"];
request(app.getHttpServer()).get(MAIN_PAGE_URL).set("Cookie", cookies).expect(OK, done);
});
});
});
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
The text was updated successfully, but these errors were encountered: