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 46 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
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,49 +222,51 @@ 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, { status: 200 })
}),
graphql.query('CurrentUser', (info) => {
return HttpResponse.json({ data: currentUser }, { status: 200 })
}),
graphql.query('DetailOwner', (info) => {
return HttpResponse.json({ data: mockOwner }, { status: 200 })
}),
http.get('/internal/:provider/:owner/account-details', (info) => {
return HttpResponse.json({}, { status: 200 })
}),
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({
myOrganizationsData: {
me: {
myOrganizations: {
edges: [],
pageInfo: { hasNextPage: false, endCursor: 'MTI=' },
graphql.query('HasAdmins', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
}),
graphql.query('Seats', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
}),
graphql.query('TermsOfService', (info) => {
return HttpResponse.json({ data: {} }, { status: 200 })
}),
graphql.query('UseMyOrganizations', (info) => {
return HttpResponse.json(
{
data: {
myOrganizationsData: {
me: {
myOrganizations: {
edges: [],
pageInfo: { hasNextPage: false, endCursor: 'MTI=' },
},
},
},
},
})
},
{ status: 200 }
)
),
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: {} }, { status: 200 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In App.test.tsx you added { status: 200 } as an example of setting status which I'm cool with, but since 200 is default, IMO we shouldn't make explicitly stating it a pattern across the codebase. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea we can remove these

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tyty appreciate

}),
http.get('/internal/users/current', (info) => {
return HttpResponse.json({}, { status: 200 })
})
)
}

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,34 +44,38 @@ 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({
me: {
isSyncing: false,
graphql.query('IsSyncing', (info) => {
return HttpResponse.json(
{
data: {
me: {
isSyncing: false,
},
},
})
},
{ status: 200 }
)
}),
graphql.mutation('SyncData', (req, res, ctx) => {
mutation(req.variables)

return res(
ctx.status(200),
ctx.data({
syncWithGitProvider: {
me: {
isSyncing: true,
graphql.mutation('SyncData', (info) => {
mutation(info.variables)

return HttpResponse.json(
{
data: {
syncWithGitProvider: {
me: {
isSyncing: true,
},
},
},
})
},
{ status: 200 }
)
})
)
Expand Down Expand Up @@ -146,9 +154,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 +169,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 +192,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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { graphql, rest } from 'msw'
import { setupServer } from 'msw/node'
import { cleanup, renderHook, waitFor } from '@testing-library/react'
import { delay, graphql, http, HttpResponse } from 'msw2'
import { setupServer } from 'msw2/node'
import { MemoryRouter, Route } from 'react-router-dom'

import config from 'config'
Expand All @@ -10,7 +10,7 @@ import { User } from 'services/user'

import { useUserAccessGate } from './useUserAccessGate'

jest.spyOn(console, 'error')
vi.spyOn(console, 'error')

const queryClient = new QueryClient({
defaultOptions: {
Expand Down Expand Up @@ -221,6 +221,7 @@ beforeAll(() => {
})

beforeEach(() => {
cleanup()
queryClient.clear()
server.resetHandlers()
})
Expand Down Expand Up @@ -251,33 +252,33 @@ describe('useUserAccessGate', () => {
internalUser: internalUserHasSyncedProviders,
}
) {
const mockMutationVariables = jest.fn()
const mockMutationVariables = vi.fn()

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, { status: 200 })
}),

graphql.query('CurrentUser', (req, res, ctx) => {
return res(ctx.status(200), ctx.data(user))
graphql.query('CurrentUser', (info) => {
return HttpResponse.json({ data: user }, { status: 200 })
}),
graphql.mutation('updateDefaultOrganization', (req, res, ctx) => {
mockMutationVariables(req.variables)
graphql.mutation('updateDefaultOrganization', async (info) => {
mockMutationVariables(info.variables)

if (delayMutation) {
return res(ctx.delay(1000), ctx.status(200), ctx.data({}))
await delay(1000)
return HttpResponse.json({}, { status: 200 })
}

return res(
ctx.status(200),
ctx.data({
return HttpResponse.json({
data: {
updateDefaultOrganization: {
defaultOrg: {
username: 'criticalRole',
},
},
})
)
},
})
})
)

Expand All @@ -286,7 +287,7 @@ describe('useUserAccessGate', () => {
}
}

afterEach(() => jest.resetAllMocks)
afterEach(() => vi.resetAllMocks)

describe.each([
[
Expand Down
File renamed without changes.
Loading