Skip to content

Commit

Permalink
feat: Add vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyphyn committed Jul 8, 2023
1 parent f04663b commit 08fce6f
Show file tree
Hide file tree
Showing 6 changed files with 748 additions and 135 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "vite dev",
"build": "vite build",
"test": "vitest",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
Expand All @@ -14,17 +15,18 @@
"@sveltejs/kit": "^1.5.0",
"@types/nprogress": "^0.2.0",
"autoprefixer": "^10.4.14",
"jsdom": "^22.1.0",
"nprogress": "^0.2.0",
"postcss": "^8.4.24",
"rollup-plugin-visualizer": "^5.9.2",
"svelte": "^3.54.0",
"svelte-check": "^3.0.1",
"svelte-hero-icons": "^5.0.0",
"svelte-markdown": "^0.2.3",
"tailwindcss": "^3.3.2",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.3.0"
"vite": "^4.3.0",
"vitest": "^0.33.0"
},
"dependencies": {
"lemmy-js-client": "^0.18.0-rc.2"
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/util/RelativeDate.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script lang="ts">
export let date: Date
export let relativeTo: Date | undefined
function formatRelativeDate(date: Date) {
const now = Date.now()
const now = relativeTo?.getTime() ?? Date.now()
const diffInMillis = now - date.getTime()
Expand Down
32 changes: 32 additions & 0 deletions tests/relativedate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import RelativeDate from '../src/lib/components/util/RelativeDate.svelte'
import { describe, expect, it } from 'vitest'

describe('RelativeDate.svelte', () => {
let host = document.createElement('div')
host.setAttribute('id', 'host')
document.body.appendChild(host)

it('mounts', () => {
let host = document.createElement('div')
const instance = new RelativeDate({
target: host,
props: {
date: new Date(),
relativeTo: new Date(),
},
})
expect(instance).toBeTruthy()
})
it('shows the correct date', () => {
const date = new Date('2023-01-01 00:00:00')
const relativeTo = new Date('2023-01-01 01:00:00')

const instance = new RelativeDate({
target: host,
props: { date: date, relativeTo: relativeTo },
})

expect(instance).toBeTruthy()
expect(host.innerHTML).toContain('1 hour ago')
})
})
3 changes: 1 addition & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { sveltekit } from '@sveltejs/kit/vite'
import { visualizer } from 'rollup-plugin-visualizer'
import { defineConfig, type PluginOption } from 'vite'

export default defineConfig({
plugins: [sveltekit(), visualizer() as PluginOption],
plugins: [sveltekit()],
})
11 changes: 11 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { defineConfig } from 'vitest/dist/config'

export default defineConfig({
// @ts-ignore
plugins: [svelte()],
test: {
globals: true,
environment: 'jsdom',
},
})
Loading

0 comments on commit 08fce6f

Please sign in to comment.