Skip to content

Commit 54ea8fa

Browse files
mcousyanick
andauthored
test: add simple typing tests (#298)
* test: add simple typing tests --------- Co-authored-by: Yanick Champoux <[email protected]>
1 parent c8158a9 commit 54ea8fa

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

.eslintrc.cjs

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ module.exports = {
1616
'simple-import-sort/imports': 'error',
1717
},
1818
overrides: [
19+
{
20+
files: ['*.svelte'],
21+
parser: 'svelte-eslint-parser',
22+
parserOptions: {
23+
parser: '@typescript-eslint/parser',
24+
},
25+
},
1926
{
2027
files: ['*.ts'],
2128
parser: '@typescript-eslint/parser',

.github/workflows/release.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ jobs:
3636
npm install --no-package-lock
3737
npm install --no-save svelte@${{ matrix.svelte }}
3838
39-
- name: ▶️ Run validate script
39+
- name: ▶️ Run tests
4040
run: npm run test:${{ matrix.test-runner }}
4141

42+
- name: ▶️ Run type-checks
43+
if: ${{ matrix.node == '20' && matrix.svelte == '4' }}
44+
run: npm run types
45+
4246
- name: ⬆️ Upload coverage report
4347
uses: codecov/codecov-action@v3
4448

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
"format:delta": "npm-run-all format:prettier:delta format:eslint:delta",
5353
"format:prettier:delta": "prettier --write `./scripts/changed-files`",
5454
"format:eslint:delta": "eslint --fix `./scripts/changed-files`",
55+
"setup": "npm install && npm run validate",
5556
"test": "vitest run --coverage",
5657
"test:watch": "vitest",
5758
"test:update": "vitest run --update",
5859
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
5960
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
60-
"setup": "npm install && npm run validate",
61-
"validate": "npm-run-all test:vitest:*",
61+
"types": "svelte-check",
62+
"validate": "npm-run-all test:vitest:* types",
6263
"contributors:add": "all-contributors add",
6364
"contributors:generate": "all-contributors generate"
6465
},
@@ -86,12 +87,14 @@
8687
"eslint-plugin-simple-import-sort": "10.0.0",
8788
"eslint-plugin-svelte": "2.35.1",
8889
"eslint-plugin-vitest-globals": "1.4.0",
90+
"expect-type": "^0.17.3",
8991
"happy-dom": "^13.3.1",
9092
"jsdom": "^22.1.0",
9193
"npm-run-all": "^4.1.5",
9294
"prettier": "3.2.4",
9395
"prettier-plugin-svelte": "3.1.2",
9496
"svelte": "^3 || ^4",
97+
"svelte-check": "^3.6.3",
9598
"svelte-jester": "^3.0.0",
9699
"typescript": "^5.3.3",
97100
"vite": "^4.3.9",

src/__tests__/fixtures/Simple.svelte

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
export let name: string
3+
</script>
4+
5+
<h1>hello {name}</h1>

tsconfig.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"noEmit": true,
5+
"skipLibCheck": true,
6+
"strict": true,
7+
"types": ["svelte", "vite/client", "vitest", "vitest/globals"],
8+
},
9+
"include": ["src", "types"],
10+
}

types/types.test-d.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expectTypeOf } from 'expect-type'
2+
import type { ComponentProps, SvelteComponent } from 'svelte'
3+
import { describe, test } from 'vitest'
4+
5+
import Simple from '../src/__tests__/fixtures/Simple.svelte'
6+
import * as subject from './index.js'
7+
8+
describe('types', () => {
9+
test('render is a function that accepts a Svelte component', () => {
10+
subject.render(Simple, { name: 'Alice' })
11+
subject.render(Simple, { props: { name: 'Alice' } })
12+
})
13+
14+
test('invalid prop types are rejected', () => {
15+
// @ts-expect-error: name should be a string
16+
subject.render(Simple, { name: 42 })
17+
18+
// @ts-expect-error: name should be a string
19+
subject.render(Simple, { props: { name: 42 } })
20+
})
21+
22+
test('render result has container and component', () => {
23+
const result = subject.render(Simple, { name: 'Alice' })
24+
25+
expectTypeOf(result).toMatchTypeOf<{
26+
container: HTMLElement
27+
component: SvelteComponent<{ name: string }>
28+
debug: (el?: HTMLElement) => void
29+
rerender: (options: ComponentProps<Simple>) => void
30+
unmount: () => void
31+
}>()
32+
})
33+
34+
test('render result has default queries', () => {
35+
const result = subject.render(Simple, { name: 'Alice' })
36+
37+
expectTypeOf(result.getByRole).parameters.toMatchTypeOf<
38+
[role: subject.ByRoleMatcher, options?: subject.ByRoleOptions]
39+
>()
40+
})
41+
42+
test('render result can have custom queries', () => {
43+
const [getByVibes] = subject.buildQueries(
44+
(_container: HTMLElement, vibes: string) => {
45+
throw new Error(`unimplemented ${vibes}`)
46+
},
47+
() => '',
48+
() => ''
49+
)
50+
const result = subject.render(
51+
Simple,
52+
{ name: 'Alice' },
53+
{ queries: { getByVibes } }
54+
)
55+
56+
expectTypeOf(result.getByVibes).parameters.toMatchTypeOf<[vibes: string]>()
57+
})
58+
})

0 commit comments

Comments
 (0)