Skip to content

Commit

Permalink
fix(test): Fix TestContext.create issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Aug 21, 2019
1 parent f639e74 commit fe68bf3
Show file tree
Hide file tree
Showing 25 changed files with 491 additions and 552 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as SuperTest from "supertest";
import {Server} from "../../../../src/Server";

describe("Calendars", () => {
let request;
let request: SuperTest.SuperTest<SuperTest.Test>;
// bootstrap your expressApplication in first
before(TestContext.bootstrap(Server));
before(TestContext.inject([ExpressApplication], (expressApplication: ExpressApplication) => {
Expand Down
1 change: 0 additions & 1 deletion integration/getting-started/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
--require ts-node/register
--require tsconfig-paths/register
--require scripts/mocha/register
--bail
{src,test}/**/*.spec.ts
1 change: 1 addition & 0 deletions integration/mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"concurrently": "^4.0.1",
"cross-env": "5.2.0",
"mocha": "^5.2.0",
"nodemon": "1.19.1",
"nyc": "^14.1.1",
Expand Down
12 changes: 10 additions & 2 deletions integration/mongoose/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,14 @@ create-error-class@^3.0.0:
dependencies:
capture-stack-trace "^1.0.0"

[email protected]:
version "5.2.0"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-5.2.0.tgz#6ecd4c015d5773e614039ee529076669b9d126f2"
integrity sha512-jtdNFfFW1hB7sMhr/H6rW1Z45LFqyI431m3qU6bFXcQ3Eh7LtBuG3h74o7ohHZ3crrRkkqHlo4jYHFPcjroANg==
dependencies:
cross-spawn "^6.0.5"
is-windows "^1.0.0"

cross-spawn@^4:
version "4.0.2"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41"
Expand All @@ -952,7 +960,7 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.0:
cross-spawn@^6.0.0, cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
integrity sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==
Expand Down Expand Up @@ -1823,7 +1831,7 @@ is-stream@^1.0.0, is-stream@^1.0.1, is-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"

is-windows@^1.0.2:
is-windows@^1.0.0, is-windows@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"

Expand Down
10 changes: 5 additions & 5 deletions integration/passportjs/.nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"node_modules",
"**/index.ts",
"**/interfaces/**",
"**/*.spec.ts",
"src/test/tools.js"
"**/*.spec.ts"
],
"extension": [
".ts"
],
"require": [],
"reporter": [
"text-summary",
"html",
"lcov"
],
"extension": [
".ts"
],
"check-coverage": true,
"lines": 99,
"statements": 100,
Expand Down
16 changes: 9 additions & 7 deletions integration/passportjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"scripts": {
"clean": "rimraf '{src,test}/**/*.{js,js.map}'",
"build": "npm run tsc",
"test": "npm run test:lint && npm run test:unit",
"test:unit": "cross-env NODE_ENV=test nyc --reporter=html --reporter=text mocha --recursive",
"test": "npm run clean && npm run test:lint && npm run test:coverage",
"test:unit": "cross-env NODE_ENV=test mocha",
"test:coverage": "cross-env NODE_ENV=test nyc mocha",
"test:lint": "tslint --project tsconfig.json",
"test:lint:fix": "tslint --project tsconfig.json --fix",
"travis:deploy-once": "travis-deploy-once",
Expand All @@ -20,11 +21,11 @@
"author": "",
"license": "MIT",
"dependencies": {
"@tsed/common": "5.27.1",
"@tsed/core": "5.27.1",
"@tsed/di": "5.27.1",
"@tsed/swagger": "5.27.1",
"@tsed/testing": "5.27.1",
"@tsed/common": "5.27.2",
"@tsed/core": "5.27.2",
"@tsed/di": "5.27.2",
"@tsed/swagger": "5.27.2",
"@tsed/testing": "5.27.2",
"@types/swagger-schema-official": "^2.0.9",
"body-parser": "^1.18.3",
"compression": "^1.7.1",
Expand All @@ -51,6 +52,7 @@
"@types/sinon": "^7.0.10",
"@types/sinon-chai": "^3.2.2",
"@types/supertest": "^2.0.4",
"cross-env": "5.2.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"mocha": "^6.0.2",
Expand Down
12 changes: 12 additions & 0 deletions integration/passportjs/scripts/mocha/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const Chai = require("chai");
const ChaiAsPromised = require("chai-as-promised");
const SinonChai = require("sinon-chai");

Chai.should();
Chai.use(SinonChai);
Chai.use(ChaiAsPromised);

process.on("unhandledRejection", (reason, p) => {
console.log("Unhandled Rejection at: Promise", p, "reason:", reason);
// application specific logging, throwing an error, or other logic here
});
10 changes: 10 additions & 0 deletions integration/passportjs/src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {GlobalAcceptMimesMiddleware, ServerLoader, ServerSettings} from "@tsed/common";
import "@tsed/swagger";
import {CalendarCtrl} from "./controllers/calendars/CalendarCtrl";
import {PassportCtrl} from "./controllers/passport/PassportCtrl";
import {RestCtrl} from "./controllers/RestCtrl";

const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
Expand All @@ -16,6 +19,13 @@ const rootDir = __dirname;
logRequest: true,
requestFields: ["reqId", "method", "url", "headers", "query", "params", "duration"]
},
mount: {
"/rest": [
RestCtrl,
CalendarCtrl,
PassportCtrl
]
},
swagger: {
path: "/api-docs"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {TestContext} from "@tsed/testing";
import {expect} from "chai";
import * as Sinon from "sinon";
import {CalendarsService} from "../../services/calendars/CalendarsService";
import {MemoryStorage} from "../../services/storage/MemoryStorage";
import {CalendarCtrl} from "./CalendarCtrl";

describe("CalendarsCtrl", () => {
describe("without IOC", () => {
it("should do something", () => {
// GIVEN
const calendarsCtrl = new CalendarCtrl(new CalendarsService(new MemoryStorage()));

expect(calendarsCtrl).to.be.an.instanceof(CalendarCtrl);
});
});

describe("with mocking dependencies", () => {
before(() => TestContext.create());
after(() => TestContext.reset());

it("should return the expected result", () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().returns(Promise.resolve({id: "1"}))
};

const calendarController = TestContext.invoke(CalendarCtrl, [
{provide: CalendarsService, use: calendarsService}
]);

// WHEN
const result = calendarController.get("1");

// THEN
result.should.eventually.deep.equal({id: "1"});
calendarsService.find.should.be.calledWithExactly("1");
expect(calendarController.calendarsService).to.equal(calendarsService);
});
});
});
79 changes: 79 additions & 0 deletions integration/passportjs/src/controllers/calendars/CalendarCtrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
Authenticated,
BodyParams,
Controller,
Delete,
Get,
PathParams,
Post,
Put,
Required,
Status
} from "@tsed/common";
import {NotFound} from "ts-httpexceptions";
import {Calendar} from "../../interfaces/Calendar";
import {CalendarsService} from "../../services/calendars/CalendarsService";
import {EventsCtrl} from "../events/EventsCtrl";

/**
* Add @Controller annotation to declare your class as Router controller.
* The first param is the global path for your controller.
* The others params is the controller dependencies.
*
* In this case, EventsCtrl is a dependency of CalendarsCtrl.
* All routes of EventsCtrl will be mounted on the `/calendars` path.
*/
@Controller("/calendars", EventsCtrl)
export class CalendarCtrl {

constructor(private calendarsService: CalendarsService) {

}

@Get("/:id")
async get(@Required() @PathParams("id") id: string): Promise<Calendar> {

const calendar = await this.calendarsService.find(id);

if (calendar) {
return calendar;
}

throw new NotFound("Calendar not found");
}

@Put("/")
save(@BodyParams("name") name: string) {
return this.calendarsService.create(name);
}

/**
*
* @param id
* @param name
* @returns {Promise<Calendar>}
*/
@Post("/:id")
async update(@PathParams("id") @Required() id: string,
@BodyParams("name") @Required() name: string): Promise<Calendar> {
return this.calendarsService.update({id, name});
}

/**
*
* @param id
* @returns {{id: string, name: string}}
*/
@Delete("/")
@Authenticated()
@Status(204)
async remove(@BodyParams("id") @Required() id: string): Promise<void> {
this.calendarsService.remove(id);
}

@Get("/")
@Authenticated()
async getAllCalendars(): Promise<Calendar[]> {
return this.calendarsService.query();
}
}

This file was deleted.

Loading

0 comments on commit fe68bf3

Please sign in to comment.