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

@altano/vitest-plugins

v0.1.0

Published

Custom matchers and snapshot serializers to enhance vitest

Downloads

83

Readme

vitest-plugins

npm Typed with TypeScript ESM only

Custom matchers and snapshot serializers to enhance vitest.

Installation

npm install -D @altano/vitest-plugins

To install the custom matchers

Modify vite.config.ts / vitest.config.ts:

export default defineConfig({
  test: {
    setupFiles: [
      "@altano/vitest-plugins/matchers",
      // ...
    ],
    // ...
  },
});

Add matcher types to your tsconfig.json (if using TypeScript):

{
  "compilerOptions": {
    "types": ["@altano/vitest-plugins/matchers"]
  }
}

To install the snapshot serializers

Modify vite.config.ts / vitest.config.ts:

export default defineConfig({
  test: {
    setupFiles: [
      "@altano/vitest-plugins/serializers",
      // ...
    ],
    // ...
  },
});

NOTE: You can pick and choose what to install: the matchers and serializers don't depend on each other.

Details

Snapshot Serializers

VFile

Will format the contents of a vfile using Prettier (auto-detecting the type from the vfile's filename), e.g.

FormattedVFile {
  "cwd": "<cwd>",
  "data": {},
  "history": [
    "tests/unit/__fixtures__/basic/input.js",
  ],
  "map": undefined,
  "messages": [],
  "value": "function face() { }"
}

Absolute Paths

Will replace any instances of process.cwd() with <cwd> in the snapshot. Useful when serializing strings that contain absolute paths, since those will be different on other machines running the tests.

Matchers

Vitest's error matchers let you match against the error message, but not the rest of the Error object:

  • toThrow(error?) - error is thrown (docs)
  • toThrowErrorMatching[Inline]Snapshot - an error exactly matches a snapshot (docs)

If you want to assert anything more complicated (e.g. an error contains some substring in the stack) then you'll need these custom matchers:

toMatchError

Verify any part of an error object (e.g. the stack):

expect(new Error("face")).toMatchError(
  expect.objectContaining({
    stack: expect.stringContaining("readme.spec.ts"),
  }),
);

toThrowErrorMatching

Verify any part of a thrown error object (e.g. the stack):

expect(() => {
  throw new Error("face");
}).toThrowErrorMatching(
  expect.objectContaining({
    stack: expect.stringContaining("readme.spec.ts"),
  }),
);

toBePath

Verify the realpath (canonical path) of expected. More lenient than a string check when dealing with logical paths, symlinks, etc.

expect("/private/some/path").toBePath("/some/path");

toBeFile

Verify a file exists

expect(import.meta.filename).toBeFile();

toBeDirectory

Verify a directory exists

expect("/").toBeDirectory();

toEqualFile

Verify that the given path matches the contents of another file

expect("/some/file.txt").toEqualFile("/other/file.txt");