Skip to content

Commit e04bb15

Browse files
committed
Add dom testing
1 parent 1c6371c commit e04bb15

15 files changed

+198
-12
lines changed

.eslintignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
docs/**
2+
.svelte-kit/**
3+
static/**
4+
build/**
5+
node_modules/**
6+
coverage/**
7+
__snapshots__/**
8+
snapshots.js

.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
// 'plugin:@typescript-eslint/recommended-requiring-type-checking',
88
'prettier'
99
],
10-
plugins: ['svelte3', 'tailwindcss', '@typescript-eslint', 'es'],
10+
plugins: ['svelte3', 'tailwindcss', '@typescript-eslint', 'es', 'vitest'],
1111
ignorePatterns: [
1212
'docs/*',
1313
'*.cjs',

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ docs/**
33
static/**
44
build/**
55
node_modules/**
6+
coverage/**
7+
__snapshots__/**
68
snapshots.js

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"pre-commit": "lint-staged",
1414
"postinstall": "husky install; cypress cache prune",
1515
"test:unit": "vitest",
16+
"test:ui": "vitest --ui",
1617
"test:coverage": "vitest run --coverage",
1718
"test:browser": "cypress run",
1819
"test": "test:unit && test:browser",
@@ -28,6 +29,7 @@
2829
"@types/pako": "^1.0.3",
2930
"@typescript-eslint/eslint-plugin": "^4.33.0",
3031
"@typescript-eslint/parser": "^4.33.0",
32+
"@vitest/ui": "^0.17.0",
3133
"autoprefixer": "^10.4.7",
3234
"c8": "^7.11.3",
3335
"chai": "^4.3.6",
@@ -39,27 +41,26 @@
3941
"eslint-config-prettier": "^8.5.0",
4042
"eslint-plugin-cypress": "^2.12.1",
4143
"eslint-plugin-es": "^4.1.0",
42-
"eslint-plugin-mocha": "^10.0.5",
4344
"eslint-plugin-postcss-modules": "^2.0.0",
4445
"eslint-plugin-svelte3": "^3.4.1",
4546
"eslint-plugin-tailwindcss": "^3.6.0",
47+
"eslint-plugin-vitest": "^0.0.8",
4648
"husky": "^8.0.1",
4749
"jsdom": "^20.0.0",
4850
"lint-staged": "13.0.3",
49-
"mocha": "^10.0.0",
50-
"msw": "^0.43.0",
5151
"node-html-parser": "^5.3.3",
5252
"postcss": "^8.4.14",
5353
"postcss-load-config": "^4.0.1",
5454
"prettier": "~2.7.1",
5555
"prettier-plugin-svelte": "^2.7.0",
5656
"svelte": "^3.48.0",
57-
"svelte-preprocess": "4.10.7",
57+
"svelte-preprocess": "^4.10.7",
5858
"tailwindcss": "^3.1.4",
5959
"tslib": "^2.4.0",
6060
"typescript": "^4.7.4",
6161
"vite": "^2.9.13",
62-
"vitest": "^0.17.0"
62+
"vitest": "^0.17.0",
63+
"vitest-svelte-kit": "^0.0.6"
6364
},
6465
"dependencies": {
6566
"@analytics/google-analytics": "^0.5.3",

src/global.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
/* eslint-disable @typescript-eslint/no-empty-interface */
2+
/* eslint-disable @typescript-eslint/ban-types */
3+
/* eslint-disable @typescript-eslint/no-unused-vars */
4+
15
/// <reference types="@sveltejs/kit" />
6+
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers';
7+
8+
declare global {
9+
namespace jest {
10+
interface Matchers<R = void>
11+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
12+
// @ts-ignore
13+
extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
14+
}
15+
}

src/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Handle } from '@sveltejs/kit/types/hooks';
1+
import type { Handle } from '@sveltejs/kit';
22

33
export const handle: Handle = async ({ event, resolve }) => {
44
const response = await resolve(event, {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`card.svelte > mounts 1`] = `"<div><div class=\\"card rounded overflow-hidden m-2 flex-grow flex flex-col shadow-2xl\\"><div class=\\"bg-primary p-2 pb-0 flex-none cursor-pointer\\"><div class=\\"flex justify-between\\"><div class=\\"flex cursor-default s-_wx1E_JHsCoF\\"><span class=\\"mr-2 font-semibold s-_wx1E_JHsCoF\\"><i class=\\"fas fa-chevron-right icon s-_wx1E_JHsCoF isOpen\\"></i> TabTest</span> <ul class=\\"tabs s-_wx1E_JHsCoF\\"><div class=\\"tab tab-lifted tab-active s-_wx1E_JHsCoF\\"><i class=\\"mr-1 undefined s-_wx1E_JHsCoF\\"></i> title1 </div><div class=\\"tab tab-lifted text-primary-content s-_wx1E_JHsCoF\\"><i class=\\"mr-1 undefined s-_wx1E_JHsCoF\\"></i> title2 </div></ul></div><!--<Tabs>--> <div class=\\"flex gap-x-4 items-center -mt-2\\"></div></div></div> <div class=\\"card-body p-0 flex-grow overflow-auto text-base-content\\"></div></div><!--<Card>--></div>"`;

src/lib/components/card/card.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { cleanup, render } from '@testing-library/svelte';
2+
import { describe, expect, it, afterEach } from 'vitest';
3+
import Card from './card.svelte';
4+
5+
describe('card.svelte', () => {
6+
// TODO: @testing-library/svelte claims to add this automatically but it doesn't work without explicit afterEach
7+
afterEach(() => cleanup());
8+
9+
it('mounts', () => {
10+
const { container } = render(Card, {
11+
title: 'TabTest',
12+
tabs: [
13+
{ id: 't1', title: 'title1' },
14+
{ id: 't2', title: 'title2' }
15+
]
16+
});
17+
expect(container).toBeTruthy();
18+
expect(container).toHaveTextContent('TabTest');
19+
expect(container).toHaveTextContent('title1');
20+
expect(container).toHaveTextContent('title2');
21+
expect(container).not.toHaveTextContent('title3');
22+
expect(container.innerHTML).toMatchSnapshot();
23+
});
24+
});

src/lib/types.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface State {
4343
}
4444

4545
export interface ValidatedState extends State {
46-
error: any;
46+
error: unknown;
4747
errorMarkers: MarkerData[];
4848
serialized: string;
4949
}

src/lib/util/fileLoaders/gist.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unsafe-return */
12
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
23
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
34
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -43,6 +44,7 @@ const getGistData = async (gistURL: string): Promise<GistData> => {
4344
}
4445
const currentItem = history[0];
4546
return {
47+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
4648
url: `${html_url}/${currentItem.version}`,
4749
code,
4850
config,

src/lib/util/fileLoaders/loader.ts

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export const loadDataFromUrl = async (): Promise<void> => {
2323
config = defaultState.mermaid;
2424
}
2525
if (!code) {
26+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27+
// @ts-ignore
28+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
2629
for (const [key, value] of searchParams.entries()) {
2730
if (key in loaders) {
2831
try {

src/tests/setup.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import matchers from '@testing-library/jest-dom/matchers';
2+
import { expect } from 'vitest';
3+
4+
expect.extend(matchers);

tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"static/**/*.js"
99
],
1010
"compilerOptions": {
11+
"lib": ["ESNext", "dom"],
12+
"resolveJsonModule": true,
1113
"allowSyntheticDefaultImports": true,
1214
"types": ["vitest/importMeta"]
1315
},

vite.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const config = {
1111
environment: 'jsdom',
1212
// in-source testing
1313
includeSource: ['src/**/*.{js,ts,svelte}'],
14-
// setupFiles: ['./src/tests/setup.ts'],
14+
setupFiles: ['./src/tests/setup.ts'],
1515
coverage: {
1616
exclude: ['src/mocks', '.svelte-kit', 'src/**/*.test.ts'],
1717
reporter: ['text', 'json', 'html', 'lcov']

0 commit comments

Comments
 (0)