diff --git a/jest.config.js b/jest.config.js index b25430910a..7f1a9a575e 100644 --- a/jest.config.js +++ b/jest.config.js @@ -16,8 +16,9 @@ module.exports = { testEnvironment: 'node', // The glob patterns Jest uses to detect test files testMatch: [ - '**/test/controllers/bill-runs.controller.test.js', - '**/test/controllers/billing-accounts.controller.test.js', + + '**/test/controllers/*.test.js', + '**/test/errors/*.test.js', '**/test/models/**/*.test.js' ], diff --git a/test/controllers/check.controller.test.js b/test/controllers/check.controller.test.js index 55dcd0e2eb..03a69ecca0 100644 --- a/test/controllers/check.controller.test.js +++ b/test/controllers/check.controller.test.js @@ -1,17 +1,12 @@ 'use strict' // Test framework dependencies - -const Sinon = require('sinon') - // Things we need to stub const TwoPartService = require('../../app/services/check/two-part.service.js') // For running our service const { init } = require('../../app/server.js') -const { describe, beforeEach, afterEach, it, expect } = require('jest') // Importing from Jest - describe('Check controller', () => { let server @@ -21,16 +16,11 @@ describe('Check controller', () => { // We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as // possible - Sinon.stub(server.logger, 'error') + server.logger.error = jest.fn().mockResolvedValue() // We silence sending a notification to our Errbit instance using Airbrake - Sinon.stub(server.app.airbrake, 'notify').resolvesThis() + server.app.airbrake.notify = jest.fn().mockResolvedValue() }) - - afterEach(() => { - Sinon.restore() - }) - describe('GET /check/two-part', () => { const options = { method: 'GET', @@ -39,7 +29,7 @@ describe('Check controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(TwoPartService, 'go').resolves({ regionName: 'Fantasia' }) + jest.spyOn(TwoPartService, 'go').mockResolvedValue({ regionName: 'Fantasia' }) }) it('displays the correct message', async () => { @@ -55,7 +45,7 @@ describe('Check controller', () => { describe('when the request fails', () => { describe('because the TwoPartService errors', () => { beforeEach(async () => { - Sinon.stub(TwoPartService, 'go').rejects() + jest.spyOn(TwoPartService, 'go').mockRejectedValue() }) it('returns a 500 status', async () => { diff --git a/test/controllers/data.controller.test.js b/test/controllers/data.controller.test.js index ab8f90a757..11a8f6e26d 100644 --- a/test/controllers/data.controller.test.js +++ b/test/controllers/data.controller.test.js @@ -1,13 +1,5 @@ 'use strict' -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') -const Sinon = require('sinon') - -const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() -const { expect } = Code - // Things we need to stub const ExportService = require('../../app/services/data/export/export.service.js') const MockService = require('../../app/services/data/mock/mock.service.js') @@ -26,14 +18,9 @@ describe('Data controller', () => { // We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as // possible - Sinon.stub(server.logger, 'error') - + server.logger.error = jest.fn().mockResolvedValue() // We silence sending a notification to our Errbit instance using Airbrake - Sinon.stub(server.app.airbrake, 'notify').resolvesThis() - }) - - afterEach(() => { - Sinon.restore() + server.app.airbrake.notify = jest.fn().mockResolvedValue() }) describe('GET /data/export', () => { @@ -44,13 +31,13 @@ describe('Data controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(ExportService, 'go').resolves() + jest.spyOn(ExportService, 'go').mockResolvedValue() }) it('displays the correct message', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(204) + expect(response.statusCode).toEqual(204) }) }) }) @@ -63,13 +50,13 @@ describe('Data controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(MockService, 'go').resolves({ data: 'mock' }) + jest.spyOn(MockService, 'go').mockResolvedValue({ data: 'mock' }) }) it('displays the correct message', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(200) + expect(response.statusCode).toEqual(200) // TODO: test the response object }) }) @@ -77,13 +64,13 @@ describe('Data controller', () => { describe('when the request fails', () => { describe('because the MockService errors', () => { beforeEach(async () => { - Sinon.stub(MockService, 'go').rejects() + jest.spyOn(MockService, 'go').mockRejectedValue() }) it('returns a 500 status', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(500) + expect(response.statusCode).toEqual(500) }) }) }) @@ -97,26 +84,25 @@ describe('Data controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(SeedService, 'go').resolves() + jest.spyOn(SeedService, 'go').mockResolvedValue() }) it('displays the correct message', async () => { const response = await server.inject(options) - - expect(response.statusCode).to.equal(204) + expect(response.statusCode).toEqual(204) }) }) describe('when the request fails', () => { describe('because the SeedService errors', () => { beforeEach(async () => { - Sinon.stub(SeedService, 'go').rejects() + jest.spyOn(SeedService, 'go').mockRejectedValue() }) it('returns a 500 status', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(500) + expect(response.statusCode).toEqual(500) }) }) }) @@ -130,26 +116,26 @@ describe('Data controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(TearDownService, 'go').resolves() + jest.spyOn(TearDownService, 'go').mockResolvedValue() }) it('returns a 204 status', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(204) + expect(response.statusCode).toEqual(204) }) }) describe('when the request fails', () => { describe('because the TearDownService errors', () => { beforeEach(async () => { - Sinon.stub(TearDownService, 'go').rejects() + jest.spyOn(TearDownService, 'go').mockRejectedValue() }) it('returns a 500 status', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(500) + expect(response.statusCode).toEqual(500) }) }) }) diff --git a/test/controllers/health.controller.test.js b/test/controllers/health.controller.test.js index 884de5e126..7ed6a130d5 100644 --- a/test/controllers/health.controller.test.js +++ b/test/controllers/health.controller.test.js @@ -1,13 +1,5 @@ 'use strict' -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') -const Sinon = require('sinon') - -const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script() -const { expect } = Code - // Things we need to stub const DatabaseHealthCheckService = require('../../app/services/health/database-health-check.service.js') const InfoService = require('../../app/services/health/info.service.js') @@ -25,14 +17,10 @@ describe('Health controller', () => { // We silence any calls to server.logger.error made in the plugin to try and keep the test output as clean as // possible - Sinon.stub(server.logger, 'error') + server.logger.error = jest.fn().mockResolvedValue() // We silence sending a notification to our Errbit instance using Airbrake - airbrakeStub = Sinon.stub(server.app.airbrake, 'notify').resolvesThis() - }) - - afterEach(() => { - Sinon.restore() + airbrakeStub = jest.spyOn(server.app.airbrake, 'notify').mockResolvedValue(true) }) describe('GET /health/airbrake', () => { @@ -44,13 +32,13 @@ describe('Health controller', () => { it('returns a 500 error', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(500) + expect(response.statusCode).toEqual(500) }) it('causes Airbrake to send a notification', async () => { await server.inject(options) - expect(airbrakeStub.called).to.equal(true) + expect(airbrakeStub).toHaveBeenCalled() }) }) @@ -62,13 +50,13 @@ describe('Health controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(DatabaseHealthCheckService, 'go').resolves() + jest.spyOn(DatabaseHealthCheckService, 'go').mockResolvedValue() }) it('returns stats about each table', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(200) + expect(response.statusCode).toEqual(200) }) }) }) @@ -81,7 +69,7 @@ describe('Health controller', () => { describe('when the request succeeds', () => { beforeEach(async () => { - Sinon.stub(InfoService, 'go').resolves({ + jest.spyOn(InfoService, 'go').mockResolvedValue({ virusScannerData: 'ClamAV 0.103.6/26738/Fri Dec 2 11:12:06 2022', redisConnectivityData: 'ERROR: Command failed: redis-server --version /bin/sh: 1: redis-server: not found', addressFacadeData: 'hola', @@ -98,7 +86,7 @@ describe('Health controller', () => { it('returns stats about each table', async () => { const response = await server.inject(options) - expect(response.statusCode).to.equal(200) + expect(response.statusCode).toEqual(200) }) }) }) diff --git a/test/controllers/root.controller.test.js b/test/controllers/root.controller.test.js index 69bf8c309d..3a6030f6df 100644 --- a/test/controllers/root.controller.test.js +++ b/test/controllers/root.controller.test.js @@ -1,12 +1,5 @@ 'use strict' -// Test framework dependencies -const Lab = require('@hapi/lab') -const Code = require('@hapi/code') - -const { describe, it, beforeEach } = exports.lab = Lab.script() -const { expect } = Code - // For running our service const { init } = require('../../app/server.js') @@ -27,7 +20,7 @@ describe('Root controller: GET /', () => { const response = await server.inject(options) const payload = JSON.parse(response.payload) - expect(response.statusCode).to.equal(200) - expect(payload.status).to.equal('alive') + expect(response.statusCode).toEqual(200) + expect(payload.status).toEqual('alive') }) })