Skip to content

Commit

Permalink
chore: mv test-utils away from test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
3imed-jaberi committed Apr 16, 2023
1 parent 9f589fe commit 6101809
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/body-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import parser from 'co-body';
import type * as Koa from 'koa';

import type {BodyParserOptions, BodyType} from './body-parser.types';
import {getIsEnabledBodyAs, getMimeTypes} from './body-parser.utils';

Expand Down
1 change: 1 addition & 0 deletions src/body-parser.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deepMerge from 'lodash.merge';

import {
type BodyParserOptions,
supportedBodyTypes,
Expand Down
25 changes: 4 additions & 21 deletions test/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import path from "path";
import request from "supertest";
import Koa from "koa";

import bodyParser from "../src";
import type { BodyParserOptions } from "../src/body-parser.types";
import { UnsupportedBodyTypeError } from "../src/body-parser.utils";

const fixtures = path.join(__dirname, "fixtures");
type CreateAppConfig = BodyParserOptions & {
rawParsedBody?: Record<string, string>;
};

const createApp = (config: CreateAppConfig = {}) => {
const { rawParsedBody, ...options } = config;
const app = new Koa();
rawParsedBody &&
app.use((ctx, next) => {
ctx.req.body = rawParsedBody;
console.log("==== middelware ====", rawParsedBody);

return next();
});

app.use(bodyParser(options));
return app;
};
import { createApp, fixtures } from "./test-utils";

describe("test/body-parser.test.ts", () => {
let server: ReturnType<ReturnType<typeof createApp>["listen"]>;
Expand All @@ -47,7 +29,9 @@ describe("test/body-parser.test.ts", () => {
expect(ctx.request.rawBody).toEqual('{"foo":"bar"}');
ctx.body = ctx.request.body;
});

server = app.listen();

request(server)
.post("/")
.send({ foo: "bar" })
Expand Down Expand Up @@ -286,7 +270,6 @@ describe("test/body-parser.test.ts", () => {
enableTypes: ["text"],
});
app.use(async (ctx) => {
console.log(ctx.request.body);
expect(ctx.headers["content-type"]).toEqual("text/html");
expect(ctx.request.body).toEqual("<h1>abc</h1>");
expect(ctx.request.rawBody).toEqual("<h1>abc</h1>");
Expand Down
23 changes: 23 additions & 0 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from "path";
import Koa from "koa";

import bodyParser from "../src";
import type { BodyParserOptions } from "../src/body-parser.types";

export const fixtures = path.join(__dirname, "fixtures");
type CreateAppConfig = BodyParserOptions & {
rawParsedBody?: Record<string, string>;
};

export const createApp = (config: CreateAppConfig = {}) => {
const { rawParsedBody, ...options } = config;
const app = new Koa();
rawParsedBody &&
app.use((ctx, next) => {
ctx.req.body = rawParsedBody;
return next();
});

app.use(bodyParser(options));
return app;
};

0 comments on commit 6101809

Please sign in to comment.