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

migrate to vitest #337

Merged
merged 2 commits into from
Jun 14, 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
1 change: 0 additions & 1 deletion __mocks__/fileMock.js

This file was deleted.

2 changes: 1 addition & 1 deletion __test__/pages/about.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react'

import About from '../../pages/about'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand Down
11 changes: 6 additions & 5 deletions __test__/pages/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs'

import { render, screen, waitFor, fireEvent } from '@testing-library/react'
import { MockInstance } from 'vitest'

import { PostMetadata } from '../../lib/postsApi'
import Index, { getStaticProps } from '../../pages/index'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand All @@ -16,7 +17,7 @@ jest.mock('next/router', () => ({
},
}))

jest.mock('../../pages/posts/building-this-site.mdx', () => ({
vi.mock('../../pages/posts/building-this-site.mdx', () => ({
metadata: {
coverImage: '/assets/blog/building-this-site/code.webp',
date: '2021-05-09T15:40:07.322Z',
Expand All @@ -28,7 +29,7 @@ jest.mock('../../pages/posts/building-this-site.mdx', () => ({
},
}))

jest.mock('../../pages/posts/bye-bye-popups.mdx', () => ({
vi.mock('../../pages/posts/bye-bye-popups.mdx', () => ({
metadata: {
title: 'Bye bye popups',
excerpt:
Expand All @@ -42,10 +43,10 @@ jest.mock('../../pages/posts/bye-bye-popups.mdx', () => ({

describe('Homepage', () => {
let posts: PostMetadata[]
let readdirSyncSpy: jest.SpyInstance
let readdirSyncSpy: MockInstance

beforeEach(async () => {
readdirSyncSpy = jest.spyOn(fs, 'readdirSync')
readdirSyncSpy = vi.spyOn(fs, 'readdirSync')
readdirSyncSpy.mockReturnValue([
'building-this-site.mdx',
'bye-bye-popups.mdx',
Expand Down
11 changes: 6 additions & 5 deletions __test__/pages/posts.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import fs from 'fs'

import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MockInstance } from 'vitest'

import { PostMetadata } from '../../lib/postsApi'
import Posts, { getStaticProps } from '../../pages/posts'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand All @@ -17,7 +18,7 @@ jest.mock('next/router', () => ({
},
}))

jest.mock('../../pages/posts/building-this-site.mdx', () => ({
vi.mock('../../pages/posts/building-this-site.mdx', () => ({
metadata: {
coverImage: '/assets/blog/building-this-site/code.webp',
date: '2021-05-09T15:40:07.322Z',
Expand All @@ -29,7 +30,7 @@ jest.mock('../../pages/posts/building-this-site.mdx', () => ({
},
}))

jest.mock('../../pages/posts/bye-bye-popups.mdx', () => ({
vi.mock('../../pages/posts/bye-bye-popups.mdx', () => ({
metadata: {
title: 'Bye bye popups',
excerpt:
Expand All @@ -43,11 +44,11 @@ jest.mock('../../pages/posts/bye-bye-popups.mdx', () => ({

describe('Posts', () => {
let posts: PostMetadata[]
let readdirSyncSpy: jest.SpyInstance
let readdirSyncSpy: MockInstance
let user: ReturnType<typeof userEvent.setup>

beforeEach(async () => {
readdirSyncSpy = jest.spyOn(fs, 'readdirSync')
readdirSyncSpy = vi.spyOn(fs, 'readdirSync')
readdirSyncSpy.mockReturnValue([
'building-this-site.mdx',
'bye-bye-popups.mdx',
Expand Down
18 changes: 10 additions & 8 deletions components/Header/Header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react'

import Header from './Header'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand Down Expand Up @@ -55,13 +55,15 @@ describe('Header', () => {
})

it('renders an extra link when on an individual post', async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const useRouter = jest.spyOn(require('next/router'), 'useRouter')
useRouter.mockImplementation(() => ({
route: '/',
pathname: '/',
query: '',
asPath: '/posts/foo',
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
pathname: '/',
query: '',
asPath: '/posts/foo',
}
},
}))
await setup()
const link = screen.getByRole('link', { name: '/foo' })
Expand Down
2 changes: 1 addition & 1 deletion components/Layout/Layout.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import userEvent from '@testing-library/user-event'

import Layout from './Layout'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand Down
2 changes: 1 addition & 1 deletion components/PostCard/PostCard.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('PostCard', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('displays the title', () => {
Expand Down
6 changes: 3 additions & 3 deletions components/PostLayout/PostLayout.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react'

import PostLayout from './PostLayout'

jest.mock('next/router', () => ({
vi.mock('next/router', () => ({
useRouter() {
return {
route: '/',
Expand Down Expand Up @@ -31,7 +31,7 @@ describe('PostLayout', () => {
}

beforeEach(async () => {
jest.spyOn(Date.prototype, 'toLocaleDateString').mockReturnValue(mockDate)
vi.spyOn(Date.prototype, 'toLocaleDateString').mockReturnValue(mockDate)
render(<PostLayout metadata={mockMetadata}>{mockContent}</PostLayout>)

// need to wait for the theme toggle to render
Expand All @@ -42,7 +42,7 @@ describe('PostLayout', () => {
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('displays the title', () => {
Expand Down
8 changes: 4 additions & 4 deletions components/PostTitle/PostTitle.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe('PostTitle', () => {
const mockTitle = 'mockTitle'

beforeEach(() => {
jest.spyOn(Date.prototype, 'toLocaleDateString').mockReturnValue(mockDate)
vi.spyOn(Date.prototype, 'toLocaleDateString').mockReturnValue(mockDate)
})

afterEach(() => {
jest.restoreAllMocks()
vi.restoreAllMocks()
})

it('displays the title', () => {
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('PostTitle', () => {
})

it('has a native "Share" button if it supports it', async () => {
navigator.share = jest.fn()
navigator.share = vi.fn()
const user = userEvent.setup()
render(
<PostTitle
Expand All @@ -126,7 +126,7 @@ describe('PostTitle', () => {
await user.click(button)
expect(navigator.share).toHaveBeenCalledWith({
title: mockTitle,
url: 'http://localhost/',
url: 'http://localhost:3000/',
})
})
})
2 changes: 1 addition & 1 deletion components/Search/Search.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Search from './Search'
describe('Search', () => {
const mockClassName = 'mockClassName'
const mockPlaceholder = 'mockPlaceholder'
const mockOnChange = jest.fn()
const mockOnChange = vi.fn()
let user: ReturnType<typeof userEvent.setup>

beforeEach(() => {
Expand Down
4 changes: 3 additions & 1 deletion components/Tag/Tag.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {

import Tag from './Tag'

jest.mock('randomcolor', () => () => '#012345')
vi.mock('randomcolor', () => ({
default: () => '#012345',
}))

describe('Tag', () => {
it('displays the tag label', () => {
Expand Down
7 changes: 4 additions & 3 deletions components/WordlePoem/Word.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { render, screen } from '@testing-library/react'
import { MockInstance } from 'vitest'

import Word from './Word'

describe('Word', () => {
let mockIntersectionObserver: jest.Mock
let mockIntersectionObserver: MockInstance

beforeEach(async () => {
// IntersectionObserver isn't available in test environment
mockIntersectionObserver = jest.fn()
mockIntersectionObserver = vi.fn()
mockIntersectionObserver.mockReturnValue({
observe: () => null,
unobserve: () => null,
disconnect: () => null,
})
window.IntersectionObserver = mockIntersectionObserver
vi.stubGlobal(`IntersectionObserver`, mockIntersectionObserver)
})

it('splits each word into individual characters and sets the correct classes', () => {
Expand Down
7 changes: 4 additions & 3 deletions components/WordlePoem/WordlePoem.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render, screen } from '@testing-library/react'
import { MockInstance } from 'vitest'

import WordlePoem from './WordlePoem'

Expand All @@ -15,17 +16,17 @@ const customMatcher =
}

describe('WordlePoem', () => {
let mockIntersectionObserver: jest.Mock
let mockIntersectionObserver: MockInstance

beforeEach(async () => {
// IntersectionObserver isn't available in test environment
mockIntersectionObserver = jest.fn()
mockIntersectionObserver = vi.fn()
mockIntersectionObserver.mockReturnValue({
observe: () => null,
unobserve: () => null,
disconnect: () => null,
})
window.IntersectionObserver = mockIntersectionObserver
vi.stubGlobal(`IntersectionObserver`, mockIntersectionObserver)
})

it('splits each line into separate words', () => {
Expand Down
1 change: 0 additions & 1 deletion config/jest-setup.ts

This file was deleted.

2 changes: 1 addition & 1 deletion config/jest-setup-after-env.ts → config/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom'
import failOnConsole from 'jest-fail-on-console'
import failOnConsole from 'vitest-fail-on-console'

failOnConsole({
shouldFailOnLog: true,
Expand Down
2 changes: 0 additions & 2 deletions custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ declare global {
const browser: Browser
const browserName: string
}

declare module 'jest-next-dynamic'
51 changes: 0 additions & 51 deletions jest.config.ts

This file was deleted.

10 changes: 6 additions & 4 deletions lib/postsApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs'

import { MockInstance } from 'vitest'

import { getAllPosts, getPostMetadata } from './postsApi'

jest.mock('../pages/posts/building-this-site.mdx', () => ({
vi.mock('../pages/posts/building-this-site.mdx', () => ({
metadata: {
coverImage: '/assets/blog/building-this-site/code.webp',
date: '2021-05-09T15:40:07.322Z',
Expand All @@ -14,7 +16,7 @@ jest.mock('../pages/posts/building-this-site.mdx', () => ({
},
}))

jest.mock('../pages/posts/bye-bye-popups.mdx', () => ({
vi.mock('../pages/posts/bye-bye-popups.mdx', () => ({
metadata: {
title: 'Bye bye popups',
excerpt:
Expand Down Expand Up @@ -43,10 +45,10 @@ describe('postsApi', () => {
})

describe('getAllPosts', () => {
let readdirSyncSpy: jest.SpyInstance
let readdirSyncSpy: MockInstance

beforeEach(() => {
readdirSyncSpy = jest.spyOn(fs, 'readdirSync')
readdirSyncSpy = vi.spyOn(fs, 'readdirSync')
})

afterEach(() => {
Expand Down
5 changes: 2 additions & 3 deletions lib/postsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ export interface PostMetadata {
}

export async function getPostMetadata(slug: string) {
const realSlug = slug.replace(/\.mdx$/, '')
const { metadata } = await import(`../pages/posts/${realSlug}.mdx`)
const { metadata } = await import(`../pages/posts/${slug}`)

return {
...metadata,
slug: realSlug,
slug: slug.replace(/\.mdx$/, ''),
}
}

Expand Down
Loading