Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("should do something", async () => {
// give the locals map to the invoke method
const instance: MyCtrl = await TestContext.invoke(MyCtrl, [{
provide: DbService,
use: {
getData: () => {
return "test";
}
}
}]);
// and test it
expect(!!instance).to.eq(true);
expect(instance.getData()).to.equals("test");
});
});
describe("Mock dependencies", () => {
// bootstrap your Server to load all endpoints before run your test
before(bootstrap(FakeServer));
after(TestContext.reset);
it("should do something", async () => {
// give the locals map to the invoke method
const instance: MyCtrl = await TestContext.invoke(MyCtrl, [{
provide: DbService,
use: {
getData: () => {
return "test";
}
}
}]);
// and test it
expect(!!instance).to.eq(true);
expect(instance.getData()).to.equals("test");
describe("get()", () => {
it("should return value stored in memoryStorage", inject([MemoryStorage], (memoryStorage: MemoryStorage) => {
// GIVEN
memoryStorage.set("key", "value");
// WHEN
memoryStorage.get("key").should.eq("value");
}));
});
});
it("should get the service from InjectorService", async () => {
// GIVEN
const memoryStorage = {
set: () => {
},
get: () => {
}
};
// WHEN
const calendarsService: CalendarsService = await TestContext.invoke(CalendarsService, [
{provide: MemoryStorage, use: memoryStorage}
]);
// THEN
expect(calendarsService).to.be.an.instanceof(CalendarsService);
// @ts-ignore
expect(calendarsService.memoryStorage).to.equal(memoryStorage);
});
});
it("should invoke a calendarService with a fake memoryStorage", () => {
// GIVEN
const memoryStorage = {
set: () => {
},
get: () => {
}
};
// WHEN
const calendarsService = TestContext.invoke(CalendarsService, [
{provide: MemoryStorage, use: memoryStorage}
]);
// THEN
expect(calendarsService.memoryStorage).to.equal(memoryStorage);
expect(calendarsService).to.be.an.instanceof(CalendarsService);
});
});
it("should return a result from mocked service", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().rejects({id: "1"})
};
const calendarsCtrl = await TestContext.invoke(CalendarsCtrl, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
let actualError;
try {
await calendarsCtrl.get("1");
} catch (er) {
actualError = er;
}
// THEN
actualError.should.be.instanceof(NotFound);
calendarsService.find.should.be.calledWithExactly("1");
});
it("should return a result from mocked service", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().resolves({id: "1"})
};
const calendarsCtrl = await TestContext.invoke(CalendarsCtrl, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
const result = await calendarsCtrl.get("1");
// THEN
result.should.deep.equal({id: "1"});
calendarsService.find.should.be.calledWithExactly("1");
calendarsCtrl.should.be.an.instanceof(CalendarsCtrl);
calendarsCtrl.calendarsService.should.deep.equal(calendarsService);
});
});
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);
});
});
it("should throw an error", async () => {
// GIVEN
const calendarsService = {
find: Sinon.stub().resolves()
};
const middleware: CheckCalendarIdMiddleware = await TestContext.invoke(CheckCalendarIdMiddleware, [{
provide: CalendarsService,
use: calendarsService
}]);
// WHEN
let actualError;
try {
await middleware.use("1");
} catch (er) {
actualError = er;
}
// THEN
// @ts-ignore
calendarsService.find.should.be.calledWithExactly("1");
actualError.should.instanceOf(NotFound);
actualError.message.should.eq("Calendar not found");
it("should return a result from mocked service", async () => {
// GIVEN
const eventsCtrl: EventsCtrl = await TestContext.invoke(EventsCtrl, []);
const calendarId = "2";
const startDate = "startDate";
const endDate = "endDate";
const name = "name";
const id = "7";
// WHEN
const result = await eventsCtrl.update(calendarId, id, startDate, endDate, name);
// THEN
result.should.deep.equal({
"calendarId": "2",
"endDate": "name",
"id": "7",
"name": "name",
"startDate": "name",