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

vitest-plugin-random-seed

v1.1.0

Published

Define and print an integer that can be used to seed libraries like [ChanceJS](https://github.com/chancejs/chancejs), [Falso](https://github.com/ngneat/falso), or [FakerJS](https://github.com/faker-js/faker).

Downloads

551

Readme

vitest-plugin-random-seed

Define and print an integer that can be used to seed libraries like ChanceJS, Falso, or FakerJS.

Installation

npm i vitest-plugin-random-seed

In your vite.config.ts or vitest.config.ts file, add the plugin:

import RandomSeed from 'vitest-plugin-random-seed';

export default defineConfig({
  plugins: [RandomSeed()],
});

And in your tests, you can access the seed via import.meta.test.SEED! That's really all this plugin does...

console.log(import.meta.test.SEED);

This plugin really shines when used in combination with ChanceJS, Falso, or FakerJS to generate reproducable, but random test data.

Chance

// src/utils/testing/fake-objects.ts
import seed from 'chance';

export const chance = seed(import.meta.test.SEED);

export function fakeFilename() {
  return chance.string();
}

FakerJS

// src/utils/testing/fake-objects.ts
import { faker } from '@faker-js/faker';

faker.seed(import.meta.test.SEED);

export function fakeFilename() {
  return faker.string.alphanumeric();
}

Falso

// src/utils/testing/fake-objects.ts
import { randDirectoryPath, seed } from '@ngneat/falso';

seed(import.meta.test.SEED);

export function fakeFilename() {
  return randFileName();
}

TypeScript Types

To add types for import.meta.test.SEED, add vitest-plugin-random-seed/types to your tsconfig.json:

{
  "compilerOptions": {
    "types": ["vitest-plugin-random-seed/types"]
  }
}

Setting the Seed

When a randomized test fails, you can recreate the test case by setting the TEST_SEED environment variable:

TEST_SEED=123456 vitest

This will run tests with the specific seed.

Test Isolation

By default, Vitest runs each test file is ran in their isolated context. That means if you use a fake data library, the seed is applied in every file. This means that running all tests, TEST_SEED=123 vitest, and running just one test, TEST_SEED=123 vitest some-file.test.ts, will result in the same random values being generated inside some-file.test.ts.

However, if you use it.only or it.skip, the test result for a file will change. To reset the seed for each test, add a setup file and reset the seed before each test:

// vitest.setup.ts
import { seed } from '@fakerjs/faker';
import { beforeEach } from 'vitest';

beforeEach(() => {
  seed(import.meta.test.SEED);
});

Because ChanceJS doesn't have a global seed, this won't work. You'll need to setup a global instance and reassign it before each test.

If you've disabled test isolation, you can manually reset the seed before each test as shown above.

Contributing

This project uses Bun. To install dependencies, run:

bun i

To run unit and E2E tests:

bun run test

To build the NPM package:

bun run build

Or run any of the other commands:

bun run check
bun run test:coverage