Skip to content

Commit

Permalink
refactor: Favor use of spies vs. done callback
Browse files Browse the repository at this point in the history
Replaced uses of done callback with async tests and sinon spies for verifing results.  Solves with the done callback where assertions would prevent done from being called and the test would report a timeout error vs the proper test failure.
  • Loading branch information
jrassa committed Oct 2, 2023
1 parent a0e2df6 commit 51b320f
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 390 deletions.
16 changes: 8 additions & 8 deletions src/app/common/util.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ describe('Utils:', () => {
date: { $date: '2015-07-01T00:00:00.000Z' }
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output = utilService.toMongoose(input) as any;
(typeof output.hello).should.equal('object');
output.hello.should.be.a.Object();
output.hello.there.should.equal('you are');
Array.isArray(output.hello.when).should.equal(true);
output.hello.when.should.be.a.Array();
output.hello.when.length.should.equal(3);
(typeof output.hello.when[0]).should.equal('object');
(output.hello.when[0].length == null).should.equal(true);
output.hello.when[0].should.be.a.Object();
output.hello.when[1].something.should.equal(0);
output.hello.when[2].getTime().should.equal(1420070400000);
output.date.getTime().should.equal(1435708800000);
Expand All @@ -44,13 +44,13 @@ describe('Utils:', () => {
obj: { $obj: '000000000000000000000001' }
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const output = utilService.toMongoose(input) as any;
(typeof output.hello).should.equal('object');
output.hello.should.be.a.Object();
output.hello.there.should.equal('you are');
Array.isArray(output.hello.when).should.equal(true);
output.hello.when.should.be.a.Array();
output.hello.when.length.should.equal(3);
(typeof output.hello.when[0]).should.equal('object');
(output.hello.when[0].length == null).should.equal(true);
output.hello.when[0].should.be.a.Object();
output.hello.when[1].something.should.equal(0);
output.hello.when[2]._bsontype.should.equal('ObjectID');
output.hello.when[2]
Expand Down
9 changes: 3 additions & 6 deletions src/app/core/feedback/feedback.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assert, createSandbox, spy, stub } from 'sinon';
import { assert, createSandbox } from 'sinon';

import * as feedbackController from './feedback.controller';
import { Feedback } from './feedback.model';
import feedbackService from './feedback.service';
import { auditService, logger } from '../../../dependencies';
import { getResponseSpy } from '../../../spec/helpers';
import { User } from '../user/user.model';

describe('Feedback Controller2', () => {
Expand All @@ -12,11 +13,7 @@ describe('Feedback Controller2', () => {

beforeEach(() => {
sandbox = createSandbox();
res = {
json: spy(),
status: stub()
};
res.status.returns(res);
res = getResponseSpy();
sandbox.stub(logger, 'error').returns();
});

Expand Down
6 changes: 1 addition & 5 deletions src/app/core/feedback/feedback.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { FilterQuery } from 'mongoose';

import { FeedbackDocument } from './feedback.model';
import feedbackService from './feedback.service';
import {
auditService,
config,
utilService as util
} from '../../../dependencies';
import { auditService, config } from '../../../dependencies';
import { Callbacks } from '../export/callbacks';
import * as exportConfigController from '../export/export-config.controller';
import { IExportConfig } from '../export/export-config.model';
Expand Down
10 changes: 3 additions & 7 deletions src/app/core/teams/teams.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Request } from 'express';
import should from 'should';
import { assert, createSandbox, match, spy, stub } from 'sinon';
import { assert, createSandbox, match, stub } from 'sinon';

import { Team, TeamDocument } from './team.model';
import * as teamsController from './teams.controller';
import teamsService from './teams.service';
import { auditService, logger } from '../../../dependencies';
import { getResponseSpy } from '../../../spec/helpers';
import { User, UserDocument } from '../user/user.model';
import userService from '../user/user.service';

Expand All @@ -18,12 +19,7 @@ describe('Teams Controller:', () => {

beforeEach(() => {
sandbox = createSandbox();
res = {
json: spy(),
end: spy(),
status: stub()
};
res.status.returns(res);
res = getResponseSpy();
});

afterEach(() => {
Expand Down
9 changes: 3 additions & 6 deletions src/app/core/user/admin/user-admin.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { assert, createSandbox, spy, stub } from 'sinon';
import { assert, createSandbox } from 'sinon';

import * as userAdminController from './user-admin.controller';
import { auditService, config } from '../../../../dependencies';
import { getResponseSpy } from '../../../../spec/helpers';
import userEmailService from '../user-email.service';
import { User } from '../user.model';
import userService from '../user.service';
Expand Down Expand Up @@ -29,11 +30,7 @@ describe('User Admin Controller:', () => {

beforeEach(() => {
sandbox = createSandbox();
res = {
json: spy(),
status: stub()
};
res.status.returns(res);
res = getResponseSpy();
});

afterEach(() => {
Expand Down
Loading

0 comments on commit 51b320f

Please sign in to comment.