Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

cypress-ct-custom-devserver

fochlac2.7kMIT2.1.0TypeScript support: included

A helper to simplify the api for creating a custom dev-server for cypress.

cypress, component test, devServer

readme

CircleCI npm

cypress-ct-custom-devserver

A helper for setting up Cypress devServers for component testing.

Compatibility

Package Version Cypress Version
< 2.0 < 14, > 12
>= 2.1 > 12

Description

This library offers a framework to abstract some of the difficulties regarding setting up a custom Cypress dev-server for component testing. Namely this library will setup an express server for delivering your bundled code and provide you with a clean api for setting up your build tools. You will have to take care of setting up the build process for the provided entry points and map the tests to their respective bundles.

Installation

Add the library to your devDependencies

npm install -D cypress-ct-custom-devserver

Example Configuration

In your cypress config or in a seperate file setup the devServer as follows:

const devServer = createCustomDevServer(async ({ cypressConfig, onBuildComplete, onBuildStart, specs, supportFile, serveStatic }) => {
    // first you need to start your prefered build tool in watch mode
    let onClose = await startBuild({
        watchMode: cypressConfig.watchForFileChanges,
        entryPoints: [...specs.map((spec) => spec.absolute), supportFile && supportFile.absolute],
        onBuildComplete, // this needs to be called whenever the build is complete so cypress can restart the test.
        onBuildStart // if this callback is provided the api will wait until the build is finished before it serves the bundles.
    })

    // if you use code splitting or want to reference assets, you should setup
    // static serving for your output folder and any other folder you want accessible from the test
    serveStatic('./output-folder')
    // you can also specify a custom path prefix
    serveStatic('./images', '/custom/image')

    return {
        // This callback is triggered when Cypress requests the bundle for a test.
        // The spec-parameter contains information about which test the bundle is requested for.
        // You can use the loadBundle function to import js files. The files need to be made available via serve static.
        // The path you provide should match the url the file has on the server. The sequence of imports will be preserved.
        // You can also inject html into the head or body using the injectHTML function. This can be used to inject i.e. styles.
        // This should be used sparingly though, as cypress makes html-snapshots after each command. Too large index.html files
        // will slow down your tests.
        loadTest: async (spec, { loadBundle, injectHTML }) => {
            // if you want to load the support bundle you need to do it before the test-bundle
            if (supportFile) {
                loadBundle(supportFile.relative)
            }

            // The path on the spec-object will have the original file extension. If the extension changed
            // during bundling you might have to adapt the path accordingly. You need to provide the 
            // relative path to the file as you serve it via serveStatic. 
            const testPath = spec.relative.replace(spec?.fileExtension, '.js')
            loadBundle(testPath)

            // per default html is injected at the end of the head element
            injectHTML('<style>body {background: red}</style>')
            // but you can also specify whether to use head or body. The html will be appended at the end of the element.
            injectHTML('<div class="portal"></div>', 'body')
        },
        // this callback will be called every time new tests are added or removed
        // you can use it to update your build-tool
        onSpecChange: async (newSpecs) => {
            await onClose()
            onClose = await startBuildInWatchMode({
            entryPoints: [...newSpecs.map((spec) => spec.absolute), supportFile && supportFile.absolute],
            onBuildComplete,
            onBuildStart
        })
        },
        // you can provide a custom port for the dev-server to use. default is 0, which means pick a random port
        devServerPort: 0,
        // you can provide a callback to gracefully shut down your dev server once cypress shuts down.
        onClose: () => onClose(),
        // you can provide a logging function, lowest loglevel is 6
        logFunction: (logLevel, ...messages) => logLevel > 4 && console.log(...messages) 
    }
})

const config = defineConfig({
    component: {
        devServer
    }
})

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

Added

  • Cypress 14+ compatibility with backward compatibility for older versions
  • Compatibility table in README

Changed

  • Event handler for dev-server:specs:changed now supports both old and new Cypress event signatures

2.0.5 - 2024-07-02

Added

  • Extended logging for static router functionality

2.0.4 - 2024-04-03

Added

  • Include types folder in published package (#1)

2.0.3 - 2024-02-05

Fixed

  • Fixed condition logic

2.0.2 - 2024-02-05

Fixed

  • Prevent rebuilds due to false positive spec-changed events

2.0.1 - 2024-01-12

Added

  • Unit tests and code refactoring

Fixed

  • URL creation for Unix systems

2.0.0 - 2024-01-04

Added

  • Preserve folder structure when importing tests
  • Improved logging for static mappings

Changed

  • BREAKING: loadBundle now requires relative paths to files as they are served via serveStatic

Fixed

  • Static mapping logging

1.2.0 - 2024-01-04

Added

  • Improved logging functionality
  • Better filename handling

Fixed

  • Stalling request logging

1.1.1 - 2023-08-07

Improved

  • Logging and route sequence handling

1.1.0 - 2023-08-07

Added

  • Expose Cypress configuration to callbacks

1.0.7 - 2023-05-23

Added

  • Configurable logging functionality

1.0.5 - 2023-05-11

Fixed

  • Build and publish process

Changed

  • Use prepublishOnly instead of deprecated prepublish

1.0.4 - 2023-05-11

Added

  • Support for relative paths (initial implementation)

1.0.0 - 2023-04-25

Added

  • Initial release
  • Express-based dev server for Cypress component testing
  • Custom build callback support
  • Static file serving
  • Test loading utilities
  • Build state management
  • Basic logging functionality

Infrastructure

  • TypeScript support
  • ESLint configuration
  • CircleCI integration
  • Unit test setup