Skip to content

Commit e77a7f1

Browse files
authoredJun 15, 2019
test: replace mocha with jest (#324)
* test: replace test with jest phase 1 * test: replace test with jest phase 2 * test: replace test with jest phase 3 * test: replace test with jest phase 4 * chore: regenerate yarn.lock * chore: update .npmignore
1 parent ad39e88 commit e77a7f1

14 files changed

+2330
-1362
lines changed
 

‎.eslintrc

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
"extends": [
33
"standard",
44
"prettier",
5-
"prettier/standard"
5+
"prettier/standard",
6+
"plugin:jest/recommended"
67
],
78
"parserOptions": {
89
"ecmaVersion": 9
910
},
1011
"plugins": [
1112
"prettier",
12-
"mocha"
13+
"jest"
1314
],
1415
"rules": {
15-
"mocha/no-exclusive-tests": "error",
16+
"jest/no-disabled-tests": "error",
1617
"prettier/prettier": "error",
1718
"standard/no-callback-literal": "off",
1819
"no-unused-vars": [
@@ -33,6 +34,6 @@
3334
"env": {
3435
"node": true,
3536
"es6": true,
36-
"mocha": true
37+
"jest/globals": true
3738
}
3839
}

‎.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
History.md
44
Makefile
55
Readme.md
6+
.DS_Store
67

78
examples/
89
support/
@@ -15,3 +16,4 @@ coverage/
1516
.travis.yml
1617
commitlint.config.js
1718
.nyc_output/
19+
jest.config.js

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ script:
2727
- MONGODB_URL=mongodb://localhost/connect-mongo-test travis_retry yarn test
2828

2929
after_success:
30-
- npm install coveralls -g && yarn cover | coveralls
30+
- npm install coveralls -g && yarn test --coverageReporters=text-lcov | coveralls

‎jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testPathIgnorePatterns: ['/node_modules/', 'src'],
4+
collectCoverageFrom: ['src/**/*.js', '!**/test/**', '!*.js'],
5+
setupFilesAfterEnv: ['<rootDir>/test/jest.setup.js'],
6+
}

‎package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,22 @@
3232
"eslint-config-prettier-standard": "^2.0.0",
3333
"eslint-config-standard": "^12.0.0",
3434
"eslint-plugin-import": "^2.17.3",
35-
"eslint-plugin-mocha": "^5.3.0",
35+
"eslint-plugin-jest": "^22.6.4",
3636
"eslint-plugin-node": "^9.1.0",
3737
"eslint-plugin-prettier": "^3.1.0",
3838
"eslint-plugin-promise": "^4.1.1",
3939
"eslint-plugin-standard": "^4.0.0",
40-
"expect.js": "^0.3.1",
4140
"express-session": "^1.0.0",
4241
"husky": "^2.4.1",
42+
"jest": "^24.8.0",
4343
"lint-staged": "^8.2.1",
44-
"mocha": "^6.1.4",
4544
"mongoose": "^5.2.0",
46-
"nyc": "^14.1.1",
4745
"prettier": "^1.18.2"
4846
},
4947
"scripts": {
5048
"lint": "eslint src test",
5149
"lint:fix": "eslint src test --fix",
52-
"cover": "nyc report --reporter=text-lcov",
53-
"test": "nyc mocha"
50+
"test": "jest --coverage"
5451
},
5552
"husky": {
5653
"hooks": {

‎src/crypto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Crypto {
5555
const hmac = this._digest(this.secret, ct.ct, this.hashing, this.encodeas)
5656

5757
if (hmac !== ct.hmac) {
58-
throw Error('Encrypted session was tampered with!')
58+
throw new Error('Encrypted session was tampered with!')
5959
}
6060

6161
if (ct.at) {

‎src/index.js

+8-13
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,11 @@ module.exports = function(connect) {
235235
.then(session => {
236236
if (session) {
237237
if (this.Crypto) {
238-
try {
239-
const tmpSession = this.transformFunctions.unserialize(
240-
session.session
241-
)
242-
session.session = this.Crypto.get(tmpSession)
243-
} catch (error) {
244-
return callback(error)
245-
}
238+
const tmpSession = this.transformFunctions.unserialize(
239+
session.session
240+
)
241+
session.session = this.Crypto.get(tmpSession)
246242
}
247-
248243
const s = this.transformFunctions.unserialize(session.session)
249244
if (this.options.touchAfter > 0 && session.lastModified) {
250245
s.lastModified = session.lastModified
@@ -269,7 +264,7 @@ module.exports = function(connect) {
269264
try {
270265
session = this.Crypto.set(session)
271266
} catch (error) {
272-
return callback(error)
267+
return withCallback(Promise.reject(error), callback)
273268
}
274269
}
275270

@@ -279,7 +274,7 @@ module.exports = function(connect) {
279274
session: this.transformFunctions.serialize(session),
280275
}
281276
} catch (err) {
282-
return callback(err)
277+
return withCallback(Promise.reject(err), callback)
283278
}
284279

285280
if (session && session.cookie && session.cookie.expires) {
@@ -338,7 +333,7 @@ module.exports = function(connect) {
338333
const timeElapsed = currentDate.getTime() - session.lastModified
339334

340335
if (timeElapsed < touchAfter) {
341-
return callback()
336+
return withCallback(Promise.resolve(), callback)
342337
}
343338
updateFields.lastModified = currentDate
344339
}
@@ -436,7 +431,7 @@ module.exports = function(connect) {
436431

437432
close() {
438433
if (this.client) {
439-
this.client.close()
434+
return this.client.close()
440435
}
441436
}
442437
}

‎test/crypto.js ‎test/crypto.test.js

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const expect = require('expect.js')
43
const Crypto = require('../src/crypto.js')
54

65
const options = {
@@ -16,28 +15,25 @@ describe('Crypto', () => {
1615

1716
it('Encrypt data', done => {
1817
ct = JSON.parse(Crypto.set('123, easy as ABC. ABC, easy as 123'))
19-
expect(ct).to.have.property('ct')
20-
expect(ct).to.have.property('iv')
21-
expect(ct).to.have.property('hmac')
18+
expect(ct).toHaveProperty('ct')
19+
expect(ct).toHaveProperty('iv')
20+
expect(ct).toHaveProperty('hmac')
2221
done()
2322
})
2423

2524
it('Decrypt data', done => {
2625
pt = Crypto.get(JSON.stringify(ct))
27-
expect(pt).to.match(/123, easy as ABC. ABC, easy as 123/)
26+
expect(pt).toMatch(/123, easy as ABC. ABC, easy as 123/)
2827
done()
2928
})
3029

3130
it('HMAC validation', done => {
3231
hmac = ct.hmac
3332
ct.hmac = 'funky chicken'
3433
ct = JSON.stringify(ct)
35-
36-
try {
34+
expect(() => {
3735
pt = Crypto.get(ct)
38-
} catch (err) {
39-
expect(err).to.match(/Encrypted session was tampered with/)
40-
}
36+
}).toThrow(/Encrypted session was tampered with/)
4137
done()
4238
})
4339

@@ -49,12 +45,9 @@ describe('Crypto', () => {
4945

5046
ct.at = 'funky chicken'
5147
ct = JSON.stringify(ct)
52-
53-
try {
48+
expect(() => {
5449
pt = Crypto.get(ct)
55-
} catch (err) {
56-
expect(err).to.match(/Unsupported state or unable to authenticate data/)
57-
}
50+
}).toThrow(/Unsupported state or unable to authenticate data/)
5851
done()
5952
})
6053

@@ -65,12 +58,9 @@ describe('Crypto', () => {
6558

6659
ct.aad = 'funky chicken'
6760
ct = JSON.stringify(ct)
68-
69-
try {
61+
expect(() => {
7062
pt = Crypto.get(ct)
71-
} catch (err) {
72-
expect(err).to.match(/Unsupported state or unable to authenticate data/)
73-
}
63+
}).toThrow(/Unsupported state or unable to authenticate data/)
7464
done()
7565
})
7666
})

‎test/events.js ‎test/events.test.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

3-
const expect = require('expect.js')
4-
53
const expressSession = require('express-session')
64
const MongoStore = require('..')(expressSession)
75

@@ -15,7 +13,6 @@ function noop() {}
1513
describe('Events', () => {
1614
let store, collection
1715
beforeEach(function(done) {
18-
this.timeout(10000)
1916
store = new MongoStore({
2017
url: connectionString,
2118
mongoOptions: { useNewUrlParser: true },
@@ -27,20 +24,20 @@ describe('Events', () => {
2724
})
2825
})
2926
afterEach(() => {
30-
store.close()
27+
return store.close()
3128
})
3229

3330
describe('set() with an unknown session id', () => {
3431
it('should emit a `create` event', done => {
3532
store.once('create', sid => {
36-
expect(sid).to.be('foo1')
33+
expect(sid).toBe('foo1')
3734
done()
3835
})
3936
store.set('foo1', { foo: 'bar' }, noop)
4037
})
4138
it('should emit a `set` event', done => {
4239
store.once('set', sid => {
43-
expect(sid).to.be('foo2')
40+
expect(sid).toBe('foo2')
4441
done()
4542
})
4643
store.set('foo2', { foo: 'bar' }, noop)
@@ -50,26 +47,26 @@ describe('Events', () => {
5047
describe('set() with a session id associated to an existing session', () => {
5148
it('should emit an `update` event', done => {
5249
store.once('update', sid => {
53-
expect(sid).to.be('foo3')
50+
expect(sid).toBe('foo3')
5451
done()
5552
})
5653
collection.insertOne(
5754
{ _id: 'foo3', session: { foo: 'bar1' }, expires: futureDate },
5855
err => {
59-
expect(err).not.to.be.ok()
56+
expect(err).toBeFalsy()
6057
store.set('foo3', { foo: 'bar2' }, noop)
6158
}
6259
)
6360
})
6461
it('should emit an `set` event', done => {
6562
store.once('update', sid => {
66-
expect(sid).to.be('foo4')
63+
expect(sid).toBe('foo4')
6764
done()
6865
})
6966
collection.insertOne(
7067
{ _id: 'foo4', session: { foo: 'bar1' }, expires: futureDate },
7168
err => {
72-
expect(err).not.to.be.ok()
69+
expect(err).toBeFalsy()
7370
store.set('foo4', { foo: 'bar2' }, noop)
7471
}
7572
)
@@ -80,7 +77,6 @@ describe('Events', () => {
8077
describe('Events w/ Crypto', () => {
8178
let store, collection
8279
beforeEach(function(done) {
83-
this.timeout(10000)
8480
store = new MongoStore({
8581
url: connectionString,
8682
mongoOptions: { useNewUrlParser: true },
@@ -93,20 +89,20 @@ describe('Events w/ Crypto', () => {
9389
})
9490
})
9591
afterEach(() => {
96-
store.close()
92+
return store.close()
9793
})
9894

9995
describe('set() with an unknown session id', () => {
10096
it('should emit a `create` event', done => {
10197
store.once('create', sid => {
102-
expect(sid).to.be('foo1')
98+
expect(sid).toBe('foo1')
10399
done()
104100
})
105101
store.set('foo1', { foo: 'bar' }, noop)
106102
})
107103
it('should emit a `set` event', done => {
108104
store.once('set', sid => {
109-
expect(sid).to.be('foo2')
105+
expect(sid).toBe('foo2')
110106
done()
111107
})
112108
store.set('foo2', { foo: 'bar' }, noop)
@@ -116,26 +112,26 @@ describe('Events w/ Crypto', () => {
116112
describe('set() with a session id associated to an existing session', () => {
117113
it('should emit an `update` event', done => {
118114
store.once('update', sid => {
119-
expect(sid).to.be('foo3')
115+
expect(sid).toBe('foo3')
120116
done()
121117
})
122118
collection.insertOne(
123119
{ _id: 'foo3', session: { foo: 'bar1' }, expires: futureDate },
124120
err => {
125-
expect(err).not.to.be.ok()
121+
expect(err).toBeFalsy()
126122
store.set('foo3', { foo: 'bar2' }, noop)
127123
}
128124
)
129125
})
130126
it('should emit an `set` event', done => {
131127
store.once('update', sid => {
132-
expect(sid).to.be('foo4')
128+
expect(sid).toBe('foo4')
133129
done()
134130
})
135131
collection.insertOne(
136132
{ _id: 'foo4', session: { foo: 'bar1' }, expires: futureDate },
137133
err => {
138-
expect(err).not.to.be.ok()
134+
expect(err).toBeFalsy()
139135
store.set('foo4', { foo: 'bar2' }, noop)
140136
}
141137
)

‎test/jest.setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.setTimeout(10000)

‎test/legacy-loader.js

-18
This file was deleted.

‎test/legacy-tests.js

-816
This file was deleted.

‎test/legacy-tests.test.js

+540
Large diffs are not rendered by default.

‎yarn.lock

+1,739-465
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.