Skip to content
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

chore: Update layouts/BaseLayout tests to Vitest #3208

Merged
merged 47 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b766325
add in vitest
nicholas-codecov Sep 16, 2024
8a1c7b1
rename old_ui tests from spec to test for vitest
nicholas-codecov Sep 16, 2024
627b145
add in data-testid to icon
nicholas-codecov Sep 16, 2024
dc8fe8d
temp
nicholas-codecov Sep 16, 2024
3f154bb
ignore test test files in craco
nicholas-codecov Sep 16, 2024
71f0a33
add in coverage stuff for vitest
nicholas-codecov Sep 16, 2024
b08164b
setup CI to run vitest tests
nicholas-codecov Sep 16, 2024
7b06a1e
whoops didn't mean to commit these
nicholas-codecov Sep 16, 2024
b572a9d
add names to test runner indexes
nicholas-codecov Sep 16, 2024
ce0b6e5
set min threads
nicholas-codecov Sep 16, 2024
bb488b9
add in reporter all tests
nicholas-codecov Sep 16, 2024
950d6ac
conditionally enable junit test reporter
nicholas-codecov Sep 16, 2024
64c2939
only include src files in coverage reporting
nicholas-codecov Sep 16, 2024
9917c18
exclude some files from coverage reporting
nicholas-codecov Sep 16, 2024
cab1973
there are some issues with coverage excludes making the files not acc…
nicholas-codecov Sep 17, 2024
f5b6466
forgot that I moved the setup file to ts
nicholas-codecov Sep 17, 2024
d2bd52b
add --changed to test watch command
nicholas-codecov Sep 17, 2024
d120380
add name to prod uploads for craco and vitest
nicholas-codecov Sep 17, 2024
fc5eb5b
tidying up some stuff in the ci
nicholas-codecov Sep 17, 2024
168ffa4
disable vitest to see what happens
nicholas-codecov Sep 17, 2024
619bbac
increase n builds
nicholas-codecov Sep 17, 2024
84d1276
remove generating icons for webpack
nicholas-codecov Sep 18, 2024
3d6ef25
update ci command
nicholas-codecov Sep 18, 2024
71f0955
rename to .jsx because it contains jsx
nicholas-codecov Sep 18, 2024
53616b2
swap from v8 to istanbul for coverage reporting
nicholas-codecov Sep 18, 2024
d188c3e
only generate icons for vite, and craco build
nicholas-codecov Sep 18, 2024
2a1068d
small tweaks to the codecov yml config
nicholas-codecov Sep 18, 2024
976ade0
generate webpack icons before running tests
nicholas-codecov Sep 18, 2024
0a5ed38
styling tweaks to vitest
nicholas-codecov Sep 18, 2024
e53de7e
small tweaks to vitest config
nicholas-codecov Sep 18, 2024
19b463e
add in msw 2
nicholas-codecov Sep 18, 2024
634a18b
add in examples so our test split function doesn't complain
nicholas-codecov Sep 18, 2024
6e656c5
add in all test file extensions
nicholas-codecov Sep 18, 2024
52fd40c
remove left over log
nicholas-codecov Sep 18, 2024
31b1335
tweak excludes
nicholas-codecov Sep 18, 2024
850a7a8
disable this eslint rule as it seems we need to do the manual cleanup
nicholas-codecov Sep 19, 2024
d8c6af2
make sure TS knows about the vi types globally
nicholas-codecov Sep 19, 2024
79f70a0
sort out vitest with setup stuff as well as loading correct envs
nicholas-codecov Sep 19, 2024
9f73de4
resolve issue with vitest types interferring with craco stuff
nicholas-codecov Sep 19, 2024
1c2aebc
tweak reporters
nicholas-codecov Sep 19, 2024
5d6b24e
remove test files from coverage reporting
nicholas-codecov Sep 19, 2024
bc5a324
update BaseLayout test
nicholas-codecov Sep 19, 2024
7317a5e
update useUserAccessGate tests
nicholas-codecov Sep 19, 2024
cff7486
update InstallationHelpBanner tests
nicholas-codecov Sep 19, 2024
532ffce
update index files from js to ts
nicholas-codecov Sep 19, 2024
e25f145
Merge branch 'main' into chore-vitest-update-base-layout-tests
nicholas-codecov Sep 19, 2024
61c44ef
remove status 200 as it's the default
nicholas-codecov Sep 19, 2024
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
35 changes: 16 additions & 19 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,55 +177,52 @@ describe('App', () => {
server.use(
http.get('/internal/user', (info) => {
if (hasSession) {
return HttpResponse.json(internalUser, { status: 200 })
return HttpResponse.json(internalUser)
} else {
return HttpResponse.json({}, { status: 200 })
return HttpResponse.json({})
}
}),
http.get('/internal/users/current', (info) => {
return HttpResponse.json({}, { status: 200 })
return HttpResponse.json({})
}),
graphql.query('DetailOwner', (info) =>
HttpResponse.json({ data: { owner: 'codecov' } }, { status: 200 })
HttpResponse.json({ data: { owner: 'codecov' } })
),
graphql.query('CurrentUser', (info) => {
if (hasLoggedInUser) {
return HttpResponse.json({ data: user }, { status: 200 })
return HttpResponse.json({ data: user })
}
HttpResponse.json({ data: {} }, { status: 200 })
HttpResponse.json({ data: {} })
}),
graphql.query('GetPlanData', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('OwnerTier', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('Seats', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('HasAdmins', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('owner', (info) => {
return HttpResponse.json(
{ data: { owner: { isAdmin: true } } },
{ status: 200 }
)
return HttpResponse.json({ data: { owner: { isAdmin: true } } })
}),
graphql.query('MyContexts', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('GetOktaConfig', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('OwnerPageData', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.mutation('updateDefaultOrganization', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
return HttpResponse.json({ data: {} })
}),
graphql.query('GetRepoOverview', (info) => {
return HttpResponse.json({ data: mockRepoOverview }, { status: 200 })
return HttpResponse.json({ data: mockRepoOverview })
})
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor } from '@testing-library/react'
import { graphql, rest } from 'msw'
import { setupServer } from 'msw/node'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import { graphql, http, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route, useLocation } from 'react-router-dom'
import { type Mock } from 'vitest'

import config from 'config'

Expand All @@ -12,16 +13,24 @@ import { useInternalUser, useUser } from 'services/user'

import BaseLayout from './BaseLayout'

jest.mock('services/image')
const mockedUseImage = useImage as jest.Mock
jest.mock('services/impersonate')
const mockedUseImpersonate = useImpersonate as jest.Mock
jest.mock('shared/GlobalTopBanners', () => () => 'GlobalTopBanners')
jest.mock('./InstallationHelpBanner', () => () => 'InstallationHelpBanner')
jest.mock('pages/TermsOfService', () => () => 'TermsOfService')
jest.mock('pages/DefaultOrgSelector', () => () => 'DefaultOrgSelector')
jest.mock('layouts/Header', () => () => 'Header')
jest.mock('layouts/Footer', () => () => 'Footer')
vi.mock('services/image')
const mockedUseImage = useImage as Mock

vi.mock('services/impersonate')
const mockedUseImpersonate = useImpersonate as Mock

vi.mock('shared/GlobalTopBanners', () => ({
default: () => 'GlobalTopBanners',
}))
vi.mock('./InstallationHelpBanner', () => ({
default: () => 'InstallationHelpBanner',
}))
vi.mock('pages/TermsOfService', () => ({ default: () => 'TermsOfService' }))
vi.mock('pages/DefaultOrgSelector', () => ({
default: () => 'DefaultOrgSelector',
}))
vi.mock('layouts/Header', () => ({ default: () => 'Header' }))
vi.mock('layouts/Footer', () => ({ default: () => 'Footer' }))

const mockOwner = {
owner: {
Expand Down Expand Up @@ -183,7 +192,8 @@ beforeAll(() => {
afterEach(() => {
queryClient.clear()
server.resetHandlers()
jest.resetAllMocks()
vi.clearAllMocks()
cleanup()
})

afterAll(() => {
Expand Down Expand Up @@ -212,32 +222,31 @@ describe('BaseLayout', () => {
mockedUseImpersonate.mockReturnValue({ isImpersonating })

server.use(
rest.get('/internal/user', (req, res, ctx) => {
return res(ctx.status(200), ctx.json(internalUser))
http.get('/internal/user', (info) => {
return HttpResponse.json(internalUser)
}),
graphql.query('CurrentUser', (info) => {
return HttpResponse.json({ data: currentUser })
}),
graphql.query('DetailOwner', (info) => {
return HttpResponse.json({ data: mockOwner })
}),
http.get('/internal/:provider/:owner/account-details', (info) => {
return HttpResponse.json({})
}),
graphql.query('CurrentUser', (_, res, ctx) =>
res(ctx.status(200), ctx.data(currentUser))
),
graphql.query('DetailOwner', (_, res, ctx) =>
res(ctx.status(200), ctx.data(mockOwner))
),
rest.get('/internal/:provider/:owner/account-details', (_, res, ctx) =>
res(ctx.status(200), ctx.json({}))
),
// Self hosted only
graphql.query('HasAdmins', (_, res, ctx) =>
res(ctx.status(200), ctx.data({}))
),
graphql.query('Seats', (_, res, ctx) =>
res(ctx.status(200), ctx.data({}))
),
graphql.query('TermsOfService', (_, res, ctx) =>
res(ctx.status(200), ctx.data({}))
),
graphql.query('UseMyOrganizations', (_, res, ctx) =>
res(
ctx.status(200),
ctx.data({
graphql.query('HasAdmins', (info) => {
return HttpResponse.json({ data: {} })
}),
graphql.query('Seats', (info) => {
return HttpResponse.json({ data: {} })
}),
graphql.query('TermsOfService', (info) => {
return HttpResponse.json({ data: {} })
}),
graphql.query('UseMyOrganizations', (info) => {
return HttpResponse.json({
data: {
myOrganizationsData: {
me: {
myOrganizations: {
Expand All @@ -246,15 +255,15 @@ describe('BaseLayout', () => {
},
},
},
})
)
),
graphql.mutation('updateDefaultOrganization', (req, res, ctx) =>
res(ctx.status(200), ctx.data({}))
),
rest.get('/internal/users/current', (_, res, ctx) =>
res(ctx.status(200), ctx.json({}))
)
},
})
}),
graphql.mutation('updateDefaultOrganization', (info) => {
return HttpResponse.json({ data: {} })
}),
http.get('/internal/users/current', (info) => {
return HttpResponse.json({})
})
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor } from '@testing-library/react'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { graphql, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route, Switch } from 'react-router-dom'
import { vi } from 'vitest'

import InstallationHelpBanner from './InstallationHelpBanner'

Expand All @@ -16,10 +17,13 @@ const server = setupServer()
beforeAll(() => {
server.listen()
})

beforeEach(() => {
cleanup()
queryClient.clear()
server.resetHandlers()
})

afterAll(() => {
server.close()
})
Expand All @@ -40,35 +44,33 @@ const wrapper =

describe('InstallationHelpBanner', () => {
function setup() {
const mutation = jest.fn()
const mutation = vi.fn()

const mockSetItem = jest.spyOn(window.localStorage.__proto__, 'setItem')
const mockGetItem = jest.spyOn(window.localStorage.__proto__, 'getItem')
const mockSetItem = vi.spyOn(window.localStorage.__proto__, 'setItem')
const mockGetItem = vi.spyOn(window.localStorage.__proto__, 'getItem')

server.use(
graphql.query('IsSyncing', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.data({
graphql.query('IsSyncing', (info) => {
return HttpResponse.json({
data: {
me: {
isSyncing: false,
},
})
)
},
})
}),
graphql.mutation('SyncData', (req, res, ctx) => {
mutation(req.variables)
graphql.mutation('SyncData', (info) => {
mutation(info.variables)

return res(
ctx.status(200),
ctx.data({
return HttpResponse.json({
data: {
syncWithGitProvider: {
me: {
isSyncing: true,
},
},
})
)
},
})
})
)

Expand Down Expand Up @@ -146,9 +148,9 @@ describe('InstallationHelpBanner', () => {
wrapper: wrapper(),
})

const dismissButton = await screen.findByRole('button', {
name: 'x.svg',
})
const dismissButton = await screen.findByTestId(
'dismiss-install-help-banner'
)
expect(dismissButton).toBeInTheDocument()
})

Expand All @@ -161,9 +163,9 @@ describe('InstallationHelpBanner', () => {
wrapper: wrapper(),
})

const dismissButton = await screen.findByRole('button', {
name: 'x.svg',
})
const dismissButton = await screen.findByTestId(
'dismiss-install-help-banner'
)
expect(dismissButton).toBeInTheDocument()
await user.click(dismissButton)

Expand All @@ -184,9 +186,9 @@ describe('InstallationHelpBanner', () => {
wrapper: wrapper(),
})

const dismissButton = await screen.findByRole('button', {
name: 'x.svg',
})
const dismissButton = await screen.findByTestId(
'dismiss-install-help-banner'
)
expect(dismissButton).toBeInTheDocument()
await user.click(dismissButton)

Expand Down
Loading
Loading