Skip to content

Commit

Permalink
Use jest (#605)
Browse files Browse the repository at this point in the history
* migrate tests to jest

* remove chai & mocha
  • Loading branch information
davidgoli authored Nov 10, 2023
1 parent 26c799f commit 3d27f69
Show file tree
Hide file tree
Showing 16 changed files with 1,713 additions and 280 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist/
coverage/
webpack.config.js
.eslintrc.js
.eslintrc.js
jest.config.js
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
}
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"format": "yarn prettier --write .",
"format-check": "yarn prettier --check .",
"run-ts": "TS_NODE_PROJECT=tsconfig.json node --loader ts-node/esm",
"test": "yarn run-ts ./node_modules/.bin/mocha **/*.test.ts",
"test-ci": "yarn run-ts ./node_modules/.bin/nyc yarn run-ts ./node_modules/.bin/mocha **/*.test.ts"
"test": "jest **/*.test.ts",
"test-ci": "yarn run-ts ./node_modules/.bin/nyc jest **/*.test.ts"
},
"nyc": {
"extension": [
Expand All @@ -51,28 +51,27 @@
},
"devDependencies": {
"@types/assert": "^1.4.3",
"@types/chai": "^4.2.7",
"@types/mocha": "^9.1.1",
"@types/jest": "^29.5.8",
"@types/mockdate": "^3.0.0",
"@types/node": "^17.0.41",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"chai": "^4.2.0",
"coverage": "^0.4.1",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^39.3.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^8.0.1",
"jest": "^29.7.0",
"lint-staged": "^13.0.1",
"mocha": "^10.0.0",
"mockdate": "^3.0.5",
"nyc": "^15.1.0",
"prettier": "^2.6.2",
"source-map-loader": "^3.0.1",
"source-map-support": "^0.5.16",
"terser-webpack-plugin": "^5.3.3",
"ts-jest": "^29.1.1",
"ts-loader": "^9.3.0",
"ts-node": "^10.8.1",
"typescript": "^4.7.3",
Expand Down
13 changes: 6 additions & 7 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Cache } from '../src/cache'
import { expect } from 'chai'
import { IterArgs } from '../src/iterresult'

const dates = [
Expand All @@ -21,7 +20,7 @@ describe('Cache', () => {
inc: true,
}

expect(cache._cacheGet('between', args)).to.be.false
expect(cache._cacheGet('between', args)).toBe(false)
})

it('returns an empty array for a cached but empty set', () => {
Expand All @@ -34,7 +33,7 @@ describe('Cache', () => {

cache._cacheAdd('between', [], args)

expect(cache._cacheGet('between', args)).to.eql([])
expect(cache._cacheGet('between', args)).toEqual([])
})

it('returns cached entries if the "what" and the args both match', () => {
Expand All @@ -47,7 +46,7 @@ describe('Cache', () => {

cache._cacheAdd('between', dates, args)

expect(cache._cacheGet('between', args)).to.eql(dates)
expect(cache._cacheGet('between', args)).toEqual(dates)
})

it('does not return cached entries if the "what" matches but the args do not', () => {
Expand All @@ -66,7 +65,7 @@ describe('Cache', () => {
/** 1ms later than the args used for the insert */
after: new Date('2021-01-01T00:00:00.001+00:00'),
})
).to.equal(false)
).toBe(false)
})

it('does not return cached entries if args match but the "what" does not', () => {
Expand All @@ -79,7 +78,7 @@ describe('Cache', () => {

cache._cacheAdd('between', dates, args)

expect(cache._cacheGet('after', args)).to.equal(false)
expect(cache._cacheGet('after', args)).toBe(false)
})

it('reuses dates cached for the "all" method when querying using another method', () => {
Expand All @@ -92,7 +91,7 @@ describe('Cache', () => {

cache._cacheAdd('all', dates)

expect(cache._cacheGet('between', args)).to.eql([
expect(cache._cacheGet('between', args)).toEqual([
new Date('2021-01-04T00:00:00.000+00:00'),
new Date('2021-01-05T00:00:00.000+00:00'),
new Date('2021-01-06T00:00:00.000+00:00'),
Expand Down
3 changes: 1 addition & 2 deletions test/dateutil.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { expect } from 'chai'
import { datetime, untilStringToDate } from '../src/dateutil'

describe('untilStringToDate', () => {
it('parses a date string', () => {
const date = untilStringToDate('19970902T090000')
expect(date.getTime()).to.equal(datetime(1997, 9, 2, 9, 0, 0).getTime())
expect(date.getTime()).toBe(datetime(1997, 9, 2, 9, 0, 0).getTime())
})
})
15 changes: 7 additions & 8 deletions test/datewithzone.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import { DateWithZone } from '../src/datewithzone'
import { expect } from 'chai'
import { set as setMockDate, reset as resetMockDate } from 'mockdate'
import { datetime, expectedDate } from './lib/utils'

describe('toString', () => {
it('returns the date when no tzid is present', () => {
const dt = new DateWithZone(datetime(2010, 10, 5, 11, 0, 0))
expect(dt.toString()).to.equal(':20101005T110000Z')
expect(dt.toString()).toBe(':20101005T110000Z')

const dt2 = new DateWithZone(datetime(2010, 10, 5, 11, 0, 0), 'UTC')
expect(dt2.toString()).to.equal(':20101005T110000Z')
expect(dt2.toString()).toBe(':20101005T110000Z')
})

it('returns the date with tzid when present', () => {
const dt = new DateWithZone(datetime(2010, 10, 5, 11, 0, 0), 'Asia/Tokyo')
expect(dt.toString()).to.equal(';TZID=Asia/Tokyo:20101005T110000')
expect(dt.toString()).toBe(';TZID=Asia/Tokyo:20101005T110000')
})
})

it('returns the time of the date', () => {
const d = datetime(2010, 10, 5, 11, 0, 0)
const dt = new DateWithZone(d)
expect(dt.getTime()).to.equal(d.getTime())
expect(dt.getTime()).toBe(d.getTime())
})

it('rejects invalid dates', () => {
expect(() => new DateWithZone(new Date(undefined))).to.throw(
expect(() => new DateWithZone(new Date(undefined))).toThrow(
'Invalid date passed to DateWithZone'
)
})
Expand All @@ -34,7 +33,7 @@ describe('rezonedDate', () => {
it('returns the original date when no zone is given', () => {
const d = datetime(2010, 10, 5, 11, 0, 0)
const dt = new DateWithZone(d)
expect(dt.rezonedDate()).to.deep.equal(d)
expect(dt.rezonedDate()).toEqual(d)
})

it('returns the date in the correct zone when given', () => {
Expand All @@ -44,7 +43,7 @@ describe('rezonedDate', () => {

const d = new Date(Date.parse('2010-10-05T11:00:00'))
const dt = new DateWithZone(d, targetZone)
expect(dt.rezonedDate()).to.deep.equal(
expect(dt.rezonedDate()).toEqual(
expectedDate(
new Date(Date.parse('2010-10-05T11:00:00')),
currentLocalDate,
Expand Down
85 changes: 40 additions & 45 deletions test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,138 +10,133 @@ import {
isPresent,
isArray,
} from '../src/helpers'
import { expect } from 'chai'
import { isNumber } from 'util'

describe('isPresent', () => {
it('is false if object is null', () => {
expect(isPresent(null)).to.be.false
expect(isPresent(null)).toBe(false)
})

it('is false if object is undefined', () => {
expect(isPresent(undefined)).to.be.false
expect(isPresent(undefined)).toBe(false)
})

it('is true if object is non-null and not undefined', () => {
expect(isPresent(0)).to.be.true
expect(isPresent('')).to.be.true
expect(isPresent('foo')).to.be.true
expect(isPresent(123)).to.be.true
expect(isPresent([])).to.be.true
expect(isPresent(0)).toBe(true)
expect(isPresent('')).toBe(true)
expect(isPresent('foo')).toBe(true)
expect(isPresent(123)).toBe(true)
expect(isPresent([])).toBe(true)
})
})

describe('isArray', () => {
it('is true if it is an array', () => {
expect(isArray([])).to.be.true
expect(isArray([1])).to.be.true
expect(isArray(['foo'])).to.be.true
expect(isArray([])).toBe(true)
expect(isArray([1])).toBe(true)
expect(isArray(['foo'])).toBe(true)
})

it('is false if it is empty', () => {
expect(isArray('foo')).to.be.false
expect(isArray(null)).to.be.false
expect(isArray(0)).to.be.false
expect(isArray(undefined)).to.be.false
expect(isArray('foo')).toBe(false)
expect(isArray(null)).toBe(false)
expect(isArray(0)).toBe(false)
expect(isArray(undefined)).toBe(false)
})
})

describe('isNumber', () => {
it('is true if it is a number', () => {
expect(isNumber(0)).to.be.true
expect(isNumber(0)).toBe(true)
})

it('is false if it is not a number', () => {
expect(isNumber('1')).to.be.false
expect(isNumber(null)).to.be.false
expect(isNumber('1')).toBe(false)
expect(isNumber(null)).toBe(false)
})
})

describe('empty', () => {
it('is empty if object is null', () => {
expect(empty(null)).to.be.true
expect(empty(null)).toBe(true)
})

it('is empty if object is undefined', () => {
expect(empty(undefined)).to.be.true
expect(empty(undefined)).toBe(true)
})

it('is empty if object is an empty array', () => {
expect(empty([])).to.be.true
expect(empty([])).toBe(true)
})

it('is not empty if object is a non-empty array', () => {
expect(empty(['foo'])).to.be.false
expect(empty([0])).to.be.false
expect(empty(['foo'])).toBe(false)
expect(empty([0])).toBe(false)
})
})

describe('includes', () => {
it('is true if the object is found', () => {
expect(includes(['foo'], 'foo')).to.be.true
expect(includes([0], 0)).to.be.true
expect(includes(['foo'], 'foo')).toBe(true)
expect(includes([0], 0)).toBe(true)
})

it('is false if the object is not found', () => {
expect(includes(['foo'], 'bar')).to.be.false
expect(includes([0], 1)).to.be.false
expect(includes(['foo'], 'bar')).toBe(false)
expect(includes([0], 1)).toBe(false)
})
})

describe('pymod', () => {
it('returns the wrapped result', () => {
expect(pymod(1, 8)).to.equal(1)
expect(pymod(-1, -8)).to.equal(-1)
expect(pymod(-1, 8)).to.equal(7)
expect(pymod(1, 8)).toBe(1)
expect(pymod(-1, -8)).toBe(-1)
expect(pymod(-1, 8)).toBe(7)
})
})

describe('divmod', () => {
it('returns the divided result', () => {
expect(divmod(1, 8)).to.deep.equal({ div: 0, mod: 1 })
expect(divmod(-1, -8)).to.deep.equal({ div: 0, mod: -1 })
expect(divmod(-1, 8)).to.deep.equal({ div: -1, mod: 7 })
expect(divmod(1, 8)).toEqual({ div: 0, mod: 1 })
expect(divmod(-1, -8)).toEqual({ div: 0, mod: -1 })
expect(divmod(-1, 8)).toEqual({ div: -1, mod: 7 })
})
})

describe('split', () => {
it('splits on the separator', () => {
expect(split('one-two-three', '-', 0)).to.deep.equal([
'one',
'two',
'three',
])
expect(split('one-two-three', '-', 0)).toEqual(['one', 'two', 'three'])
})

it('only splits the specified number when nonzero', () => {
expect(split('one-two-three', '-', 1)).to.deep.equal(['one', 'two-three'])
expect(split('one-two-three', '-', 1)).toEqual(['one', 'two-three'])
})
})

describe('clone', () => {
it('copies an array', () => {
const a = ['a', 'b', 'c']
expect(clone(a)).not.to.equal(a)
expect(clone(a)).to.deep.equal(a)
expect(clone(a)).not.toBe(a)
expect(clone(a)).toEqual(a)
})
})

describe('range', () => {
it('generates a range', () => {
expect(range(3, 7)).to.deep.equal([3, 4, 5, 6])
expect(range(3, 7)).toEqual([3, 4, 5, 6])
})

it('generates to the first argument if no second argument is given', () => {
expect(range(7)).to.deep.equal([0, 1, 2, 3, 4, 5, 6])
expect(range(7)).toEqual([0, 1, 2, 3, 4, 5, 6])
})
})

describe('repeat', () => {
it('repeats a single item', () => {
expect(repeat('foo', 3)).to.deep.equal(['foo', 'foo', 'foo'])
expect(repeat('foo', 3)).toEqual(['foo', 'foo', 'foo'])
})

it('repeats an array', () => {
expect(repeat(['foo'], 3)).to.deep.equal([['foo'], ['foo'], ['foo']])
expect(repeat(['foo'], 3)).toEqual([['foo'], ['foo'], ['foo']])
})
})
Loading

0 comments on commit 3d27f69

Please sign in to comment.