Skip to content

Commit

Permalink
Replace Hapi Lab with node-test for test runner
Browse files Browse the repository at this point in the history
DEFRA/water-abstraction-team#130

We as a team would love to move the [water-abstraction-system](https://github.com/DEFRA/water-abstraction-system) project to use [ECMAScript Modules (ESM)](https://nodejs.org/api/esm.html). The one thing that has held us back is our test framework [Hapi Lab](https://hapi.dev/module/lab/).

We've worked hard over the last year restructuring how we build our tests and [moving away from our dependence on the database](DEFRA/water-abstraction-team#124).

This means we are now ready to make the switch to a test framework that natively supports ESM. We've opted for [node-test](https://nodejs.org/api/test.html#test-runner).

In this change we

- remove the **Hapi Lab** dependency
- update all tests to require **node-test**
- update our scripts to use **node-test**
  • Loading branch information
Cruikshanks committed Dec 30, 2024
1 parent c45bd11 commit c7bd75b
Show file tree
Hide file tree
Showing 287 changed files with 1,183 additions and 1,465 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"seed": "knex seed:run --knexfile knexfile.application.js",
"lint": "eslint .",
"pretest": "NODE_ENV=test node db/clean.js",
"test": "lab --silent-skips --shuffle",
"test:skip": "lab --silent-skips --shuffle",
"test": "NODE_ENV=test node --experimental-test-coverage --test-coverage-exclude 'test/**/*.js' --test-coverage-exclude 'db/migrations/**/*.js' --test-coverage-exclude 'db/seeds/**/*.js' --test-coverage-exclude 'app/plugins/**/*.js' --test '**/*.test.js'",
"test:check": "NODE_ENV=test node --experimental-test-coverage --test-coverage-exclude 'test/**/*.js' --test-coverage-exclude 'db/migrations/**/*.js' --test-coverage-exclude 'db/seeds/**/*.js' --test-coverage-exclude 'app/plugins/**/*.js' --test 'test/services/**/*.test.js'",
"test:skip": "NODE_ENV=test node --experimental-test-coverage --test-coverage-exclude 'test/**/*.js' --test-coverage-exclude 'db/migrations/**/*.js' --test-coverage-exclude 'db/seeds/**/*.js' --test-coverage-exclude 'app/plugins/**/*.js' --test '**/*.test.js'",
"test:lcov": "NODE_ENV=test node --experimental-test-coverage --test-coverage-exclude 'test/**/*.js' --test-coverage-exclude 'db/migrations/**/*.js' --test-coverage-exclude 'db/seeds/**/*.js' --test-coverage-exclude 'app/plugins/**/*.js' --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=lcov.info --test '**/*.test.js'",
"postinstall": "npm run build",
"version": "npx --yes auto-changelog -p --commit-limit false && git add CHANGELOG.md"
},
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/bill-licences.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
12 changes: 7 additions & 5 deletions test/controllers/bill-runs-review.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach, after } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { closeConnection } = require('../support/database.js')
const { postRequestOptions } = require('../support/general.js')

// Things we need to stub
Expand Down Expand Up @@ -55,6 +53,10 @@ describe('Bill Runs Review controller', () => {
Sinon.restore()
})

after(async () => {
await closeConnection()
})

describe('/bill-runs/review/{billRunId}', () => {
beforeEach(() => {
path = '97db1a27-8308-4aba-b463-8a6af2558b28'
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/bill-runs-setup.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/bill-runs.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/billing-accounts.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Things we need to stub
const Boom = require('@hapi/boom')
const ChangeAddressService = require('../../app/services/billing-accounts/change-address.service.js')
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/bills.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/data.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/health.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, 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')
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/jobs.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Things we need to stub
const ExportService = require('../../app/services/jobs/export/export.service.js')
const ImportLicence = require('../../app/services/jobs/import/import-licences.service.js')
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/licences.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const Boom = require('@hapi/boom')
const { postRequestOptions } = require('../support/general.js')
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/monitoring-stations.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Things we need to stub
const ViewService = require('../../app/services/monitoring-stations/view.service.js')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
8 changes: 3 additions & 5 deletions test/controllers/notifications-setup.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

// Things we need to stub
Expand Down
7 changes: 2 additions & 5 deletions test/controllers/return-versions-setup.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const { postRequestOptions } = require('../support/general.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/return-versions.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, before, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, before, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Things we need to stub
const ViewService = require('../../app/services/return-versions/view.service.js')

Expand Down
7 changes: 2 additions & 5 deletions test/controllers/root.controller.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, before } = (exports.lab = Lab.script())
const { expect } = Code
const { describe, it, before } = require('node:test')
const { expect } = require('@hapi/code')

// For running our service
const { init } = require('../../app/server.js')
Expand Down
7 changes: 2 additions & 5 deletions test/errors/expanded.error.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'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
const { describe, it, beforeEach } = require('node:test')
const { expect } = require('@hapi/code')

// Thing under test
const ExpandedError = require('../../app/errors/expanded.error.js')
Expand Down
7 changes: 2 additions & 5 deletions test/lib/base-notifier.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Thing under test
const BaseNotifierLib = require('../../app/lib/base-notifier.lib.js')

Expand Down
7 changes: 2 additions & 5 deletions test/lib/boom-notifier.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Thing under test
const BoomNotifierLib = require('../../app/lib/boom-notifier.lib.js')

Expand Down
7 changes: 2 additions & 5 deletions test/lib/dates.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, before } = (exports.lab = Lab.script())
const { expect } = Code
const { describe, it, before } = require('node:test')
const { expect } = require('@hapi/code')

// Thing under test
const DateLib = require('../../app/lib/dates.lib.js')
Expand Down
7 changes: 2 additions & 5 deletions test/lib/general.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, beforeEach, afterEach } = require('node:test')
const { expect } = require('@hapi/code')
const Sinon = require('sinon')

const { describe, it, beforeEach, afterEach } = (exports.lab = Lab.script())
const { expect } = Code

// Test helpers
const TransactionHelper = require('../support/helpers/transaction.helper.js')

Expand Down
7 changes: 2 additions & 5 deletions test/lib/global-notifier.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const { describe, it, beforeEach, afterEach } = require('node:test')
const { expect } = 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 BaseNotifierLib = require('../../app/lib/base-notifier.lib.js')

Expand Down
7 changes: 2 additions & 5 deletions test/lib/legacy-db-snake-case-mappers.lib.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it } = (exports.lab = Lab.script())
const { expect } = Code
const { describe, it } = require('node:test')
const { expect } = require('@hapi/code')

// Thing under test
const LegacyDbSnakeCaseMappersLib = require('../../app/lib/legacy-db-snake-case-mappers.lib.js')
Expand Down
Loading

0 comments on commit c7bd75b

Please sign in to comment.