Skip to content

Commit

Permalink
Move to jest instead of old mocha + istanbul
Browse files Browse the repository at this point in the history
  • Loading branch information
queicherius committed Oct 4, 2018
1 parent a131732 commit d0c7e3b
Show file tree
Hide file tree
Showing 54 changed files with 1,347 additions and 1,397 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "./build/index.js",
"scripts": {
"build": "abc build",
"test": "abc test && abc lint",
"test": "jest --runInBand --forceExit && abc lint",
"version": "abc build"
},
"author": "[email protected]",
Expand All @@ -30,6 +30,7 @@
},
"devDependencies": {
"abc-environment": "^2.0.0",
"jest": "^23.6.0",
"redis": "^2.6.2"
}
}
1 change: 1 addition & 0 deletions src/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export default class AbstractEndpoint {
// If we are partially caching and all not-cached ids are all invalid,
// simulate the API behaviour by silently swallowing errors.
let handleMissingIds = (err) => {
/* istanbul ignore else */
if (partialRequest && err.response && err.response.status === 404) {
return Promise.resolve([])
}
Expand Down
37 changes: 18 additions & 19 deletions tests/cache/browser.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-env node, mocha */
import { expect } from 'chai'
/* eslint-env jest */
import storage from '../../src/cache/browser'
import idbMock from '../mocks/idb.mock.js'

const cache = storage({storageEngine: idbMock, gcTick: 500, persistDebounce: 100})
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

describe('cache > browser', function () {
this.timeout(5000)
jest.setTimeout(5000)

beforeEach(() => {
cache.flush()
Expand All @@ -31,54 +30,54 @@ describe('cache > browser', function () {
// Make sure the data is cached
await wait(100)
let cachedFromPersistent = await tmpCache.get('foo')
expect(cachedFromPersistent, 'cachedFromPersistent').to.deep.equal({bar: 1337})
expect(cachedFromPersistent).toEqual({bar: 1337})
})

it('can set and get a single value', async () => {
await cache.set('foo', {herp: 'derp'}, 2)

// Make sure the data is cached
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.deep.equal({herp: 'derp'})
expect(cachedFresh).toEqual({herp: 'derp'})

// Make sure that the debounce saving is respected
let storageBeforeSave = await idbMock.get('gw2api-cache')
expect(storageBeforeSave, 'storageBeforeSave').to.equal(undefined)
expect(storageBeforeSave).toEqual(undefined)
await wait(150)
let storageAfterSave = await idbMock.get('gw2api-cache')
expect(storageAfterSave.foo.value, 'storageAfterSave value').to.deep.equal({herp: 'derp'})
expect(storageAfterSave.foo.expiry, 'storageAfterSave expiry').to.be.a('number')
expect(storageAfterSave.foo.value).toEqual({herp: 'derp'})
expect(typeof storageAfterSave.foo.expiry).toEqual('number')

// Make sure the data expires
await wait(3000)
let cachedExpired = await cache.get('foo')
expect(cachedExpired, 'cachedExpired').to.deep.equal(null)
expect(cachedExpired).toEqual(null)
})

it('can set and get multiple values', async () => {
await cache.set('abc', {foo: 'bar'}, 2)
await cache.mset([['foo', 'bar', 2], ['herp', {derp: 1}, 2]])

let cachedFresh = await cache.get('abc')
expect(cachedFresh, 'cachedFresh').to.deep.equal({foo: 'bar'})
expect(cachedFresh).toEqual({foo: 'bar'})
let cachedFreshMany = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedFreshMany, 'cachedFreshMany').to.deep.equal(['bar', {derp: 1}, {foo: 'bar'}])
expect(cachedFreshMany).toEqual(['bar', {derp: 1}, {foo: 'bar'}])

await wait(3000)

let cachedExpired = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedExpired, 'cachedExpired').to.deep.equal([null, null, null])
expect(cachedExpired).toEqual([null, null, null])
})

it('can flush the cache', async () => {
await cache.set('foo', 'bar', 60 * 60)
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.equal('bar')
expect(cachedFresh).toEqual('bar')

await cache.flush()

let cachedFlushed = await cache.get('foo')
expect(cachedFlushed, 'cachedFlushed').to.equal(null)
expect(cachedFlushed).toEqual(null)
})

it('triggers garbage collection', async () => {
Expand All @@ -89,16 +88,16 @@ describe('cache > browser', function () {

// Memory keys
let memoryKeys = Object.keys(cache._getStorage())
expect(memoryKeys).to.deep.equal(['herp'])
expect(memoryKeys).toEqual(['herp'])

// Make sure we don't delete other keys of importance
let keys = await idbMock.keys()
expect(keys).to.deep.equal(['gw2api-temp-cache', 'lol', 'gw2api-cache'])
expect(keys).toEqual(['gw2api-temp-cache', 'lol', 'gw2api-cache'])

// Make sure that the non-expired item is persisted
let persistentStorage = await idbMock.get('gw2api-cache')
expect(Object.keys(persistentStorage), 'persistentStorage keys').to.deep.equal(['herp'])
expect(persistentStorage.herp.value, 'persistentStorage value').to.equal('derp')
expect(persistentStorage.herp.expiry, 'persistentStorage expiry').to.be.a('number')
expect(Object.keys(persistentStorage)).toEqual(['herp'])
expect(persistentStorage.herp.value).toEqual('derp')
expect(typeof persistentStorage.herp.expiry).toEqual('number')
})
})
21 changes: 10 additions & 11 deletions tests/cache/memory.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-env node, mocha */
import { expect } from 'chai'
/* eslint-env jest */
import storage from '../../src/cache/memory'
const cache = storage({gcTick: 500})
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

describe('cache > memory', function () {
this.timeout(5000)
jest.setTimeout(5000)

beforeEach(() => {
cache.flush()
Expand All @@ -14,40 +13,40 @@ describe('cache > memory', function () {
it('can flush the cache', async () => {
await cache.set('foo', 'bar', 60 * 60)
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.equal('bar')
expect(cachedFresh).toEqual('bar')

await cache.flush()

let cachedFlushed = await cache.get('foo')
expect(cachedFlushed, 'cachedFlushed').to.equal(null)
expect(cachedFlushed).toEqual(null)
})

it('can set and get a single value', async () => {
await cache.set('foo', {herp: 'derp'}, 2)

// Make sure the data is cached
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.deep.equal({herp: 'derp'})
expect(cachedFresh).toEqual({herp: 'derp'})

// Make sure the data expires
await wait(3000)
let cachedExpired = await cache.get('foo')
expect(cachedExpired, 'cachedExpired').to.deep.equal(null)
expect(cachedExpired).toEqual(null)
})

it('can set and get multiple values', async () => {
await cache.set('abc', {foo: 'bar'}, 2)
await cache.mset([['foo', 'bar', 2], ['herp', {derp: 1}, 2]])

let cachedFresh = await cache.get('abc')
expect(cachedFresh, 'cachedFresh').to.deep.equal({foo: 'bar'})
expect(cachedFresh).toEqual({foo: 'bar'})
let cachedFreshMany = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedFreshMany, 'cachedFreshMany').to.deep.equal(['bar', {derp: 1}, {foo: 'bar'}])
expect(cachedFreshMany).toEqual(['bar', {derp: 1}, {foo: 'bar'}])

await wait(3000)

let cachedExpired = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedExpired, 'cachedExpired').to.deep.equal([null, null, null])
expect(cachedExpired).toEqual([null, null, null])
})

it('triggers garbage collection', async () => {
Expand All @@ -57,6 +56,6 @@ describe('cache > memory', function () {
await wait(3000)

let keys = Object.keys(cache._getStorage())
expect(keys).to.deep.equal(['herp'])
expect(keys).toEqual(['herp'])
})
})
7 changes: 3 additions & 4 deletions tests/cache/null.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env node, mocha */
import { expect } from 'chai'
/* eslint-env jest */
import storage from '../../src/cache/null'
const cache = storage()

Expand All @@ -11,12 +10,12 @@ describe('cache > null', function () {
it('can set and get a single value', async () => {
await cache.set('foo', 'bar', 1)
let cached = await cache.get('foo')
expect(cached).to.equal(null)
expect(cached).toEqual(null)
})

it('can set and get multiple values', async () => {
await cache.mset(['foo', 'bar', 1], ['herp', 'derp', 1])
let cached = await cache.mget(['foo', 'herp'])
expect(cached).to.deep.equal([null, null])
expect(cached).toEqual([null, null])
})
})
21 changes: 10 additions & 11 deletions tests/cache/redis.spec.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
/* eslint-env node, mocha */
import { expect } from 'chai'
/* eslint-env jest */
import storage from '../../src/cache/redis'
import redis from 'redis'
const cache = storage({redis: redis.createClient()})
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

describe('cache > redis', function () {
this.timeout(5000)
jest.setTimeout(5000)

beforeEach(() => {
cache.flush()
})

it('requires an configuration object', () => {
expect(() => storage()).to.throw()
expect(() => storage()).toThrow()
})

it('can flush the cache', async () => {
await cache.set('foo', 'bar', 60 * 60)
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.equal('bar')
expect(cachedFresh).toEqual('bar')

await cache.flush()

let cachedFlushed = await cache.get('foo')
expect(cachedFlushed, 'cachedFlushed').to.equal(null)
expect(cachedFlushed).toEqual(null)
})

it('can set and get a single value', async () => {
await cache.set('foo', {herp: 'derp'}, 2)

// Make sure the data is cached
let cachedFresh = await cache.get('foo')
expect(cachedFresh, 'cachedFresh').to.deep.equal({herp: 'derp'})
expect(cachedFresh).toEqual({herp: 'derp'})

// Make sure the data expires
await wait(3000)
let cachedExpired = await cache.get('foo')
expect(cachedExpired, 'cachedExpired').to.deep.equal(null)
expect(cachedExpired).toEqual(null)
})

it('can set and get multiple values', async () => {
await cache.set('abc', {foo: 'bar'}, 2)
await cache.mset([['foo', 'bar', 2], ['herp', {derp: 1}, 2]])

let cachedFresh = await cache.get('abc')
expect(cachedFresh, 'cachedFresh').to.deep.equal({foo: 'bar'})
expect(cachedFresh).toEqual({foo: 'bar'})
let cachedFreshMany = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedFreshMany, 'cachedFreshMany').to.deep.equal(['bar', {derp: 1}, {foo: 'bar'}])
expect(cachedFreshMany).toEqual(['bar', {derp: 1}, {foo: 'bar'}])

await wait(3000)

let cachedExpired = await cache.mget(['foo', 'herp', 'abc'])
expect(cachedExpired, 'cachedExpired').to.deep.equal([null, null, null])
expect(cachedExpired).toEqual([null, null, null])
})
})
Loading

0 comments on commit d0c7e3b

Please sign in to comment.