The most straight-forward way to test a jumpgen generator is with test fixtures and a test runner like Vitest.
-
For each test, create a test fixture in the
test/__fixtures__
directory. This is usually a folder containing the necessary files and directories to run the generator. Sometimes, you might generate certain files in the fixture at test setup time if they are common dependencies of the generator (i.e. apackage.json
file ortsconfig.json
file). -
In your test file, use a glob library like tinyglobby to find the test fixtures in the
__fixtures__
directory. In afor
loop, declare a test for each fixture. Generally, this means calling your generator's factory function with the fixture as theroot
directory. -
It's recommended to “gitignore” any output files created by the generator. You may also want to clean up after each test, to avoid leaving test artifacts in your fixture.
If you have any questions, please open an issue.
Another way to test a jumpgen generator is with “module mocking”, a feature supported by many test frameworks, like Vitest.
With module mocking, you can substitute usage of node:fs
with an in-memory implementation, allowing you to control the behavior of the file system for each test.
The recommended “mock implementation” is memfs
.
In this example, we'll use Vitest.
-
Install
memfs
andvitest
.pnpm add memfs vitest -D
-
Create the
__mocks__/fs.cjs
module in your test folder. This will contain the mock implementation. It's recommended to use CommonJS syntax, so you don't have to manually export each function frommemfs
.const { fs } = require('memfs') module.exports = fs
You don't need to mock
fs/promises
, since jumpgen only uses synchronous file system APIs. -
To ensure the filesystem mock is actually used, we need to tell Vitest to process Jumpgen, since the default behavior is to avoid processing
node_modules
entirely. In your Vitest config, add the following:export default defineConfig({ test: { server: { deps: { inline: ['jumpgen'], }, }, }, })
-
In your test file, tell Vitest to use the mock implementation of
fs
.import { beforeEach, vi } from 'vitest' import { fs, vol } from 'memfs' vi.mock('fs') // Reset the in-memory file system before each test. beforeEach(() => { vol.reset() })
For more information on using
memfs
, see the API reference.