Is it possible to run Storybook with .stories and .spec files in the same project?

Integrating Storybook and Test Files in a Single Vue.js Project: Is It Feasible?

As developers dive into modern frontend workflows, combining component documentation with testing can streamline development and improve code quality. However, integrating tools like Storybook with existing testing frameworks can sometimes lead to unforeseen challenges. In this article, we explore a common issue faced when attempting to run Storybook alongside existing .spec files within the same Vue.js project, and we provide insights into potential solutions.

The Scenario

Imagine a developer who has an existing Vue.js application with a few components and associated unit tests written using Vitest. These tests reside in .spec files and are configured to evaluate store modules within the application. The developer decides to introduce Storybook into the project to better showcase UI components and document their usage.

The Challenge

Upon installing Storybook and creating the first component, the developer notices that their existing .spec files cease to be recognized under the Vitest test coverage reports. Interestingly, Storybook continues to correctly display the .stories files, but Vitest’s coverage reports indicate that no tests are found for the store modules, even though the coverage tool can locate the files.

The developer attempted to resolve this by creating a separate Vitest configuration specifically for the .spec files. However, doing so inadvertently causes the Storybook story coverage to break or behave unpredictably.

Understanding the Underlying Issue

The core of the problem lies in how test and story files are configured and how our testing and documentation tools interact with your project files. Several key points are worth noting:

  • File Recognition Patterns: Both Storybook and Vitest rely on glob patterns defined in their configuration files to identify relevant files. Overlapping or conflicting patterns can lead to one tool ignoring files intended for the other.

  • Shared Configurations: Using separate configuration files for testing can sometimes cause path resolution issues, especially when story files (.stories.js/.vue) and test files (.spec.js/.ts) are located in the same directories or follow similar naming conventions.

  • Tool Interactions: Since Storybook is primarily a UI component explorer, it doesn’t inherently interfere with test files unless configured to do so. Conversely, Vitest’s coverage and test discovery can be affected by the patterns specified in its configuration.

Possible Solutions and Best Practices

  1. Refine File Pattern Configurations

Ensure that your Vitest configuration explicitly includes only your test files, e.g.:

“`js
// vitest.config


Leave a Reply

Your email address will not be published. Required fields are marked *