npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lmiller1990/vite-plugin-cypress-esm

v0.4.0

Published

Make ESM Modules mutable in the browser with Cypress and Vite

Downloads

12

Readme

@cypress/vite-plugin-cypress-esm

A Vite plugin that intercepts and rewrites ES module imports within Cypress component tests. The ESM specification generates modules that are "sealed", requiring the runtime (the browser) to prevent any alteration to the module namespace. While this has security and performance benefits, it prevents use of mocking libraries which would need to replace namespace members. This plugin wraps modules in a special Proxy implementation, allowing for instrumentation by libraries such as Sinon.

Note: This package is a pre-release and is not yet stable. There are likely to be bugs and edge cases. Please report any bugs here

Debugging

Run Cypress with DEBUG=cypress:vite-plugin-cypress-esm. You will get logs in the terminal, for the code transformation, and in the browser console, for intercepting and wrapping the modules in a Proxy.

Compatibility

| @cypress/vite-plugin-mock-esm | cypress | | ------------------------ | ------- | | >= v1 | >= v12 |

Usage

This plugin rewrites the ES modules served by Vite to make them mutable and therefore compatible with methods like cy.spy() and cy.stub() that require modifying otherwise-sealed objects. Since this is a testing-specific plugin it is recommended to apply it your Vite config only when running your Cypress tests. One way to do so would be in cypress.config:

import { defineConfig } from 'cypress'
import viteConfig from './vite.config'
import { mergeConfig } from 'vite'
import { CypressEsm } from '@cypress/vite-plugin-cypress-esm'

export default defineConfig({
  component: {
    devServer: {
      bundler: 'vite',
      framework: 'react',
      viteConfig: () => {
        return mergeConfig(
          viteConfig,
          {
            plugins: [
              CypressEsm(),
            ]
          }
        ),
      }
    },
  }
})

Some modules may be incompatible with Proxy-based implementation. The eventual goal is to support wrapping all modules in a Proxy to better facilitate testing. For now, if you run into any issues with a particular module, you can add it to the ignoreList like so:

CypressEsm({
  ignoreList: ['react-router', 'react-router-dom']
})

You can also use a glob, which uses picomatch internally:

CypressEsm({
  ignoreList: ['*react*']
})

React is known to have some conflicts with the Proxy implementation. You probably don't want to stub your UI library anyway, so it's a good idea to add it to the ignoreList.

Known Issues

  • This module uses Regular Expression matching to transform the modules on the server to facilitate wrapping them in a Proxy on the client. In future updates, a more robust AST-based approach will be explored.
  • All known import syntax is supported, however there may edge cases that have not been identified
  • Auto-hosting of imports is not performed, rather they are currently transformed in place. This may result in some code behaving differently, typically observed as a "use before define" error.

React Router and Lazy Routes Issue

There is a known edge case with lazy routes in React Router. Given:

// App.tsx
import {lazy, Suspense} from 'react'
import {BrowserRouter, Routes, Route} from 'react-router-dom'
const About = lazy(() => import('./About'))

export default function App() {
  return (
    <BrowserRouter>
      <Suspense fallback={<div />}>
        <Routes>
          <Route path="/about" element={<About />} />
        </Routes>
      </Suspense>
    </BrowserRouter>
  )
}

// About.tsx
const About = () => <h1>About</h1>

export default About

You will encounter a cryptic error. The fix is simply to use a named function instead of an anonymous function assigned to a const:

// About.tsx
function About () {
  return <h1>About</h1>
}

export default About

License

license

This project is licensed under the terms of the MIT license.

Changelog