Skip to content

Commit

Permalink
test: remove unnecessary tests and skips (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed Mar 25, 2024
1 parent 8d98aa5 commit 16f6b40
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ module.exports = {
parserOptions: {
parser: '@typescript-eslint/parser',
},
rules: {
'no-undef-init': 'off',
},
},
{
files: ['*.ts'],
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
scripts/*
.eslintignore
.prettierignore
.all-contributorsrc
2 changes: 1 addition & 1 deletion .prettierrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ trailingComma: es5
plugins:
- prettier-plugin-svelte
overrides:
- files: "*.svelte"
- files: '*.svelte'
options:
parser: svelte
39 changes: 8 additions & 31 deletions src/__tests__/act.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import { beforeEach, describe, expect, test } from 'vitest'
import { setTimeout } from 'node:timers/promises'

import { act, render } from '@testing-library/svelte'
import { describe, expect, test } from 'vitest'

import { act, fireEvent, render as stlRender } from '@testing-library/svelte'
import Comp from './fixtures/Comp.svelte'

describe('act', () => {
let props

const render = () => {
return stlRender(Comp, {
props
})
}

beforeEach(() => {
props = {
name: 'World'
}
})

test('state updates are flushed', async () => {
const { getByText } = render()
const { getByText } = render(Comp)
const button = getByText('Button')

expect(button).toHaveTextContent('Button')
Expand All @@ -31,24 +19,13 @@ describe('act', () => {
expect(button).toHaveTextContent('Button Clicked')
})

test('findByTestId returns the element', async () => {
const { findByTestId } = render()

expect(await findByTestId('test')).toHaveTextContent(`Hello ${props.name}!`)
})

test('accepts async functions', async () => {
const sleep = (ms) =>
new Promise((resolve) => {
setTimeout(() => resolve(), ms)
})

const { getByText } = render()
const { getByText } = render(Comp)
const button = getByText('Button')

await act(async () => {
await sleep(100)
await fireEvent.click(button)
await setTimeout(100)
button.click()
})

expect(button).toHaveTextContent('Button Clicked')
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/auto-cleanup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render } from '@testing-library/svelte'
import { describe, expect, test } from 'vitest'

import { render } from '@testing-library/svelte'
import Comp from './fixtures/Comp.svelte'

describe('auto-cleanup', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/__tests__/cleanup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { cleanup, render } from '@testing-library/svelte'
import { describe, expect, test, vi } from 'vitest'
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'

import { act, cleanup, render } from '@testing-library/svelte'
import Mounter from './fixtures/Mounter.svelte'

const onExecuted = vi.fn()
Expand All @@ -16,8 +15,8 @@ describe('cleanup', () => {
expect(document.body).toBeEmptyDOMElement()
})

test.runIf(SVELTE_VERSION < '5')('cleanup unmounts component', async () => {
await act(renderSubject)
test('cleanup unmounts component', () => {
renderSubject()
cleanup()

expect(onDestroyed).toHaveBeenCalledOnce()
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/context.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render } from '@testing-library/svelte'
import { expect, test } from 'vitest'

import { render } from '@testing-library/svelte'
import Comp from './fixtures/Context.svelte'
import { IS_HAPPYDOM, IS_SVELTE_5 } from './utils.js'

Expand Down
14 changes: 4 additions & 10 deletions src/__tests__/debug.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { prettyDOM } from '@testing-library/dom'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'

import { render } from '@testing-library/svelte'
import { describe, expect, test, vi } from 'vitest'

import Comp from './fixtures/Comp.svelte'

describe('debug', () => {
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
console.log.mockRestore()
})

test('pretty prints the base element', () => {
vi.stubGlobal('console', { log: vi.fn(), warn: vi.fn(), error: vi.fn() })

const { baseElement, debug } = render(Comp, { props: { name: 'world' } })

debug()
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/events.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, render } from '@testing-library/svelte'
import { describe, expect, test } from 'vitest'

import { fireEvent, render } from '@testing-library/svelte'
import Comp from './fixtures/Comp.svelte'

describe('events', () => {
Expand All @@ -21,7 +21,7 @@ describe('events', () => {
button,
new MouseEvent('click', {
bubbles: true,
cancelable: true
cancelable: true,
})
)

Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/fixtures/Comp.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<svelte:options accessors />

<script>
export let name
export let name = 'World'
let buttonText = 'Button'
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/fixtures/Context.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { getContext } from 'svelte';
import { getContext } from 'svelte'
const ctx = getContext('foo');
const ctx = getContext('foo')
</script>

<div>{ctx.message}</div>
2 changes: 1 addition & 1 deletion src/__tests__/mount.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, render, screen } from '@testing-library/svelte'
import { describe, expect, test, vi } from 'vitest'

import { act, render, screen } from '@testing-library/svelte'
import Mounter from './fixtures/Mounter.svelte'
import { IS_HAPPYDOM, IS_SVELTE_5 } from './utils.js'

Expand Down
39 changes: 18 additions & 21 deletions src/__tests__/render.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from '@testing-library/svelte'
import { VERSION as SVELTE_VERSION } from 'svelte/compiler'
import { describe, expect, test } from 'vitest'

import Comp from './fixtures/Comp.svelte'
import { IS_SVELTE_5 } from './utils.js'

describe('render', () => {
const props = { name: 'World' }
Expand Down Expand Up @@ -65,24 +65,21 @@ describe('render', () => {
expect(baseElement.firstChild).toBe(container)
})

test.runIf(SVELTE_VERSION < '5')(
'should accept anchor option in Svelte v4',
() => {
const baseElement = document.body
const target = document.createElement('section')
const anchor = document.createElement('div')
baseElement.appendChild(target)
target.appendChild(anchor)

const { getByTestId } = render(
Comp,
{ props, target, anchor },
{ baseElement }
)
const firstElement = getByTestId('test')

expect(target.firstChild).toBe(firstElement)
expect(target.lastChild).toBe(anchor)
}
)
test.skipIf(IS_SVELTE_5)('should accept anchor option in Svelte v4', () => {
const baseElement = document.body
const target = document.createElement('section')
const anchor = document.createElement('div')
baseElement.appendChild(target)
target.appendChild(anchor)

const { getByTestId } = render(
Comp,
{ props, target, anchor },
{ baseElement }
)
const firstElement = getByTestId('test')

expect(target.firstChild).toBe(firstElement)
expect(target.lastChild).toBe(anchor)
})
})
2 changes: 1 addition & 1 deletion src/__tests__/rerender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('rerender', () => {
})

test('warns if incorrect arguments shape used', async () => {
vi.stubGlobal('console', { warn: vi.fn() })
vi.stubGlobal('console', { log: vi.fn(), warn: vi.fn(), error: vi.fn() })

const { rerender } = render(Comp, { name: 'World' })
const element = screen.getByText('Hello World!')
Expand Down
7 changes: 3 additions & 4 deletions src/__tests__/transition.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { render, screen, waitFor } from '@testing-library/svelte'
import { userEvent } from '@testing-library/user-event'
import { beforeEach, describe, expect, test, vi } from 'vitest'

import { IS_JSDOM, IS_SVELTE_5 } from './utils.js'

import { render, screen, waitFor } from '@testing-library/svelte'
import Transitioner from './fixtures/Transitioner.svelte'
import { IS_JSDOM, IS_SVELTE_5 } from './utils.js'

describe.runIf(!IS_SVELTE_5)('transitions', () => {
describe.skipIf(IS_SVELTE_5)('transitions', () => {
beforeEach(() => {
if (!IS_JSDOM) return

Expand Down
3 changes: 1 addition & 2 deletions src/vitest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { afterEach } from 'vitest'

import { act, cleanup } from '@testing-library/svelte'
import { afterEach } from 'vitest'

afterEach(async () => {
await act()
Expand Down
6 changes: 3 additions & 3 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'

export default {
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
}

0 comments on commit 16f6b40

Please sign in to comment.