Skip to content

Commit

Permalink
frontend: replace usages of jest with vi
Browse files Browse the repository at this point in the history
vitest also provides a global jest object
with the same interface.
After the switch to vitest, use the vi
object directly.
  • Loading branch information
BacLuc committed Nov 26, 2023
1 parent 1e95ec3 commit fc72d77
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiCheckbox from '../ApiCheckbox.vue'
import ApiWrapper from '@/components/form/api/ApiWrapper.vue'
import Vue from 'vue'
import Vuetify from 'vuetify'
import { vi } from 'vitest'
import flushPromises from 'flush-promises'
import formBaseComponents from '@/plugins/formBaseComponents'
import merge from 'lodash/merge'
Expand All @@ -26,7 +27,7 @@ describe('An ApiCheckbox', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
wrapper.destroy()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import user from '@testing-library/user-event'
import { ApiMock } from '@/components/form/api/__tests__/ApiMock'
import { extend } from 'vee-validate'
import { regex } from 'vee-validate/dist/rules'
import { vi } from 'vitest'

extend('regex', regex)

Expand All @@ -22,7 +23,7 @@ describe('An ApiColorPicker', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

test('triggers api.patch and status update if input changes', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiDatePicker from '../ApiDatePicker.vue'
import { screen, waitFor } from '@testing-library/vue'
import { render, setTestLocale } from '@/test/renderWithVuetify.js'
import user from '@testing-library/user-event'
import { vi } from 'vitest'
import { ApiMock } from '@/components/form/api/__tests__/ApiMock'

describe('An ApiDatePicker', () => {
Expand All @@ -19,7 +20,7 @@ describe('An ApiDatePicker', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('triggers api.patch and status update if input changes', async () => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/form/api/__tests__/ApiSelect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import flushPromises from 'flush-promises'
import ApiWrapper from '@/components/form/api/ApiWrapper.vue'
import { i18n } from '@/plugins'
import merge from 'lodash/merge'
import { vi } from 'vitest'
import { ApiMock } from '@/components/form/api/__tests__/ApiMock'
import { waitForDebounce } from '@/test/util'

Expand Down Expand Up @@ -40,7 +41,7 @@ describe('An ApiSelect', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
wrapper.destroy()
})

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/form/api/__tests__/ApiSwitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiSwitch from '../ApiSwitch.vue'
import ApiWrapper from '@/components/form/api/ApiWrapper.vue'
import Vue from 'vue'
import Vuetify from 'vuetify'
import { vi } from 'vitest'
import flushPromises from 'flush-promises'
import formBaseComponents from '@/plugins/formBaseComponents'
import merge from 'lodash/merge'
Expand All @@ -26,7 +27,7 @@ describe('An ApiSwitch', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
wrapper.destroy()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiTextField from '../ApiTextField.vue'
import ApiWrapper from '@/components/form/api/ApiWrapper.vue'
import Vue from 'vue'
import Vuetify from 'vuetify'
import { vi } from 'vitest'
import flushPromises from 'flush-promises'
import formBaseComponents from '@/plugins/formBaseComponents'
import merge from 'lodash/merge'
Expand Down Expand Up @@ -30,7 +31,7 @@ describe('An ApiTextField', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
wrapper.destroy()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApiTextarea from '@/components/form/api/ApiTextarea.vue'
import ApiWrapper from '@/components/form/api/ApiWrapper.vue'
import Vue from 'vue'
import Vuetify from 'vuetify'
import { vi } from 'vitest'
import flushPromises from 'flush-promises'
import formBaseComponents from '@/plugins/formBaseComponents'
import merge from 'lodash/merge'
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('An ApiTextarea', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
wrapper.destroy()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { screen, waitFor } from '@testing-library/vue'
import { render, setTestLocale } from '@/test/renderWithVuetify.js'
import user from '@testing-library/user-event'
import { ApiMock } from '@/components/form/api/__tests__/ApiMock'
import { vi } from 'vitest'

describe('An ApiTimePicker', () => {
let apiMock
Expand All @@ -19,7 +20,7 @@ describe('An ApiTimePicker', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('triggers api.patch and status update if input changes', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('Testing ApiWrapper [autoSave=true; manual external value]', () => {
wrapper = shallowMount(ApiWrapper, config)
vm = wrapper.vm

apiPatch = jest.spyOn(config.mocks.api, 'patch')
apiPatch = vi.spyOn(config.mocks.api, 'patch')

validateCalled = new Promise((resolve) => (validateResolveFunction = resolve))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Vuetify from 'vuetify'
import formBaseComponents from '@/plugins/formBaseComponents'

import { mount as mountComponent } from '@vue/test-utils'
import { vi } from 'vitest'
import ECheckbox from '../ECheckbox.vue'

Vue.use(Vuetify)
Expand Down Expand Up @@ -78,7 +79,7 @@ describe('An ECheckbox', () => {
await input.trigger('click')
expect(wrapper.vm.data).toBe(true)

jest.resetAllMocks()
vi.resetAllMocks()
await input.trigger('click')
expect(wrapper.vm.data).toBe(false)
})
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/form/base/__tests__/ESwitch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Vuetify from 'vuetify'
import formBaseComponents from '@/plugins/formBaseComponents'

import { mount as mountComponent } from '@vue/test-utils'
import { vi } from 'vitest'
import ESwitch from '@/components/form/base/ESwitch.vue'
import { touch } from '@/test/util'

Expand Down Expand Up @@ -88,7 +89,7 @@ describe('An ESwitch', () => {
touch(wrapper.find('.v-input--selection-controls__ripple')).start(0, 0).end(20, 0)
expect(wrapper.vm.data).toBe(true)

jest.resetAllMocks()
vi.resetAllMocks()
touch(wrapper.find('.v-input--selection-controls__ripple')).start(0, 0).end(-20, 0)
expect(wrapper.vm.data).toBe(false)
})
Expand All @@ -103,7 +104,7 @@ describe('An ESwitch', () => {
input.trigger('keydown.right')
expect(wrapper.vm.data).toBe(true)

jest.resetAllMocks()
vi.resetAllMocks()
input.trigger('keydown.left')
expect(wrapper.vm.data).toBe(false)
})
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/helpers/__tests__/serverError.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { transformViolations } from '@/helpers/serverError'
import cloneDeep from 'lodash/cloneDeep'
import { fallbackLocale } from '@/plugins/i18n'
import { vi } from 'vitest'

describe('transformViolations', () => {
describe('without i18n', () => {
Expand Down Expand Up @@ -31,8 +32,8 @@ describe('transformViolations', () => {
})

describe('with i18n', () => {
const te = jest.fn()
const tc = jest.fn()
const te = vi.fn()
const tc = vi.fn()
const i18n = { te, tc }

beforeEach(() => {
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/plugins/__tests__/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ vi.mock('@/router', async () => {

describe('authentication logic', () => {
afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
Cookies.remove('localhost_jwt_hp')
window.environment = cloneDeep(envBackup)
})
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('authentication logic', () => {
it('sends a POST request to the API', async () => {
// given
store.replaceState(createState())
jest.spyOn(apiStore, 'post').mockImplementation(async () => {})
vi.spyOn(apiStore, 'post').mockImplementation(async () => {})

// when
await auth.register({ email: 'bar', password: 'baz' })
Expand All @@ -128,7 +128,7 @@ describe('authentication logic', () => {
it('resolves to true if the user successfully logs in', async () => {
// given
store.replaceState(createState())
jest.spyOn(apiStore, 'post').mockImplementation(async () => {
vi.spyOn(apiStore, 'post').mockImplementation(async () => {
Cookies.set('localhost_jwt_hp', validJWTPayload)
})

Expand All @@ -146,7 +146,7 @@ describe('authentication logic', () => {

it('resolves to false if the login fails', async () => {
// given
jest.spyOn(apiStore, 'post').mockImplementation(async () => {
vi.spyOn(apiStore, 'post').mockImplementation(async () => {
// login fails, no cookie added
})

Expand All @@ -167,7 +167,7 @@ describe('authentication logic', () => {
it('resolves to null if not logged in', async () => {
// given
store.replaceState(createState())
jest.spyOn(apiStore, 'get')
vi.spyOn(apiStore, 'get')

// when
const result = await auth.loadUser()
Expand All @@ -181,7 +181,7 @@ describe('authentication logic', () => {
// given
store.replaceState(createState())
Cookies.set('localhost_jwt_hp', validJWTPayload)
jest.spyOn(apiStore, 'get')
vi.spyOn(apiStore, 'get')

// when
const result = await auth.loadUser()
Expand All @@ -208,8 +208,8 @@ describe('authentication logic', () => {
}),
},
}
jest.spyOn(apiStore, 'get').mockImplementation(() => user)
jest.spyOn(auth, 'logout')
vi.spyOn(apiStore, 'get').mockImplementation(() => user)
vi.spyOn(auth, 'logout')

// when
const result = await auth.loadUser()
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/views/camp/__tests__/Dashboard.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Dashboard from '../Dashboard.vue'
import { shallowMount } from '@vue/test-utils'
import { vi } from 'vitest'

describe('Dashboard view', () => {
it('Renders View', async () => {
Expand Down Expand Up @@ -29,10 +30,10 @@ const ROUTE = () => ({
query: {},
})
const ROUTER = () => ({
replace: jest.fn(),
replace: vi.fn(),
})
const AUTH = {
loadUser: jest.fn(),
loadUser: vi.fn(),
}

function createCampWithRole(role) {
Expand Down

0 comments on commit fc72d77

Please sign in to comment.