Gathering detailed insights and metrics for vitest-plugin-random-seed
Gathering detailed insights and metrics for vitest-plugin-random-seed
Gathering detailed insights and metrics for vitest-plugin-random-seed
Gathering detailed insights and metrics for vitest-plugin-random-seed
Define and print an integer that can be used to seed fake data libraries
npm install vitest-plugin-random-seed
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
2 Stars
19 Commits
1 Forks
1 Watchers
1 Branches
1 Contributors
Updated on Mar 03, 2025
Latest Version
1.1.1
Package Id
vitest-plugin-random-seed@1.1.1
Unpacked Size
10.12 kB
Size
3.57 kB
File Count
9
NPM Version
10.8.2
Node Version
18.20.7
Published on
Mar 03, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Define and print an integer that can be used to seed libraries like ChanceJS, Falso, or FakerJS.
1npm i vitest-plugin-random-seed
In your vite.config.ts
or vitest.config.ts
file, add the plugin:
1import RandomSeed from 'vitest-plugin-random-seed'; 2 3export default defineConfig({ 4 plugins: [RandomSeed()], 5});
And in your tests, you can access the seed via import.meta.test.SEED
! That's really all this plugin does...
1console.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.
1// src/utils/testing/fake-objects.ts 2import seed from 'chance'; 3 4export const chance = seed(import.meta.test.SEED); 5 6export function fakeFilename() { 7 return chance.string(); 8}
1// src/utils/testing/fake-objects.ts 2import { faker } from '@faker-js/faker'; 3 4faker.seed(import.meta.test.SEED); 5 6export function fakeFilename() { 7 return faker.string.alphanumeric(); 8}
1// src/utils/testing/fake-objects.ts 2import { randDirectoryPath, seed } from '@ngneat/falso'; 3 4seed(import.meta.test.SEED); 5 6export function fakeFilename() { 7 return randFileName(); 8}
To add types for import.meta.test.SEED
, add vitest-plugin-random-seed/types
to your tsconfig.json
:
1{ 2 "compilerOptions": { 3 "types": ["vitest-plugin-random-seed/types"] 4 } 5}
When a randomized test fails, you can recreate the test case by setting the TEST_SEED
environment variable:
1TEST_SEED=123456 vitest
This will run tests with the specific seed.
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:
1// vitest.setup.ts 2import { seed } from '@fakerjs/faker'; 3import { beforeEach } from 'vitest'; 4 5beforeEach(() => { 6 seed(import.meta.test.SEED); 7});
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.
This project uses Bun. To install dependencies, run:
1bun i
To run unit and E2E tests:
1bun run test
To build the NPM package:
1bun run build
Or run any of the other commands:
1bun run check 2bun run test:coverage
No vulnerabilities found.
No security vulnerabilities found.