Skip to content

Commit adb40d1

Browse files
committed
Move from jest to vitest for better integration with vite
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
1 parent 0294cc5 commit adb40d1

16 files changed

Lines changed: 127 additions & 114 deletions

File tree

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"l10n:extract": "node build/extract-l10n.js",
2020
"lint": "eslint --ext .js,.vue src",
2121
"lint:fix": "eslint --ext .js,.vue src --fix",
22-
"test": "jest --verbose",
23-
"test:coverage": "jest --verbose --coverage --no-cache",
22+
"test": "vitest run",
23+
"test:coverage": "vitest run --coverage",
2424
"stylelint": "stylelint src/**/*.vue src/**/*.scss src/**/*.css",
2525
"stylelint:fix": "stylelint src/**/*.vue src/**/*.scss src/**/*.css --fix",
2626
"styleguide": "vue-styleguidist server",
@@ -92,20 +92,21 @@
9292
"@vitejs/plugin-vue2": "^2.0.1",
9393
"@vitest/coverage-c8": "^0.25.3",
9494
"@vue/compiler-sfc": "^2.7.14",
95-
"babel-loader": "^8.2.5",
95+
"@vue/test-utils": "^1.3.3",
9696
"browserslist-to-esbuild": "^1.2.0",
9797
"css-loader": "~6.7.1",
9898
"cypress": "^9.7.0",
9999
"cypress-visual-regression": "^1.5.0",
100100
"eslint-plugin-cypress": "^2.11.1",
101101
"gettext-extractor": "^3.6.0",
102102
"gettext-parser": "^6.0.0",
103-
"glob": "^8.0.3",
103+
"jsdom": "^20.0.3",
104104
"rollup-plugin-inject-process-env": "^1.3.1",
105105
"rollup-plugin-node-externals": "^5.0.2",
106106
"sanitize-filename": "^1.6.3",
107107
"sass": "^1.56.1",
108108
"vite": "^3.2.4",
109+
"vitest": "^0.25.3",
109110
"vue-eslint-parser": "^9.1.0"
110111
},
111112
"browserslist": [

tests/.eslintrc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
env: {
3-
jest: true,
4-
"cypress/globals": true
3+
'jest/gobals': false,
4+
'cypress/globals': true,
55
},
66
extends: [
7-
"plugin:cypress/recommended"
7+
'plugin:cypress/recommended',
88
],
99
}

tests/OC.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
Util: {
1616
naturalSortCompare(a, b) {
1717
return 0
18-
}
18+
},
1919
},
2020

2121
coreApps: [

tests/setup.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
*
2121
*/
2222

23+
import { vi } from 'vitest'
2324
import OC from './OC.js'
24-
// eslint-disable-next-line node/no-unpublished-import
25-
import 'regenerator-runtime/runtime'
2625

27-
global.OC = OC
28-
29-
global.TRANSLATIONS = []
30-
global.SCOPE_VERSION = 1
26+
vi.stubGlobal('OC', OC)

tests/unit/components/NcActions/NcActions.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @author Raimund Schlüßler <raimund.schluessler@mailbox.org>
55
*
6-
* @license GNU AGPL version 3 or any later version
6+
* @license AGPL-3.0-or-later
77
*
88
* This program is free software: you can redistribute it and/or modify
99
* it under the terms of the GNU Affero General Public License as
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import { mount } from '@vue/test-utils'
24+
import { beforeEach, describe, expect, it } from 'vitest'
2425

2526
import NcActions from '../../../../src/components/NcActions/NcActions.vue'
2627
import NcActionButton from '../../../../src/components/NcActionButton/NcActionButton.vue'

tests/unit/components/NcAppNavigation/NcAppNavigation.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @author Azul <azul@riseup.net>
55
*
6-
* @license GNU AGPL version 3 or any later version
6+
* @license AGPL-3.0-or-later
77
*
88
* This program is free software: you can redistribute it and/or modify
99
* it under the terms of the GNU Affero General Public License as
@@ -22,6 +22,8 @@
2222

2323
import { mount } from '@vue/test-utils'
2424
import { emit } from '@nextcloud/event-bus'
25+
import { describe, expect, it } from 'vitest'
26+
2527
import NcAppNavigation from '../../../../src/components/NcAppNavigation/NcAppNavigation.vue'
2628

2729
describe('NcAppNavigation.vue', () => {
@@ -36,14 +38,14 @@ describe('NcAppNavigation.vue', () => {
3638
describe('toggle via event bus', () => {
3739
it('toggle to turn it off', () => {
3840
const wrapper = mount(NcAppNavigation)
39-
emit('toggle-navigation', {open: undefined})
41+
emit('toggle-navigation', { open: undefined })
4042
expect(wrapper.vm.$data.open).toBe(false)
4143
})
4244

4345
it('toggle with open: false keeps it closed', () => {
4446
const wrapper = mount(NcAppNavigation)
45-
emit('toggle-navigation', {open: false})
46-
emit('toggle-navigation', {open: false})
47+
emit('toggle-navigation', { open: false })
48+
emit('toggle-navigation', { open: false })
4749
expect(wrapper.vm.$data.open).toBe(false)
4850
})
4951

tests/unit/components/NcAppSidebar/NcAppSidebarTabs.spec.js

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* @author Raimund Schlüßler <raimund.schluessler@mailbox.org>
66
*
7-
* @license GNU AGPL version 3 or any later version
7+
* @license AGPL-3.0-or-later
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Affero General Public License as
@@ -22,35 +22,34 @@
2222
*/
2323

2424
import { mount } from '@vue/test-utils'
25+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2526

2627
import Vue from 'vue'
2728
import NcAppSidebarTabs from '../../../../src/components/NcAppSidebar/NcAppSidebarTabs.vue'
2829
import NcAppSidebarTab from '../../../../src/components/NcAppSidebarTab/NcAppSidebarTab.vue'
2930
import NcActionButton from '../../../../src/components/NcActionButton/NcActionButton.vue'
3031

31-
let onWarning
32-
let consoleDebug
33-
34-
let wrapper
35-
3632
const initialConsole = { ...console }
3733

3834
describe('NcAppSidebarTabs.vue', () => {
3935
'use strict'
40-
beforeEach(() => {
41-
onWarning = jest.fn()
42-
consoleDebug = jest.fn()
43-
Vue.config.warnHandler = onWarning
44-
global.console = { ...console, debug: consoleDebug }
36+
37+
beforeEach((ctx) => {
38+
ctx.onWarning = vi.fn()
39+
ctx.consoleDebug = vi.fn()
40+
Vue.config.warnHandler = ctx.onWarning
41+
global.console = { ...console, debug: ctx.consoleDebug }
4542
})
43+
4644
afterEach(() => {
4745
Vue.config.warnHandler = () => null
4846
global.console = initialConsole
4947
})
48+
5049
describe('when using the component without tabs', () => {
5150
describe('with only one div', () => {
52-
beforeEach(() => {
53-
wrapper = mount(NcAppSidebarTabs, {
51+
beforeEach((ctx) => {
52+
ctx.wrapper = mount(NcAppSidebarTabs, {
5453
propsData: {
5554
title: 'Sidebar title.',
5655
},
@@ -59,17 +58,22 @@ describe('NcAppSidebarTabs.vue', () => {
5958
},
6059
})
6160
})
62-
it('Issues no warning nor logs to console.', () => {
61+
62+
it('does not display the nav element', ({wrapper}) => {
63+
expect(wrapper.find('nav').exists()).toBe(false)
64+
})
65+
it('Issues no warning nor logs to console.', ({ consoleDebug, onWarning }) => {
6366
expect(onWarning).toHaveBeenCalledTimes(0)
6467
expect(consoleDebug).toHaveBeenCalledTimes(0)
6568
})
66-
it('does not display the nav element', () => {
69+
it('does not display the nav element', ({ wrapper }) => {
6770
expect(wrapper.find('nav').exists()).toBe(false)
6871
})
6972
})
73+
7074
describe('with div and secondary action', () => {
71-
beforeEach(() => {
72-
wrapper = mount(NcAppSidebarTabs, {
75+
beforeEach((ctx) => {
76+
ctx.wrapper = mount(NcAppSidebarTabs, {
7377
propsData: {
7478
title: 'Sidebar title.',
7579
},
@@ -83,19 +87,16 @@ describe('NcAppSidebarTabs.vue', () => {
8387
},
8488
})
8589
})
86-
it('Issues no warning.', () => {
90+
it('Issues no warning.', ({ onWarning }) => {
8791
expect(onWarning).toHaveBeenCalledTimes(0)
8892
})
8993
})
90-
91-
it('does not display the nav element', () => {
92-
expect(wrapper.find('nav').exists()).toBe(false)
93-
})
9494
})
95+
9596
describe('when only children of type AppSidebarTab is used', () => {
9697
describe('when 3 children of type AppSidebarTab are used', () => {
97-
beforeEach(() => {
98-
wrapper = mount(NcAppSidebarTabs, {
98+
beforeEach((ctx) => {
99+
ctx.wrapper = mount(NcAppSidebarTabs, {
99100
slots: {
100101
default: [
101102
'<nc-app-sidebar-tab id="first" icon="icon-details" name="Tab1">Tab1</nc-app-sidebar-tab>',
@@ -109,67 +110,69 @@ describe('NcAppSidebarTabs.vue', () => {
109110
},
110111
})
111112
})
112-
it('Issues no warning.', () => {
113+
114+
it('Issues no warning.', ({ onWarning }) => {
113115
expect(onWarning).toHaveBeenCalledTimes(0)
114116
})
115-
it('display the nav element', () => {
117+
it('display the nav element', ({ wrapper }) => {
116118
expect(wrapper.find('nav').exists()).toBe(true)
117119
})
118-
it('display all the 3 elements in li', () => {
120+
it('display all the 3 elements in li', ({ wrapper }) => {
119121
const liList = wrapper.findAll('nav>ul>li')
120122
expect(liList.length).toEqual(3)
121123
})
122-
it('emit "update:active" event with the selected tab id when clicking on a tab', () => {
124+
it('emit "update:active" event with the selected tab id when clicking on a tab', ({ wrapper }) => {
123125
const lastLink = wrapper.find('nav>ul>li:last-of-type>a')
124126
lastLink.trigger('click')
125127
expect(wrapper.emitted('update:active')[0]).toEqual(['last'])
126128
})
127-
it('emit "update:active" event with the first tab id when keydown pageup is pressed', () => {
129+
it('emit "update:active" event with the first tab id when keydown pageup is pressed', ({ wrapper }) => {
128130
const lastLink = wrapper.find('nav>ul>li:last-of-type>a')
129-
lastLink.trigger('keydown.pageup')
131+
lastLink.trigger('keydown', { keyCode: 33 })
130132
expect(wrapper.emitted('update:active')[0]).toEqual(['first'])
131133
})
132-
it('emit "update:active" event with the last tab id when keydown pagedown is pressed', () => {
134+
it('emit "update:active" event with the last tab id when keydown pagedown is pressed', ({ wrapper }) => {
133135
const lastLink = wrapper.find('nav>ul>li:last-of-type>a')
134-
lastLink.trigger('keydown.pagedown')
136+
lastLink.trigger('keydown', { keyCode: 34 })
135137
expect(wrapper.emitted('update:active')[0]).toEqual(['last'])
136138
})
137139
describe('when we select the first element', () => {
138-
beforeEach(() => {
139-
wrapper.setData({ activeTab: 'first' })
140+
beforeEach((ctx) => {
141+
ctx.wrapper.setData({ activeTab: 'first' })
140142
})
141-
it('does not emit "update:active" event when keydown left is pressed', () => {
143+
it('does not emit "update:active" event when keydown left is pressed', ({ wrapper }) => {
142144
expect(wrapper.emitted('update:active')).toBeFalsy()
143145
const firstLink = wrapper.find('nav>ul>li>a')
144146
firstLink.trigger('keydown.left')
145147
expect(wrapper.emitted('update:active')).toBeFalsy()
146148
})
147-
it('emit "update:active" event with the next tab id when keydown right is pressed', () => {
149+
it('emit "update:active" event with the next tab id when keydown right is pressed', ({ wrapper }) => {
148150
const firstLink = wrapper.find('nav>ul>li>a')
149151
firstLink.trigger('keydown.right')
150152
expect(wrapper.emitted('update:active')[0]).toEqual(['second'])
151153
})
152154
})
153155
describe('when we select the last element', () => {
154-
beforeEach(() => {
155-
wrapper.setData({ activeTab: 'last' })
156+
beforeEach((ctx) => {
157+
ctx.wrapper.setData({ activeTab: 'last' })
156158
})
157-
it('emit "update:active" event with the previous tab id when keydown left is pressed', () => {
159+
it('emit "update:active" event with the previous tab id when keydown left is pressed', ({ wrapper }) => {
158160
const lastLink = wrapper.find('nav>ul>li:last-of-type>a')
159161
lastLink.trigger('keydown.left')
160162
expect(wrapper.emitted('update:active')[0]).toEqual(['second'])
161163
})
162-
it('does not emit "update:active" event when keydown right is pressed', () => {
164+
it('does not emit "update:active" event when keydown right is pressed', ({ wrapper }) => {
163165
expect(wrapper.emitted('update:active')).toBeFalsy()
164166
const lastLink = wrapper.find('nav>ul>li:last-of-type>a')
165167
lastLink.trigger('keydown.right')
166168
expect(wrapper.emitted('update:active')).toBeFalsy()
167169
})
168170
})
169171
})
172+
170173
describe('when they is only 1 child of type AppSidebarTab are used', () => {
171-
beforeEach(() => {
172-
wrapper = mount(NcAppSidebarTabs, {
174+
beforeEach((ctx) => {
175+
ctx.wrapper = mount(NcAppSidebarTabs, {
173176
slots: {
174177
default: [
175178
'<nc-app-sidebar-tab id="1" icon="icon-details" name="Tab1">Tab1</nc-app-sidebar-tab>',
@@ -181,16 +184,16 @@ describe('NcAppSidebarTabs.vue', () => {
181184
},
182185
})
183186
})
184-
it('Issues no warning.', () => {
187+
it('Issues no warning.', ({ onWarning }) => {
185188
expect(onWarning).toHaveBeenCalledTimes(0)
186189
})
187-
it('does not display the nav element', () => {
190+
it('does not display the nav element', ({ wrapper }) => {
188191
expect(wrapper.find('nav').exists()).toBe(false)
189192
})
190193
})
191194
})
192195
describe('when tabs and other elements are mixed', () => {
193-
it('Issues a warning and logs to console .', () => {
196+
it('Issues a warning and logs to console .', ({ consoleDebug, onWarning }) => {
194197
mount(NcAppSidebarTabs, {
195198
slots: {
196199
default: [

tests/unit/components/NcHighlight/NcHighlight.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @author Raimund Schlüßler <raimund.schluessler@mailbox.org>
55
*
6-
* @license GNU AGPL version 3 or any later version
6+
* @license AGPL-3.0-or-later
77
*
88
* This program is free software: you can redistribute it and/or modify
99
* it under the terms of the GNU Affero General Public License as
@@ -21,6 +21,8 @@
2121
*/
2222

2323
import { mount } from '@vue/test-utils'
24+
import { describe, expect, it } from 'vitest'
25+
2426
import NcHighlight from '../../../../src/components/NcHighlight/NcHighlight.vue'
2527

2628
describe('NcHighlight.vue', () => {

tests/unit/components/NcMultiselect/NcMultiselect.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @author Raimund Schlüßler <raimund.schluessler@mailbox.org>
55
*
6-
* @license GNU AGPL version 3 or any later version
6+
* @license AGPL-3.0-or-later
77
*
88
* This program is free software: you can redistribute it and/or modify
99
* it under the terms of the GNU Affero General Public License as
@@ -21,13 +21,15 @@
2121
*/
2222

2323
import { mount } from '@vue/test-utils'
24+
import { describe, expect, it, vi } from 'vitest'
25+
2426
import NcMultiselect from '../../../../src/components/NcMultiselect/NcMultiselect.vue'
2527

2628
describe('NcMultiselect.vue', () => {
2729
'use strict'
2830

2931
it('Test that the input function is only called once on select', () => {
30-
const onInput = jest.fn()
32+
const onInput = vi.fn()
3133
const wrapper = mount(NcMultiselect, {
3234
propsData: {
3335
label: 'name',
@@ -39,7 +41,7 @@ describe('NcMultiselect.vue', () => {
3941
},
4042
listeners: {
4143
input: onInput
42-
}
44+
},
4345
})
4446
wrapper.trigger('click')
4547
wrapper.find('li.multiselect__element .multiselect__option').trigger('click')

0 commit comments

Comments
 (0)