Skip to content

Instantly share code, notes, and snippets.

@sombriks
Created August 22, 2024 19:25
Show Gist options
  • Save sombriks/fe7ec7e7af75bc2ab4246fa142709e88 to your computer and use it in GitHub Desktop.
Save sombriks/fe7ec7e7af75bc2ab4246fa142709e88 to your computer and use it in GitHub Desktop.
working version of testcase passing on github CI or: the importance of await vi.waitFor
import {describe, expect, it, afterAll, afterEach, beforeAll, vi} from 'vitest'
import {render, screen, fireEvent} from '@testing-library/vue'
import {http, HttpResponse} from 'msw'
import {setupServer} from 'msw/node'
import {router} from "./services/route-config.ts";
import App from './App.vue'
describe('Basic tests', () => {
// https://vitest.dev/guide/mocking.html#configuration
const server = setupServer(
// see .env.test for more details
http.get('http://mock/companies/count', () => HttpResponse.json({total: 1})),
http.get('http://mock/user-portfolios/count', () => HttpResponse.json({total: 1})),
http.get('http://mock/users/count-all', () => HttpResponse.json({total: 1})),
http.get('http://mock/companies', () => HttpResponse.json([{id:10, description:"My test Company"}])),
)
// Start server before all tests
beforeAll(async () => {
server.listen({onUnhandledRequest: 'error'})
// give the mock server a time, without the wait it ends up in network error
await vi.waitFor(()=> {
render(App, {
global: {
plugins: [router]
}
})
})
})
// Close server after all tests
afterAll(() => server.close())
// Reset handlers after each test `important for test isolation`
afterEach(() => server.resetHandlers())
it('should be in test mode', () => {
expect('test', import.meta.env.NODE_ENV)
})
it('should get the logo', async () => {
const result = <HTMLImageElement>screen.getByAltText('markerr-logo')
expect(result).toBeTruthy()
expect(result.src).toContain('markerr-icon.png')
})
it('should navigate to companies page', async () => {
// when
const result = <HTMLAnchorElement>screen.getByText('Manage Companies')
// then
expect(result).toBeTruthy()
expect(result.href).toContain('/companies-page')
await fireEvent.click(result)
// git vue-router a time to re-render the document
await vi.waitFor(async () => {
const check = screen.getByTestId('page-title')
expect(check).toBeTruthy()
expect(check.innerText).eq('Companies')
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment