Skip to content

Commit

Permalink
Merge pull request #662 from nextcloud-libraries/fix/migrate-vitest
Browse files Browse the repository at this point in the history
feat: Migrate to vitest for testing
  • Loading branch information
susnux authored Apr 24, 2024
2 parents c46af75 + 189cdff commit 4aa999d
Show file tree
Hide file tree
Showing 9 changed files with 1,284 additions and 978 deletions.
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

7 changes: 0 additions & 7 deletions jest.config.mjs

This file was deleted.

1,724 changes: 1,009 additions & 715 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
"dev:watch": "vite --mode development build --watch",
"lint": "eslint lib",
"lint:fix": "eslint lib --fix",
"test": "jest",
"test:coverage": "jest --coverage"
"test": "vitest run",
"test:coverage": "vitest run --coverage"
},
"keywords": [
"nextcloud"
],
"homepage": "https://github.com/nextcloud/nextcloud-logger#readme",
"homepage": "https://github.com/nextcloud-libraries/nextcloud-logger#readme",
"author": "Christoph Wurst",
"license": "GPL-3.0-or-later",
"repository": {
"type": "git",
"url": "https://github.com/nextcloud/nextcloud-logger"
"url": "https://github.com/nextcloud-libraries/nextcloud-logger"
},
"dependencies": {
"@nextcloud/auth": "^2.3.0"
Expand All @@ -42,15 +42,14 @@
"@nextcloud/eslint-config": "^8.3.0",
"@nextcloud/typings": "^1.8.0",
"@nextcloud/vite-config": "^1.2.3",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.7",
"@vitest/coverage-v8": "^1.5.1",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-jest": "^29.1.2",
"happy-dom": "^14.7.1",
"typedoc": "^0.25.13",
"typescript": "^5.4.5",
"vite": "^5.2.10"
"vite": "^5.2.10",
"vitest": "^1.5.1"
},
"browserslist": [
"extends @nextcloud/browserslist-config"
Expand Down
33 changes: 17 additions & 16 deletions tests/ConsoleLogger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, describe, expect, it, test, vi } from 'vitest'
import { ConsoleLogger, buildConsoleLogger } from '../lib/ConsoleLogger'

// Dummy Error
Expand All @@ -11,28 +12,28 @@ class MyError extends Error {
}

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

test('building the console logger', () => {
const logger = buildConsoleLogger({ app: 'myapp' })
expect(logger).toBeInstanceOf(ConsoleLogger)

// ensure initial context is preserved
const consoleArgs = [] as any[]
const warn = jest.spyOn(console, 'warn').mockImplementation((...args) => consoleArgs.push(...args))
const consoleArgs = [] as unknown[]
const warn = vi.spyOn(console, 'warn').mockImplementation((...args) => consoleArgs.push(...args))
logger.warn('some message', { foo: 'bar' })
expect(warn).toHaveBeenCalledTimes(1)
expect(consoleArgs).toHaveLength(2)
expect(consoleArgs[1]).toHaveProperty('app', 'myapp')
})

describe('ConsoleLogger', () => {
afterEach(() => jest.resetAllMocks())
afterEach(() => { vi.resetAllMocks() })

it('logs debug messages', () => {
const logger = new ConsoleLogger()
const debug = jest.spyOn(window.console, 'debug').mockImplementation(() => {})
const debug = vi.spyOn(window.console, 'debug').mockImplementation(() => {})

logger.debug('Should be logged')
expect(debug).toHaveBeenCalledTimes(1)
Expand All @@ -41,7 +42,7 @@ describe('ConsoleLogger', () => {

it('logs info messages', () => {
const logger = new ConsoleLogger()
const info = jest.spyOn(window.console, 'info').mockImplementation(() => {})
const info = vi.spyOn(window.console, 'info').mockImplementation(() => {})

logger.info('Should be logged')
expect(info).toHaveBeenCalledTimes(1)
Expand All @@ -50,7 +51,7 @@ describe('ConsoleLogger', () => {

it('logs warn messages', () => {
const logger = new ConsoleLogger()
const warn = jest.spyOn(window.console, 'warn').mockImplementation(() => {})
const warn = vi.spyOn(window.console, 'warn').mockImplementation(() => {})

logger.warn('Should be logged')
expect(warn).toHaveBeenCalledTimes(1)
Expand All @@ -59,7 +60,7 @@ describe('ConsoleLogger', () => {

it('logs error messages', () => {
const logger = new ConsoleLogger()
const error = jest.spyOn(window.console, 'error').mockImplementation(() => {})
const error = vi.spyOn(window.console, 'error').mockImplementation(() => {})

logger.error('Should be logged')
expect(error).toHaveBeenCalledTimes(1)
Expand All @@ -68,7 +69,7 @@ describe('ConsoleLogger', () => {

it('logs fatal messages', () => {
const logger = new ConsoleLogger()
const error = jest.spyOn(window.console, 'error').mockImplementation(() => {})
const error = vi.spyOn(window.console, 'error').mockImplementation(() => {})

logger.fatal('Should be logged')
expect(error).toHaveBeenCalledTimes(1)
Expand All @@ -77,7 +78,7 @@ describe('ConsoleLogger', () => {

it('allows global context', () => {
const logger = new ConsoleLogger({ foo: 'bar' })
const debug = jest.spyOn(window.console, 'debug').mockImplementation(() => {})
const debug = vi.spyOn(window.console, 'debug').mockImplementation(() => {})

logger.debug('Should be logged')
expect(debug).toHaveBeenCalledTimes(1)
Expand All @@ -87,7 +88,7 @@ describe('ConsoleLogger', () => {

it('allows extending global context', () => {
const logger = new ConsoleLogger({ one: 1, two: 2 })
const debug = jest.spyOn(window.console, 'debug').mockImplementation(() => {})
const debug = vi.spyOn(window.console, 'debug').mockImplementation(() => {})

logger.debug('Should be logged', { two: 3 })
expect(debug).toHaveBeenCalledTimes(1)
Expand All @@ -97,7 +98,7 @@ describe('ConsoleLogger', () => {

it('allows extending empty global context', () => {
const logger = new ConsoleLogger()
const debug = jest.spyOn(window.console, 'debug').mockImplementation(() => {})
const debug = vi.spyOn(window.console, 'debug').mockImplementation(() => {})

logger.debug('Should be logged', { one: 1 })
expect(debug).toHaveBeenCalledTimes(1)
Expand All @@ -108,7 +109,7 @@ describe('ConsoleLogger', () => {
it('only logs configured levels', () => {
const logger = new ConsoleLogger({ level: 2 })

const debug = jest.spyOn(window.console, 'debug')
const debug = vi.spyOn(window.console, 'debug')

logger.debug('Should not be logged')
expect(debug).toHaveBeenCalledTimes(0)
Expand All @@ -119,7 +120,7 @@ describe('ConsoleLogger', () => {
const logger = new ConsoleLogger({})

const console = [] as [string, unknown][]
const warn = jest.spyOn(window.console, 'warn').mockImplementation((msg, ctx) => console.push([msg, ctx]))
const warn = vi.spyOn(window.console, 'warn').mockImplementation((msg, ctx) => console.push([msg, ctx]))

logger.warn(error)
expect(warn).toHaveBeenCalledTimes(1)
Expand All @@ -134,7 +135,7 @@ describe('ConsoleLogger', () => {
const logger = new ConsoleLogger({})

const console = [] as [string, unknown][]
const debug = jest.spyOn(window.console, 'debug').mockImplementation((msg, ctx) => console.push([msg, ctx]))
const debug = vi.spyOn(window.console, 'debug').mockImplementation((msg, ctx) => console.push([msg, ctx]))

logger.debug(error)
expect(debug).toHaveBeenCalledTimes(1)
Expand All @@ -148,7 +149,7 @@ describe('ConsoleLogger', () => {
const logger = new ConsoleLogger({ error: 'none' })

const console = [] as [string, unknown][]
const warn = jest.spyOn(window.console, 'warn').mockImplementation((msg, ctx) => console.push([msg, ctx]))
const warn = vi.spyOn(window.console, 'warn').mockImplementation((msg, ctx) => console.push([msg, ctx]))

logger.warn(error)
expect(warn).toHaveBeenCalledTimes(1)
Expand Down
Loading

0 comments on commit 4aa999d

Please sign in to comment.