Skip to content

Commit

Permalink
feat: Migrate to vitest for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Apr 21, 2024
1 parent 9f10aac commit ca78970
Show file tree
Hide file tree
Showing 10 changed files with 3,432 additions and 6,860 deletions.
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

10,214 changes: 3,388 additions & 6,826 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"dev": "vite --mode development build --watch",
"lint": "eslint .",
"lint:fix": "eslint --fix lib tests",
"test": "jest --verbose",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage"
"test": "vitest run",
"test:watch": "vitest watch",
"test:coverage": "vitest run --coverage"
},
"keywords": [
"nextcloud"
Expand All @@ -40,6 +40,8 @@
"dependencies": {
"@nextcloud/router": "^2.2.1",
"@nextcloud/typings": "^1.8.0",
"@types/dompurify": "^3.0.5",
"@types/escape-html": "^1.0.4",
"dompurify": "^3.1.0",
"escape-html": "^1.0.3",
"node-gettext": "^3.0.0"
Expand All @@ -49,14 +51,13 @@
"@nextcloud/eslint-config": "^8.3.0",
"@nextcloud/vite-config": "^1.2.2",
"@types/node-gettext": "^3.0",
"@vitest/coverage-v8": "^1.5.0",
"@zamiell/typedoc-plugin-not-exported": "^0.2.0",
"check-es-compat": "^3.2.1",
"gettext-parser": "^8.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"mock-xmlhttprequest": "^8.3.0",
"typedoc": "^0.25.13",
"vite": "^5.2.10"
"vite": "^5.2.10",
"vitest": "^1.5.0"
},
"browserslist": [
"extends @nextcloud/browserslist-config"
Expand Down
3 changes: 2 additions & 1 deletion tests/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/// <reference types="@nextcloud/typings" />
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { getDayNames, getDayNamesMin, getDayNamesShort, getFirstDay, getMonthNames, getMonthNamesShort } from '../lib/date'

declare let window: Nextcloud.v24.WindowWithGlobals
Expand All @@ -11,7 +12,7 @@ describe('date', () => {
console.warn = orginalWarn
})
beforeEach(() => {
console.warn = jest.fn()
console.warn = vi.fn()
})

describe('getFirstDay', () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/gettext.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { po } from 'gettext-parser'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { getGettextBuilder } from '../lib/gettext.ts'

describe('gettext', () => {
beforeEach(() => {
jest.spyOn(console, 'warn')
vi.spyOn(console, 'warn')
console.warn.mockImplementation(() => {})
})

Expand Down
23 changes: 12 additions & 11 deletions tests/loadTranslations.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { MockXhrServer, newServer } from 'mock-xmlhttprequest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { loadTranslations, register, translate, unregister } from '../lib/translation'

const setLocale = (locale) => document.documentElement.setAttribute('data-locale', locale)
const setLocale = (locale: string) => document.documentElement.setAttribute('data-locale', locale)

describe('loadTranslations', () => {
let server: MockXhrServer
Expand Down Expand Up @@ -57,15 +58,15 @@ describe('loadTranslations', () => {

afterEach(() => {
server.remove()
jest.clearAllMocks()
vi.clearAllMocks()
})

it('calls callback if app already exists', async () => {
register('myapp', {
Bye: 'Tschüss',
})

const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('myapp', callback)
// Callback called
Expand All @@ -85,7 +86,7 @@ describe('loadTranslations', () => {

it('calls callback if locale is English', async () => {
setLocale('en')
const callback = jest.fn()
const callback = vi.fn()

try {
await loadTranslations('myapp', callback)
Expand All @@ -99,7 +100,7 @@ describe('loadTranslations', () => {
})

it('registers new translations', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('myapp', callback)
// Callback called
Expand All @@ -114,7 +115,7 @@ describe('loadTranslations', () => {
})

it('does reject on network error', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('networkissue', callback)
expect('').toBe('Unexpected pass')
Expand All @@ -125,7 +126,7 @@ describe('loadTranslations', () => {
})

it('does reject on server error', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('500', callback)
expect('').toBe('Unexpected pass')
Expand All @@ -136,7 +137,7 @@ describe('loadTranslations', () => {
})

it('does reject on unavailable bundle', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('404', callback)
expect('').toBe('Unexpected pass')
Expand All @@ -147,7 +148,7 @@ describe('loadTranslations', () => {
})

it('does reject on invalid bundle', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('invalid', callback)
expect('').toBe('Unexpected pass')
Expand All @@ -158,7 +159,7 @@ describe('loadTranslations', () => {
})

it('does reject on missing bundle', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('missing-bundle', callback)
expect('').toBe('Unexpected pass')
Expand All @@ -169,7 +170,7 @@ describe('loadTranslations', () => {
})

it('does reject on empty response', async () => {
const callback = jest.fn()
const callback = vi.fn()
try {
await loadTranslations('empty', callback)
expect('').toBe('Unexpected pass')
Expand Down
1 change: 1 addition & 0 deletions tests/locale.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, test } from 'vitest'
import {
getCanonicalLocale,
getLanguage,
Expand Down
1 change: 1 addition & 0 deletions tests/registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, test } from 'vitest'
import type { NextcloudWindowWithRegistry } from '../lib/registry'

import {
Expand Down
1 change: 1 addition & 0 deletions tests/translation.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import type { NextcloudWindowWithRegistry } from '../lib/registry'
import {
getPlural,
Expand Down
17 changes: 17 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import viteConfig from './vite.config'

export default async (env: Parameters<typeof viteConfig>[0]) => {
const config = typeof viteConfig === 'function' ? await viteConfig(env) : viteConfig
// node-externals conflicts with vitest
config.plugins = config.plugins!.filter((plugin) => plugin && (!('name' in plugin) || plugin?.name !== 'node-externals'))

return {
...config,
test: {
environment: 'jsdom',
coverage: {
reporter: ['text', 'lcov'],
},
},
}
}

0 comments on commit ca78970

Please sign in to comment.