Skip to content

Commit 1e795a7

Browse files
committed
PP-14403 Migrate unit tests to use Jest
- This repo uses Karma (existing tests) and Jest (used for newer tests). - This PR updates some existing tests to use Jest. - Updated the Karam config to ignore these files when it is running as Jest will test them instead. - Updated the NPM Jest command to run the updated tests. - Future PRs will migrate the remaining tests.
1 parent 30cca65 commit 1e795a7

9 files changed

Lines changed: 35 additions & 43 deletions

karma.conf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module.exports = karma => karma.set({
1414
exclude: [
1515
'**/axios-base-client.test.js',
1616
'analytics/**/*.js',
17-
'utils/middleware/**/*.js',
18-
'**/errors.test.js'
17+
'**/utils/**/*.js',
18+
'**/errors.test.js',
19+
'**/nunjucks-filters/**/*.js'
1920
],
2021
plugins: [
2122
'karma-mocha',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"prepare": "npm run transpile && npm run browserify-analytics",
2121
"test": "npm run jest-tests npm && npm run karma-tests",
2222
"karma-tests": "karma start",
23-
"jest-tests": "jest src/analytics src/utils/axios-base-client/*.test.js src/utils/middleware/*.test.js",
23+
"jest-tests": "jest src/analytics src/utils/**.test.js src/utils/**/*.test.js src/nunjucks-filters/*.test.js",
2424
"lint": "standard --fix"
2525
},
2626
"repository": {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

3-
const { expect } = require('chai')
43
const countryISOtoName = require('./country-iso-to-name')
54

65
describe('When a country ISO code is passed through the Nunjucks filter', () => {
76
it('it should convert it to the full name of that country', () => {
8-
expect(countryISOtoName('GB')).to.equal('United Kingdom')
7+
expect(countryISOtoName('GB')).toEqual('United Kingdom')
98
})
109
})
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
'use strict'
22

3-
const { expect } = require('chai')
43
const dateTimeFilter = require('./datetime')
54

65
describe('When an ISO timestring is passed through the Nunjucks date/time filter', () => {
76
const isoDateString = '2017-12-11T17:15:47Z'
87

98
it('it should output a full human readable date time', () => {
10-
expect(dateTimeFilter(isoDateString, 'full')).to.equal('11 December 2017 5:15:47pm GMT')
9+
expect(dateTimeFilter(isoDateString, 'full')).toEqual('11 December 2017 5:15:47pm GMT')
1110
})
1211
it('it should output a human readable date', () => {
13-
expect(dateTimeFilter(isoDateString, 'date')).to.equal('11/12/2017')
12+
expect(dateTimeFilter(isoDateString, 'date')).toEqual('11/12/2017')
1413
})
1514
it('it should output a human readable long date', () => {
16-
expect(dateTimeFilter(isoDateString, 'datelong')).to.equal('11 December 2017')
15+
expect(dateTimeFilter(isoDateString, 'datelong')).toEqual('11 December 2017')
1716
})
1817
it('it should output a human readable time', () => {
19-
expect(dateTimeFilter(isoDateString, 'time')).to.equal('17:15:47')
18+
expect(dateTimeFilter(isoDateString, 'time')).toEqual('17:15:47')
2019
})
2120
it('it should output a human readable date time with no seconds or timezone', () => {
22-
expect(dateTimeFilter(isoDateString, 'datetime')).to.equal('11 December 2017 17:15')
21+
expect(dateTimeFilter(isoDateString, 'datetime')).toEqual('11 December 2017 17:15')
2322
})
2423
})
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict'
22

3-
const { expect } = require('chai')
43
const removeArticles = require('./remove-indefinite-articles')
54

65
describe('When a string is passed through the Nunjucks removeIndefiniteArticles filter', () => {
76
it('it should remove indefinite articles (a/an)', () => {
8-
expect(removeArticles('Pay for a thing')).to.equal('Pay for thing')
7+
expect(removeArticles('Pay for a thing')).toEqual('Pay for thing')
98
})
109
it('it should remove indefinite articles (the)', () => {
11-
expect(removeArticles('The Private Office of Jon Heslop')).to.equal('Private Office of Jon Heslop')
10+
expect(removeArticles('The Private Office of Jon Heslop')).toEqual('Private Office of Jon Heslop')
1211
})
1312
})
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
'use strict'
22

3-
const { expect } = require('chai')
43
const slugify = require('./slugify')
54

65
describe('When a string is passed through the Nunjucks slugify filter', () => {
76
it('it should make it url friendly', () => {
8-
expect(slugify('Someönés*_+~.()\'"!:@?=,;{}/[]s string with-hyphen')).to.equal('someoness-string-with-hyphen')
7+
expect(slugify('Someönés*_+~.()\'"!:@?=,;{}/[]s string with-hyphen')).toEqual('someoness-string-with-hyphen')
98
})
109

1110
it('should replace all Welsh accented vowels with unaccented characters', () => {
12-
expect(slugify('îïìíûùúüêèéëâàáäæåãôòóöœøyŷỳýÿŵẁẃẅ')).to.equal('iiiiuuuueeeeaaaaaeaaoooooeoyyyyywwww')
11+
expect(slugify('îïìíûùúüêèéëâàáäæåãôòóöœøyŷỳýÿŵẁẃẅ')).toEqual('iiiiuuuueeeeaaaaaeaaoooooeoyyyyywwww')
1312
})
1413

1514
it('should replace capital Welsh accented vowels with lower case unaccented characters', () => {
16-
expect(slugify('ÎÏÌÍÛÙÚÜÊÈÉËÂÀÁÄÆÅÃÔÒÓÖŒØŶỲÝŸŴẀẂẄ')).to.equal('iiiiuuuueeeeaaaaaeaaoooooeoyyyywwww')
15+
expect(slugify('ÎÏÌÍÛÙÚÜÊÈÉËÂÀÁÄÆÅÃÔÒÓÖŒØŶỲÝŸŴẀẂẄ')).toEqual('iiiiuuuueeeeaaaaaeaaoooooeoyyyywwww')
1716
})
1817
})

src/utils/countries.test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
const { expect } = require('chai')
21
const countries = require('./countries')
32

43
describe('countries', () => {
54
describe('retrieveCountries', () => {
65
it('should list of countries ordered', () => {
76
const retrievedCountries = countries.retrieveCountries()
87

9-
expect(retrievedCountries[0].entry.country).to.eql('AF')
10-
expect(retrievedCountries[1].entry.country).to.eql('AL')
8+
expect(retrievedCountries[0].entry.country).toEqual('AF')
9+
expect(retrievedCountries[1].entry.country).toEqual('AL')
1110
})
1211

1312
it('should select only the correct country', () => {
1413
const retrievedCountries = countries.retrieveCountries('CH')
1514
const selectedCountries = retrievedCountries.filter(country => country.entry.selected === true)
16-
expect(selectedCountries.length).to.be.eql(1)
17-
expect(selectedCountries[0].entry.country).to.eql('CH')
15+
expect(selectedCountries.length).toEqual(1)
16+
expect(selectedCountries[0].entry.country).toEqual('CH')
1817
})
1918

2019
it('should select GB by default', () => {
2120
const retrievedCountries = countries.retrieveCountries()
2221
const selectedCountries = retrievedCountries.filter(country => country.entry.selected === true)
23-
expect(selectedCountries.length).to.be.eql(1)
24-
expect(selectedCountries[0].entry.country).to.eql('GB')
22+
expect(selectedCountries.length).toEqual(1)
23+
expect(selectedCountries[0].entry.country).toEqual('GB')
2524
})
2625
})
2726

2827
describe('translateAlpha2', () => {
2928
it('should translate country code to name', () => {
30-
expect(countries.translateAlpha2('GB')).to.eql('United Kingdom')
29+
expect(countries.translateAlpha2('GB')).toEqual('United Kingdom')
3130
})
3231

3332
it('should return undefined when an invalid country code is used', () => {
34-
expect(countries.translateAlpha2('ZZ')).to.eql(undefined)
33+
expect(countries.translateAlpha2('ZZ')).toEqual(undefined)
3534
})
3635

3736
it('should return undefined when no country code is used', () => {
38-
expect(countries.translateAlpha2()).to.eql(undefined)
37+
expect(countries.translateAlpha2()).toEqual(undefined)
3938
})
4039
})
4140
})

src/utils/currency-formatter.test.js

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

33
// NPM dependencies
4-
const { expect } = require('chai')
54
const {
65
penceToPounds,
76
poundsToPence,
@@ -19,49 +18,49 @@ const invalidAmountWithLetter = '10.50g'
1918
describe('Pence to pounds', () => {
2019
describe(`when given valid pence amount ${validPoundsAndPenceAmount}`, () => {
2120
it(`should return ${validPoundsAndPenceAmount}`, () => {
22-
expect(penceToPounds(validPenceAmount)).to.equal(validPoundsAndPenceAmount)
21+
expect(penceToPounds(validPenceAmount)).toEqual(validPoundsAndPenceAmount)
2322
})
2423
})
2524
})
2625

2726
describe('Pounds to pence', () => {
2827
describe(`when given valid pounds amount ${validPoundsAndPenceAmount}`, () => {
2928
it(`should return ${validPenceAmount}`, () => {
30-
expect(poundsToPence(validPoundsAndPenceAmount)).to.equal('1050')
29+
expect(poundsToPence(validPoundsAndPenceAmount)).toEqual('1050')
3130
})
3231
})
3332
})
3433

3534
describe('Pounds to pence with currency', () => {
3635
describe(`when given valid pence amount ${validPenceAmount}`, () => {
3736
it(`should return ${validPoundsAndPenceAmountWithCurrency}`, () => {
38-
expect(penceToPoundsWithCurrency(validPenceAmount)).to.equal(validPoundsAndPenceAmountWithCurrency)
37+
expect(penceToPoundsWithCurrency(validPenceAmount)).toEqual(validPoundsAndPenceAmountWithCurrency)
3938
})
4039
})
4140
})
4241

4342
describe('Sanitise pounds and pence user input', () => {
4443
describe(`when given pounds and pence amount from user input ${validPoundsAndPenceAmount}`, () => {
4544
it(`should return ${validPenceAmount}`, () => {
46-
expect(sanitisePoundsAndPenceInput(validPoundsAndPenceAmount)).to.equal(validPenceAmount)
45+
expect(sanitisePoundsAndPenceInput(validPoundsAndPenceAmount)).toEqual(validPenceAmount)
4746
})
4847
})
4948

5049
describe(`when given pounds from user input ${invalidAmountWithoutDecimals}`, () => {
5150
it(`should return ${validPenceAmount}`, () => {
52-
expect(sanitisePoundsAndPenceInput(invalidAmountWithoutDecimals)).to.equal(1000)
51+
expect(sanitisePoundsAndPenceInput(invalidAmountWithoutDecimals)).toEqual(1000)
5352
})
5453
})
5554

5655
describe(`when given invalid pounds and pence from user input ${invalidAmountWithOneDecimals}`, () => {
5756
it(`should return ${validPenceAmount}`, () => {
58-
expect(sanitisePoundsAndPenceInput(invalidAmountWithOneDecimals)).to.equal(validPenceAmount)
57+
expect(sanitisePoundsAndPenceInput(invalidAmountWithOneDecimals)).toEqual(validPenceAmount)
5958
})
6059
})
6160

6261
describe(`when given invalid pounds and pence from user input ${invalidAmountWithLetter}`, () => {
6362
it(`should return ${validPenceAmount}`, () => {
64-
expect(sanitisePoundsAndPenceInput(invalidAmountWithLetter)).to.equal(validPenceAmount)
63+
expect(sanitisePoundsAndPenceInput(invalidAmountWithLetter)).toEqual(validPenceAmount)
6564
})
6665
})
6766
})

src/utils/is-valid-email.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
'use strict'
2-
3-
const { expect } = require('chai')
4-
52
const emailValidator = require('./is-valid-email')
63

74
describe('When the email validation function', () => {
85
describe('is passed an invalid email address', () => {
96
it('should return false', () => {
10-
expect(emailValidator('name@mail')).to.equal(false)
7+
expect(emailValidator('name@mail')).toEqual(false)
118
})
129
})
1310
describe('is passed an a valid email address', () => {
1411
it('should return true', () => {
15-
expect(emailValidator('name@mail.com')).to.equal(true)
16-
expect(emailValidator('first.last@subdomain.mail.com')).to.equal(true)
17-
expect(emailValidator('first.last@digital.cabinet-office.gov.uk')).to.equal(true)
12+
expect(emailValidator('name@mail.com')).toEqual(true)
13+
expect(emailValidator('first.last@subdomain.mail.com')).toEqual(true)
14+
expect(emailValidator('first.last@digital.cabinet-office.gov.uk')).toEqual(true)
1815
})
1916
})
2017
})

0 commit comments

Comments
 (0)