[RFC] Vite Plugin for Next.js Component Testing with Vitest #28428
Closed
valentinpalkovic
started this conversation in
RFC
Replies: 1 comment
-
This is released in 8.3! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
This RFC proposes a new Vite plugin, vite-plugin-nextjs, to enable component testing for Next.js components within a Vitest environment. This plugin aims to leverage Vitest's new browser mode, enabling visual component rendering and testing and addressing the limitations of the existing
next/jest.js
plugin.Problem Statement
When a React component renders inside Next.js, its behavior can reflect LOTS of context. Obvious context includes things like environment variables and “smart” handling of images, fonts, and various CSS styling systems. Subtle context includes things like the compiler settings that affect the actual output code. In testing, accurately mocking/emulating this context can be the difference between a useful test and false positives/negatives.
The
next/jest.js
plugin is a solution for mocking a component’s environment. It offers several advantages for mocking components, mainly focusing on non-visual testing environments. However, it has notable limitations:next/font
is entirely mocked.These limitations make
next/jest.js
unsuitable for visual component testing. In contrast, Vitest, with its experimental browser mode, can render components in a natural browser environment, allowing for more comprehensive visual testing and assertions.Goals
next/font
next/image
next/router
next/navigation
next/cache
next/head
next/link
@storybook/nextjs'
Webpack builder.Non-goals
next/navigation
,next/router
, or any other API, which requires a React Context setup. Therefore, rendering more complex components that require additional context setup only works inside Storybook as a plugin for Storybook’s Vite builder or when doing tests with Vitest in conjunction with portable stories.Implementation
Registering the Plugin
To register the
vite-plugin-nextjs
invitest.config.ts
, the following configuration is proposed:Usage
The plugin assumes the following prerequisites:
Prelude: Portable Stories API
Understanding the portable stories API is essential before diving into the implementation. For more information, refer to Portable Stories API Documentation.
Vitest Plugin: Implementation Example
Example of mounting a Next.js component using Testing Library and the portable stories API:
The plugin respects CSS, fonts, and images, allowing the component to be displayed visually true to the original in Vitest's browser mode.
When [Storybook RFC #28386](#28386) gets implemented, it will even not be necessary to write dedicated tests via Vitest. Vitest will be able to recognize
.stories.js|ts
files and transform them on the fly to component tests featured by Vitest.Deliverables
vite-plugin-nextjs
.Risks
Unresolved Questions
Alternatives considered / Abandoned Ideas
Enhancing next/jest.js: Modifying the existing Jest plugin to support visual testing. This would mean though that we couldn’t provide a solution for the Vitest community.
Beta Was this translation helpful? Give feedback.
All reactions