Skip to content

Commit 557b68c

Browse files
author
Manoj Bisht
committed
Completing refactoring controller tests
1 parent 7b34d7b commit 557b68c

File tree

5 files changed

+33
-75
lines changed

5 files changed

+33
-75
lines changed

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ module.exports = {
1616
testEnvironment: 'node',
1717
// The glob patterns Jest uses to detect test files
1818
testMatch: [
19-
'**/test/controllers/bill-runs.controller.test.js',
20-
'**/test/controllers/billing-accounts.controller.test.js',
19+
20+
'**/test/controllers/*.test.js',
21+
2122
'**/test/errors/*.test.js',
2223
'**/test/models/**/*.test.js'
2324
],

test/controllers/check.controller.test.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
'use strict'
22

33
// Test framework dependencies
4-
5-
const Sinon = require('sinon')
6-
74
// Things we need to stub
85
const TwoPartService = require('../../app/services/check/two-part.service.js')
96

107
// For running our service
118
const { init } = require('../../app/server.js')
129

13-
const { describe, beforeEach, afterEach, it, expect } = require('jest') // Importing from Jest
14-
1510
describe('Check controller', () => {
1611
let server
1712

@@ -21,16 +16,11 @@ describe('Check controller', () => {
2116

2217
// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
2318
// possible
24-
Sinon.stub(server.logger, 'error')
19+
server.logger.error = jest.fn().mockResolvedValue()
2520

2621
// We silence sending a notification to our Errbit instance using Airbrake
27-
Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
22+
server.app.airbrake.notify = jest.fn().mockResolvedValue()
2823
})
29-
30-
afterEach(() => {
31-
Sinon.restore()
32-
})
33-
3424
describe('GET /check/two-part', () => {
3525
const options = {
3626
method: 'GET',
@@ -39,7 +29,7 @@ describe('Check controller', () => {
3929

4030
describe('when the request succeeds', () => {
4131
beforeEach(async () => {
42-
Sinon.stub(TwoPartService, 'go').resolves({ regionName: 'Fantasia' })
32+
jest.spyOn(TwoPartService, 'go').mockResolvedValue({ regionName: 'Fantasia' })
4333
})
4434

4535
it('displays the correct message', async () => {
@@ -55,7 +45,7 @@ describe('Check controller', () => {
5545
describe('when the request fails', () => {
5646
describe('because the TwoPartService errors', () => {
5747
beforeEach(async () => {
58-
Sinon.stub(TwoPartService, 'go').rejects()
48+
jest.spyOn(TwoPartService, 'go').mockRejectedValue()
5949
})
6050

6151
it('returns a 500 status', async () => {

test/controllers/data.controller.test.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use strict'
22

3-
// Test framework dependencies
4-
const Lab = require('@hapi/lab')
5-
const Code = require('@hapi/code')
6-
const Sinon = require('sinon')
7-
8-
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
9-
const { expect } = Code
10-
113
// Things we need to stub
124
const ExportService = require('../../app/services/data/export/export.service.js')
135
const MockService = require('../../app/services/data/mock/mock.service.js')
@@ -26,14 +18,9 @@ describe('Data controller', () => {
2618

2719
// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
2820
// possible
29-
Sinon.stub(server.logger, 'error')
30-
21+
server.logger.error = jest.fn().mockResolvedValue()
3122
// We silence sending a notification to our Errbit instance using Airbrake
32-
Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
33-
})
34-
35-
afterEach(() => {
36-
Sinon.restore()
23+
server.app.airbrake.notify = jest.fn().mockResolvedValue()
3724
})
3825

3926
describe('GET /data/export', () => {
@@ -44,13 +31,13 @@ describe('Data controller', () => {
4431

4532
describe('when the request succeeds', () => {
4633
beforeEach(async () => {
47-
Sinon.stub(ExportService, 'go').resolves()
34+
jest.spyOn(ExportService, 'go').mockResolvedValue()
4835
})
4936

5037
it('displays the correct message', async () => {
5138
const response = await server.inject(options)
5239

53-
expect(response.statusCode).to.equal(204)
40+
expect(response.statusCode).toEqual(204)
5441
})
5542
})
5643
})
@@ -63,27 +50,27 @@ describe('Data controller', () => {
6350

6451
describe('when the request succeeds', () => {
6552
beforeEach(async () => {
66-
Sinon.stub(MockService, 'go').resolves({ data: 'mock' })
53+
jest.spyOn(MockService, 'go').mockResolvedValue({ data: 'mock' })
6754
})
6855

6956
it('displays the correct message', async () => {
7057
const response = await server.inject(options)
7158

72-
expect(response.statusCode).to.equal(200)
59+
expect(response.statusCode).toEqual(200)
7360
// TODO: test the response object
7461
})
7562
})
7663

7764
describe('when the request fails', () => {
7865
describe('because the MockService errors', () => {
7966
beforeEach(async () => {
80-
Sinon.stub(MockService, 'go').rejects()
67+
jest.spyOn(MockService, 'go').mockRejectedValue()
8168
})
8269

8370
it('returns a 500 status', async () => {
8471
const response = await server.inject(options)
8572

86-
expect(response.statusCode).to.equal(500)
73+
expect(response.statusCode).toEqual(500)
8774
})
8875
})
8976
})
@@ -97,26 +84,25 @@ describe('Data controller', () => {
9784

9885
describe('when the request succeeds', () => {
9986
beforeEach(async () => {
100-
Sinon.stub(SeedService, 'go').resolves()
87+
jest.spyOn(SeedService, 'go').mockResolvedValue()
10188
})
10289

10390
it('displays the correct message', async () => {
10491
const response = await server.inject(options)
105-
106-
expect(response.statusCode).to.equal(204)
92+
expect(response.statusCode).toEqual(204)
10793
})
10894
})
10995

11096
describe('when the request fails', () => {
11197
describe('because the SeedService errors', () => {
11298
beforeEach(async () => {
113-
Sinon.stub(SeedService, 'go').rejects()
99+
jest.spyOn(SeedService, 'go').mockRejectedValue()
114100
})
115101

116102
it('returns a 500 status', async () => {
117103
const response = await server.inject(options)
118104

119-
expect(response.statusCode).to.equal(500)
105+
expect(response.statusCode).toEqual(500)
120106
})
121107
})
122108
})
@@ -130,26 +116,26 @@ describe('Data controller', () => {
130116

131117
describe('when the request succeeds', () => {
132118
beforeEach(async () => {
133-
Sinon.stub(TearDownService, 'go').resolves()
119+
jest.spyOn(TearDownService, 'go').mockResolvedValue()
134120
})
135121

136122
it('returns a 204 status', async () => {
137123
const response = await server.inject(options)
138124

139-
expect(response.statusCode).to.equal(204)
125+
expect(response.statusCode).toEqual(204)
140126
})
141127
})
142128

143129
describe('when the request fails', () => {
144130
describe('because the TearDownService errors', () => {
145131
beforeEach(async () => {
146-
Sinon.stub(TearDownService, 'go').rejects()
132+
jest.spyOn(TearDownService, 'go').mockRejectedValue()
147133
})
148134

149135
it('returns a 500 status', async () => {
150136
const response = await server.inject(options)
151137

152-
expect(response.statusCode).to.equal(500)
138+
expect(response.statusCode).toEqual(500)
153139
})
154140
})
155141
})

test/controllers/health.controller.test.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use strict'
22

3-
// Test framework dependencies
4-
const Lab = require('@hapi/lab')
5-
const Code = require('@hapi/code')
6-
const Sinon = require('sinon')
7-
8-
const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
9-
const { expect } = Code
10-
113
// Things we need to stub
124
const DatabaseHealthCheckService = require('../../app/services/health/database-health-check.service.js')
135
const InfoService = require('../../app/services/health/info.service.js')
@@ -25,14 +17,10 @@ describe('Health controller', () => {
2517

2618
// We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as
2719
// possible
28-
Sinon.stub(server.logger, 'error')
20+
server.logger.error = jest.fn().mockResolvedValue()
2921

3022
// We silence sending a notification to our Errbit instance using Airbrake
31-
airbrakeStub = Sinon.stub(server.app.airbrake, 'notify').resolvesThis()
32-
})
33-
34-
afterEach(() => {
35-
Sinon.restore()
23+
airbrakeStub = jest.spyOn(server.app.airbrake, 'notify').mockResolvedValue(true)
3624
})
3725

3826
describe('GET /health/airbrake', () => {
@@ -44,13 +32,13 @@ describe('Health controller', () => {
4432
it('returns a 500 error', async () => {
4533
const response = await server.inject(options)
4634

47-
expect(response.statusCode).to.equal(500)
35+
expect(response.statusCode).toEqual(500)
4836
})
4937

5038
it('causes Airbrake to send a notification', async () => {
5139
await server.inject(options)
5240

53-
expect(airbrakeStub.called).to.equal(true)
41+
expect(airbrakeStub).toHaveBeenCalled()
5442
})
5543
})
5644

@@ -62,13 +50,13 @@ describe('Health controller', () => {
6250

6351
describe('when the request succeeds', () => {
6452
beforeEach(async () => {
65-
Sinon.stub(DatabaseHealthCheckService, 'go').resolves()
53+
jest.spyOn(DatabaseHealthCheckService, 'go').mockResolvedValue()
6654
})
6755

6856
it('returns stats about each table', async () => {
6957
const response = await server.inject(options)
7058

71-
expect(response.statusCode).to.equal(200)
59+
expect(response.statusCode).toEqual(200)
7260
})
7361
})
7462
})
@@ -81,7 +69,7 @@ describe('Health controller', () => {
8169

8270
describe('when the request succeeds', () => {
8371
beforeEach(async () => {
84-
Sinon.stub(InfoService, 'go').resolves({
72+
jest.spyOn(InfoService, 'go').mockResolvedValue({
8573
virusScannerData: 'ClamAV 0.103.6/26738/Fri Dec 2 11:12:06 2022',
8674
redisConnectivityData: 'ERROR: Command failed: redis-server --version /bin/sh: 1: redis-server: not found',
8775
addressFacadeData: 'hola',
@@ -98,7 +86,7 @@ describe('Health controller', () => {
9886
it('returns stats about each table', async () => {
9987
const response = await server.inject(options)
10088

101-
expect(response.statusCode).to.equal(200)
89+
expect(response.statusCode).toEqual(200)
10290
})
10391
})
10492
})

test/controllers/root.controller.test.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
'use strict'
22

3-
// Test framework dependencies
4-
const Lab = require('@hapi/lab')
5-
const Code = require('@hapi/code')
6-
7-
const { describe, it, beforeEach } = exports.lab = Lab.script()
8-
const { expect } = Code
9-
103
// For running our service
114
const { init } = require('../../app/server.js')
125

@@ -27,7 +20,7 @@ describe('Root controller: GET /', () => {
2720
const response = await server.inject(options)
2821
const payload = JSON.parse(response.payload)
2922

30-
expect(response.statusCode).to.equal(200)
31-
expect(payload.status).to.equal('alive')
23+
expect(response.statusCode).toEqual(200)
24+
expect(payload.status).toEqual('alive')
3225
})
3326
})

0 commit comments

Comments
 (0)