Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

test: add simple typing tests #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanick merged 5 commits into testing-library:main from mcous:test-types
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.cjs
View file Open in desktop
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
6 changes: 5 additions & 1 deletion .github/workflows/release.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ jobs:
npm install --no-package-lock
npm install --no-save svelte@${{ matrix.svelte }}

- name: ▶️ Run validate script
- name: ▶️ Run tests
run: npm run test:${{ matrix.test-runner }}

- name: ▶️ Run type-checks
if: ${{ matrix.node == '20' && matrix.svelte == '4' }}
run: npm run types

- name: ⬆️ Upload coverage report
uses: codecov/codecov-action@v3

Expand Down
7 changes: 5 additions & 2 deletions package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
"format:delta": "npm-run-all format:prettier:delta format:eslint:delta",
"format:prettier:delta": "prettier --write `./scripts/changed-files`",
"format:eslint:delta": "eslint --fix `./scripts/changed-files`",
"setup": "npm install && npm run validate",
"test": "vitest run --coverage",
"test:watch": "vitest",
"test:update": "vitest run --update",
"test:vitest:jsdom": "vitest run --coverage --environment jsdom",
"test:vitest:happy-dom": "vitest run --coverage --environment happy-dom",
"setup": "npm install && npm run validate",
"validate": "npm-run-all test:vitest:*",
"types": "svelte-check",
"validate": "npm-run-all test:vitest:* types",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate"
},
Expand Down Expand Up @@ -86,12 +87,14 @@
"eslint-plugin-simple-import-sort": "10.0.0",
"eslint-plugin-svelte": "2.35.1",
"eslint-plugin-vitest-globals": "1.4.0",
"expect-type": "^0.17.3",
"happy-dom": "^13.3.1",
"jsdom": "^22.1.0",
"npm-run-all": "^4.1.5",
"prettier": "3.2.4",
"prettier-plugin-svelte": "3.1.2",
"svelte": "^3 || ^4",
"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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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]>()
})
})

AltStyle によって変換されたページ (->オリジナル) /