Skip to content

Commit

Permalink
test: add simple typing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed Jan 29, 2024
1 parent ee1b1f7 commit 4cc2fce
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ module.exports = {
'simple-import-sort/imports': 'error',
},
overrides: [
{
files: ['*.svelte'],
parser: 'svelte-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
},
},
{
files: ['*.ts'],
parser: '@typescript-eslint/parser',
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
"test": "vitest run --coverage",
"test:watch": "vitest",
"test:update": "vitest run --update",
"types": "svelte-check",
"setup": "npm install && npm run validate",
"validate": "npm-run-all lint test",
"validate": "npm-run-all lint test types",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate"
},
Expand Down Expand Up @@ -79,13 +80,15 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-svelte": "^2.32.0",
"eslint-plugin-vitest-globals": "^1.3.1",
"expect-type": "^0.17.3",
"husky": "^8.0.3",
"jsdom": "^22.1.0",
"lint-staged": "^13.2.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.0",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.0.1",
"svelte-check": "^3.6.3",
"svelte-jester": "^3.0.0",
"typescript": "^5.3.3",
"vite": "^4.3.9",
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/fixtures/Simple.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
export let name: string
</script>

<h1>hello {name}</h1>
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "node16",
"noEmit": true,
"skipLibCheck": true,
"strict": true,
"types": ["svelte", "vite/client", "vitest", "vitest/globals"],
},
"include": ["src", "types"],
}
58 changes: 58 additions & 0 deletions types/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expectTypeOf } from 'expect-type'
import type { ComponentProps, SvelteComponent } from 'svelte'
import { describe, test } from 'vitest'

import Simple from '../src/__tests__/fixtures/Simple.svelte'
import * as subject from './index.js'

describe('types', () => {
test('render is a function that accepts a Svelte component', () => {
subject.render(Simple, { name: 'Alice' })
subject.render(Simple, { props: { name: 'Alice' } })
})

test('invalid prop types are rejected', () => {
// @ts-expect-error: name should be a string
subject.render(Simple, { name: 42 })

// @ts-expect-error: name should be a string
subject.render(Simple, { props: { name: 42 } })
})

test('render result has container and component', () => {
const result = subject.render(Simple, { name: 'Alice' })

expectTypeOf(result).toMatchTypeOf<{
container: HTMLElement
component: SvelteComponent<{ name: string }>
debug: (el?: HTMLElement) => void
rerender: (options: ComponentProps<Simple>) => void
unmount: () => void
}>()
})

test('render result has default queries', () => {
const result = subject.render(Simple, { name: 'Alice' })

expectTypeOf(result.getByRole).parameters.toMatchTypeOf<
[role: subject.ByRoleMatcher, options?: subject.ByRoleOptions]
>()
})

test('render result can have custom queries', () => {
const [getByVibes] = subject.buildQueries(
(_container: HTMLElement, vibes: string) => {
throw new Error(`unimplemented ${vibes}`)
},
() => '',
() => ''
)
const result = subject.render(
Simple,
{ name: 'Alice' },
{ queries: { getByVibes } }
)

expectTypeOf(result.getByVibes).parameters.toMatchTypeOf<[vibes: string]>()
})
})

0 comments on commit 4cc2fce

Please sign in to comment.