nest-next icon indicating copy to clipboard operation
nest-next copied to clipboard

Question: how to use with supertest

Open Marcus-Rise opened this issue 4 years ago • 4 comments

  console.log node_modules/next/dist/build/output/log.js:1
    info  - Using external babel configuration from /home/ilya/WebstormProjects/timesheet/app/src/client/.babelrc

  console.log node_modules/next/dist/build/output/log.js:1
    event - compiled successfully


TypeError: Cannot set property 'render' of undefined

    at RenderService.bindHttpServer (/home/ilya/WebstormProjects/timesheet/app/node_modules/nest-next/dist/render.service.js:79:23)
    at Function.init (/home/ilya/WebstormProjects/timesheet/app/node_modules/nest-next/dist/render.service.js:19:14)
    at InstanceWrapper.useFactory [as metatype] (/home/ilya/WebstormProjects/timesheet/app/node_modules/nest-next/dist/render.module.js:45:67)
    at Injector.instantiateClass (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/injector.js:293:55)
    at callback (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/injector.js:77:41)
    at Injector.resolveConstructorParams (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/injector.js:118:24)
    at Injector.loadInstance (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/injector.js:81:9)
    at Injector.loadProvider (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/injector.js:38:9)
    at async Promise.all (index 3)
    at InstanceLoader.createInstancesOfProviders (/home/ilya/WebstormProjects/timesheet/app/node_modules/@nestjs/core/injector/instance-loader.js:43:9)

Marcus-Rise avatar Aug 13 '20 14:08 Marcus-Rise

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);
                });
        });
    });
});

Marcus-Rise avatar Aug 13 '20 14:08 Marcus-Rise

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: "",
        },
    );
};

Marcus-Rise avatar Aug 13 '20 14:08 Marcus-Rise

next.config.js

module.exports = {
    reactStrictMode: true,
    distDir: process.env.NODE_ENV === "production" ? "../../dist/client/.next" : undefined,
};

Marcus-Rise avatar Aug 13 '20 14:08 Marcus-Rise

Only works deprecated config

bootstrap.ts

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);
                });
        });
    });
});

Marcus-Rise avatar Aug 14 '20 05:08 Marcus-Rise