Skip to content

Commit a1c7274

Browse files
committed
Replace karma/mocha with vitest for unit tests.
1 parent 2e44be1 commit a1c7274

23 files changed

+3682
-3160
lines changed

karma.conf.js

-34
This file was deleted.

package.json

+11-24
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,32 @@
88
"docs": "rm -rf docs/; jsdoc -c jsdoc.json",
99
"e2e": "concurrently --success first --kill-others \"npm:fakeapiserver\" \"vue-cli-service test:e2e --env chrome --headless\"",
1010
"fakeapiserver": "node examples/fake-json-api-server.js",
11-
"test": "npm run unit -- --single-run && npm run e2e",
11+
"test": "npm run unit && npm run e2e",
1212
"testapp": "concurrently --kill-others \"npm:fakeapiserver\" \"vue-cli-service serve\"",
13-
"unit": "karma start"
13+
"unit": "vitest run"
1414
},
1515
"main": "src/jsonapi-vuex.js",
1616
"files": [
1717
"src/"
1818
],
1919
"dependencies": {
2020
"jsonpath-plus": "^7.2.0",
21-
"lodash": "^4.17.21"
21+
"lodash": "^4.17.21",
22+
"vitest": "^0.34.6"
2223
},
2324
"devDependencies": {
24-
"@babel/core": "^7.21.0",
25-
"@babel/eslint-parser": "^7.19.1",
26-
"@babel/preset-env": "^7.20.2",
2725
"@vue/cli-plugin-babel": "~5.0.8",
2826
"@vue/cli-plugin-e2e-nightwatch": "~5.0.8",
2927
"@vue/cli-plugin-eslint": "~5.0.8",
3028
"@vue/cli-plugin-vuex": "~5.0.8",
3129
"@vue/cli-service": "~5.0.8",
3230
"@vue/test-utils": "^2.3.1",
33-
"axios": "^1.3.4",
34-
"axios-mock-adapter": "^1.21.2",
35-
"babel-loader": "^9.1.2",
36-
"babel-plugin-istanbul": "^6.1.1",
37-
"babel-polyfill": "^6.26.0",
38-
"chai": "^4.3.7",
39-
"chai-as-promised": "^7.1.1",
31+
"axios": "^1.6.2",
32+
"axios-mock-adapter": "^1.22.0",
33+
"@babel/core": "^7.21.0",
34+
"@babel/eslint-parser": "^7.19.1",
35+
"@babel/preset-env": "^7.20.2",
36+
"chai": "^4.3.10",
4037
"chromedriver": "^118.0.0",
4138
"concurrently": "^7.6.0",
4239
"core-js": "^3.29.1",
@@ -50,20 +47,10 @@
5047
"geckodriver": "^3.2.0",
5148
"husky": "^8.0.3",
5249
"jsdoc": "^4.0.2",
53-
"karma": "^6.4.1",
54-
"karma-chai": "^0.1.0",
55-
"karma-coverage": "^2.2.0",
56-
"karma-firefox-launcher": "^2.1.2",
57-
"karma-mocha": "^2.0.1",
58-
"karma-sinon": "^1.0.5",
59-
"karma-verbose-reporter": "^0.0.8",
60-
"karma-webpack": "^5.0.0",
6150
"lint-staged": "^13.2.0",
62-
"mocha": "^10.2.0",
63-
"mocha-eslint": "^7.0.0",
6451
"nightwatch": "^2.6.17",
6552
"prettier": "^2.8.4",
66-
"sinon": "^15.0.2",
53+
"sinon": "^17.0.1",
6754
"sinon-chai": "^3.7.0",
6855
"vue": "^3.2.47",
6956
"vuex": "^4.1.0",

tests/unit/actions/create.spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { expect } from 'chai'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import { makeApi } from '../server'
3+
let api
24

35
import createJsonapiModule from '../utils/createJsonapiModule'
46

57
describe('create', function () {
68
let jsonapiModule
79

810
beforeEach(function () {
9-
jsonapiModule = createJsonapiModule(this.api)
11+
[ api ] = makeApi()
12+
jsonapiModule = createJsonapiModule(api)
1013
})
1114

12-
it('should be an alias for post', function () {
15+
test('should be an alias for post', function () {
1316
expect(jsonapiModule.actions.create).to.equal(jsonapiModule.actions.post)
1417
})
1518
})

tests/unit/actions/delete.spec.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { expect } from 'chai'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import sinonChai from 'sinon-chai'
3+
import sinon from 'sinon'
4+
import chai from 'chai'
5+
chai.use(sinonChai)
6+
7+
import { makeApi } from '../server'
8+
let api, mockApi
29

310
import createStubContext from '../stubs/context'
411
import createJsonapiModule from '../utils/createJsonapiModule'
@@ -8,66 +15,67 @@ describe('delete', function () {
815
let jsonWidget1, normWidget1, jsonapiModule, stubContext
916

1017
beforeEach(function () {
18+
[ api, mockApi ] = makeApi()
1119
jsonWidget1 = createJsonWidget1()
1220
normWidget1 = createNormWidget1()
1321

14-
jsonapiModule = createJsonapiModule(this.api)
22+
jsonapiModule = createJsonapiModule(api)
1523
stubContext = createStubContext(jsonapiModule)
1624
})
1725

18-
it('should make an api call to DELETE item(s)', async function () {
19-
this.mockApi.onAny().reply(204)
26+
test('should make an api call to DELETE item(s)', async function () {
27+
mockApi.onAny().reply(204)
2028

2129
await jsonapiModule.actions.delete(stubContext, normWidget1)
2230

23-
expect(this.mockApi.history.delete[0].url).to.equal(normWidget1['_jv']['links']['self'])
31+
expect(mockApi.history.delete[0].url).to.equal(normWidget1['_jv']['links']['self'])
2432
})
2533

26-
it('should accept axios config as the 2nd arg in a list', async function () {
27-
this.mockApi.onAny().reply(200, { data: jsonWidget1 })
34+
test('should accept axios config as the 2nd arg in a list', async function () {
35+
mockApi.onAny().reply(200, { data: jsonWidget1 })
2836
const params = { filter: 'color' }
2937

3038
await jsonapiModule.actions.delete(stubContext, [normWidget1, { params: params }])
3139

32-
expect(this.mockApi.history.delete[0].params).to.deep.equal(params)
40+
expect(mockApi.history.delete[0].params).to.deep.equal(params)
3341
})
3442

35-
it('should allow the endpoint url to be overridden in config', async function () {
36-
this.mockApi.onAny().reply(200, { data: jsonWidget1 })
43+
test('should allow the endpoint url to be overridden in config', async function () {
44+
mockApi.onAny().reply(200, { data: jsonWidget1 })
3745
const url = '/fish/1'
3846

3947
await jsonapiModule.actions.delete(stubContext, [normWidget1, { url: url }])
40-
expect(this.mockApi.history.delete[0].url).to.equal(url)
48+
expect(mockApi.history.delete[0].url).to.equal(url)
4149
})
4250

43-
it('should delete a record from the store', async function () {
44-
this.mockApi.onAny().reply(204)
51+
test('should delete a record from the store', async function () {
52+
mockApi.onAny().reply(204)
4553

4654
await jsonapiModule.actions.delete(stubContext, normWidget1)
4755

4856
expect(stubContext.commit).to.have.been.calledWith('deleteRecord', normWidget1)
4957
})
5058

51-
it('should delete a record (string) from the store', async function () {
52-
this.mockApi.onAny().reply(204)
59+
test('should delete a record (string) from the store', async function () {
60+
mockApi.onAny().reply(204)
5361

5462
// Leading slash is incorrect syntax, but we should handle it so test with it in
5563
await jsonapiModule.actions.delete(stubContext, 'widget/1')
5664

5765
expect(stubContext.commit).to.have.been.calledWith('deleteRecord', 'widget/1')
5866
})
5967

60-
it('should return deleted object if passed back by server', async function () {
61-
this.mockApi.onAny().reply(200, { data: jsonWidget1 })
68+
test('should return deleted object if passed back by server', async function () {
69+
mockApi.onAny().reply(200, { data: jsonWidget1 })
6270

6371
// Leading slash is incorrect syntax, but we should handle it so test with it in
6472
let res = await jsonapiModule.actions.delete(stubContext, normWidget1)
6573

6674
expect(res).to.deep.equal(normWidget1)
6775
})
6876

69-
it('should handle API errors', async function () {
70-
this.mockApi.onAny().reply(500)
77+
test('should handle API errors', async function () {
78+
mockApi.onAny().reply(500)
7179

7280
try {
7381
await jsonapiModule.actions.delete(stubContext, normWidget1)

tests/unit/actions/deleteRelated.spec.js

+33-30
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { expect } from 'chai'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import { makeApi } from '../server'
3+
let api, mockApi
24

35
import createStubContext from '../stubs/context'
46
import createJsonapiModule from '../utils/createJsonapiModule'
@@ -9,14 +11,15 @@ describe('deleteRelated', function () {
911
let normWidget1, jsonWidget1, jsonapiModule, stubContext
1012

1113
beforeEach(function () {
14+
[ api, mockApi ] = makeApi()
1215
normWidget1 = createNormWidget1()
1316
jsonWidget1 = createJsonWidget1()
1417

15-
jsonapiModule = createJsonapiModule(this.api)
18+
jsonapiModule = createJsonapiModule(api)
1619
stubContext = createStubContext(jsonapiModule)
1720
})
1821

19-
it('Should throw an error if passed an object with no type or id', async function () {
22+
test('Should throw an error if passed an object with no type or id', async function () {
2023
try {
2124
await jsonapiModule.actions.deleteRelated(stubContext, { _jv: {} })
2225
throw 'Should have thrown an error (no id)'
@@ -25,7 +28,7 @@ describe('deleteRelated', function () {
2528
}
2629
})
2730

28-
it('Should throw an error if passed an object with no relationships', async function () {
31+
test('Should throw an error if passed an object with no relationships', async function () {
2932
try {
3033
await jsonapiModule.actions.deleteRelated(stubContext, { _jv: { type: 'widget', id: 1 } })
3134
throw 'Should have thrown an error (no relationships)'
@@ -34,24 +37,24 @@ describe('deleteRelated', function () {
3437
}
3538
})
3639

37-
it('should make a delete request for the object passed in.', async function () {
38-
this.mockApi.onDelete().replyOnce(204)
39-
this.mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
40+
test('should make a delete request for the object passed in.', async function () {
41+
mockApi.onDelete().replyOnce(204)
42+
mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
4043

4144
const rel = { data: { type: 'widget', id: '2' } }
4245
normWidget1['_jv']['relationships'] = { widgets: rel }
4346

4447
await jsonapiModule.actions.deleteRelated(stubContext, normWidget1)
4548
// Expect a delete call to rel url, with rel payload, then get object to update store
46-
expect(this.mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
47-
expect(this.mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel))
48-
expect(this.mockApi.history.get[0].params).to.have.property('include')
49-
expect(this.mockApi.history.get[0].url).to.equal('widget/1')
49+
expect(mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
50+
expect(mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel))
51+
expect(mockApi.history.get[0].params).to.have.property('include')
52+
expect(mockApi.history.get[0].url).to.equal('widget/1')
5053
})
5154

52-
it('should make a delete request for the object passed in.', async function () {
53-
this.mockApi.onDelete().replyOnce(204)
54-
this.mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
55+
test('should make a delete request for the object passed in.', async function () {
56+
mockApi.onDelete().replyOnce(204)
57+
mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
5558

5659
const rel = { data: { type: 'widget', id: '2' } }
5760
normWidget1['_jv']['relationships'] = { widgets: rel }
@@ -60,33 +63,33 @@ describe('deleteRelated', function () {
6063

6164
await jsonapiModule.actions.deleteRelated(stubContext, normWidget1)
6265
// Expect a delete call to rel url, with rel payload, then get object to update store
63-
expect(this.mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
64-
expect(this.mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel))
65-
expect(this.mockApi.history.get[0].params).to.not.have.property('include')
66-
expect(this.mockApi.history.get[0].url).to.equal('widget/1')
66+
expect(mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
67+
expect(mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel))
68+
expect(mockApi.history.get[0].params).to.not.have.property('include')
69+
expect(mockApi.history.get[0].url).to.equal('widget/1')
6770
})
6871

69-
it('should handle multiple relationships', async function () {
70-
this.mockApi.onDelete().reply(204)
71-
this.mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
72+
test('should handle multiple relationships', async function () {
73+
mockApi.onDelete().reply(204)
74+
mockApi.onGet().replyOnce(200, { data: jsonWidget1 })
7275

7376
const rel1 = { data: { type: 'widget', id: '2' } }
7477
const rel2 = { data: { type: 'doohickey', id: '3' } }
7578
normWidget1['_jv']['relationships'] = { widgets: rel1, doohickeys: rel2 }
7679

7780
await jsonapiModule.actions.deleteRelated(stubContext, normWidget1)
78-
expect(this.mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
79-
expect(this.mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel1))
80-
expect(this.mockApi.history.delete[1].url).to.equal('widget/1/relationships/doohickeys')
81-
expect(this.mockApi.history.delete[1].data).to.deep.equal(JSON.stringify(rel2))
82-
expect(this.mockApi.history.get[0].params).to.have.property('include')
83-
expect(this.mockApi.history.get[0].url).to.equal('widget/1')
81+
expect(mockApi.history.delete[0].url).to.equal('widget/1/relationships/widgets')
82+
expect(mockApi.history.delete[0].data).to.deep.equal(JSON.stringify(rel1))
83+
expect(mockApi.history.delete[1].url).to.equal('widget/1/relationships/doohickeys')
84+
expect(mockApi.history.delete[1].data).to.deep.equal(JSON.stringify(rel2))
85+
expect(mockApi.history.get[0].params).to.have.property('include')
86+
expect(mockApi.history.get[0].url).to.equal('widget/1')
8487
// Only get the object once at end
85-
expect(this.mockApi.history.get.length).to.equal(1)
88+
expect(mockApi.history.get.length).to.equal(1)
8689
})
8790

88-
it('Should handle API errors (in the data)', async function () {
89-
this.mockApi.onDelete().reply(500)
91+
test('Should handle API errors (in the data)', async function () {
92+
mockApi.onDelete().reply(500)
9093

9194
const rel = { data: { type: 'widget', id: '2' } }
9295
normWidget1['_jv']['relationships'] = { widgets: rel }

tests/unit/actions/fetch.spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { expect } from 'chai'
1+
import { beforeEach, describe, expect, test } from 'vitest'
2+
import { makeApi } from '../server'
3+
let api
24

35
import createJsonapiModule from '../utils/createJsonapiModule'
46

57
describe('fetch', function () {
68
let jsonapiModule
79

810
beforeEach(function () {
9-
jsonapiModule = createJsonapiModule(this.api)
11+
[ api ] = makeApi()
12+
jsonapiModule = createJsonapiModule(api)
1013
})
1114

12-
it('should be an alias for get', function () {
15+
test('should be an alias for get', function () {
1316
expect(jsonapiModule.actions.fetch).to.equal(jsonapiModule.actions.get)
1417
})
1518
})

0 commit comments

Comments
 (0)