Skip to content

Commit

Permalink
fix: Fix TestContext signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis committed Apr 21, 2020
1 parent f37fc6d commit c1af938
Show file tree
Hide file tree
Showing 10 changed files with 1,024 additions and 728 deletions.
13 changes: 13 additions & 0 deletions examples/getting-started/.mocharc.js
@@ -0,0 +1,13 @@
module.exports = {
require: [
"ts-node/register/transpile-only",
"tsconfig-paths/register",
"scripts/mocha/register"
],
recursive: true,
reporter: "dot",
spec: [
"src/**/*.spec.ts",
"test/**/*.spec.ts"
]
};
2 changes: 0 additions & 2 deletions examples/getting-started/src/Server.ts
Expand Up @@ -6,7 +6,6 @@ import * as cookieParser from "cookie-parser";
import * as methodOverride from "method-override";
import * as cors from "cors";
import {join} from "path";
import {RestCtrl} from "./controllers/RestCtrl";

const rootDir = __dirname;

Expand All @@ -22,7 +21,6 @@ const rootDir = __dirname;
},
mount: {
"/rest": [
RestCtrl, // Manual import (remove it)
`${rootDir}/controllers/**/*.ts` // Automatic Import, /!\ doesn't works with webpack/jest, use require.context() or manual import instead
]
},
Expand Down
12 changes: 0 additions & 12 deletions examples/getting-started/src/controllers/RestCtrl.spec.ts

This file was deleted.

14 changes: 0 additions & 14 deletions examples/getting-started/src/controllers/RestCtrl.ts

This file was deleted.

84 changes: 84 additions & 0 deletions examples/getting-started/test/integration/example.spec.ts
@@ -0,0 +1,84 @@
import * as SuperTest from "supertest";
import {Controller, ExpressApplication, Get, PathParams, Post, Res} from "@tsed/common";
import {inject, TestContext} from "@tsed/testing";
import {expect} from "chai";
import {Server} from "../../src/Server";

@Controller("/")
export class PathParamsCtrl {
@Get("/scenario1/:scope/:scopeId")
async testScenario1(@PathParams("scope") scope: string) {
// Here scope will be {0: 'a', 1: 'b', 2: 'c'} instead of 'abc' in version 5.47.0
expect(scope).to.eq("abc");

return scope;
}

@Get("/scenario2/:scope/:scopeId")
async testScenario2(@PathParams("scope") scope: any) {
// This way it works in version 5.47.0
expect(scope).to.eq("abc");

return scope;
}

@Post("/scenario3/:scope/:scopeId")
async testScenario3(
@PathParams("scope") scope: string
): Promise<any[]> {
expect(scope).to.eq("abc");

// Here the function will return {0: 'a', 1: 'b', 2: 'c'} instead of ['a','b','c'] in version 5.44.13
return ["a", "b", "c"];
}

@Post("/scenario4/:scope/:scopeId")
async testScenario4(
@PathParams("scope") scope: string,
@Res() response: any
): Promise<any[]> {
expect(scope).to.eq("abc");

// This way it works in version 5.44.13
return response.json(["a", "b", "c"]);
}
}

describe("PathParams", () => {
let request: SuperTest.SuperTest<SuperTest.Test>;
before(TestContext.bootstrap(Server, {
mount: {
"/rest": PathParamsCtrl
}
}));
before(
inject([ExpressApplication], (expressApplication: ExpressApplication) => {
request = SuperTest(expressApplication);
})
);
after(TestContext.reset);

it("scenario 1: GET /rest/scope/scopeId", async () => {
const response = await request.get("/rest/scenario1/abc/1").expect(200);

response.text.should.be.deep.equal("abc");
});

it("scenario 2: GET /rest/scope/scopeId", async () => {
const response = await request.get("/rest/scenario2/abc/1").expect(200);

response.text.should.be.deep.equal("abc");
});

it("scenario 3: POST /rest/scope/scopeId", async () => {
const response = await request.post("/rest/scenario3/abc/1").expect(200);

response.body.should.be.deep.equal(["a", "b", "c"]);
});

it("scenario 4: POST /rest/scope/scopeId", async () => {
const response = await request.post("/rest/scenario4/abc/1").expect(200);

response.body.should.be.deep.equal(["a", "b", "c"]);
});
});
4 changes: 0 additions & 4 deletions examples/getting-started/test/mocha.opts

This file was deleted.

0 comments on commit c1af938

Please sign in to comment.