Skip to content
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

Open
Marcus-Rise opened this issue Aug 13, 2020 · 4 comments
Open

Question: how to use with supertest #54

Marcus-Rise opened this issue Aug 13, 2020 · 4 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@Marcus-Rise
Copy link

Marcus-Rise commented Aug 13, 2020

  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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

next.config.js

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

@Marcus-Rise
Copy link
Author

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

@kyle-mccarthy kyle-mccarthy added question Further information is requested help wanted Extra attention is needed labels Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants