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

mock-tmp

v0.0.4

Published

A (mostly) drop-in replacement for `mock-fs` using your tmp filesystem

Downloads

2,142

Readme

mock-tmp

A (mostly) drop-in replacement for mock-fs using your tmp filesystem to quickly mock files and folders

Keeping and maintaining mocks for testing can be a pain; mock-tmp can help make it easier by synchronously setting up and tearing down files and folders specified by a single, simple object.

Inspired by the excellent mock-fs, mock-tmp works solely within your system's native temp filesystem, and uses Node.js's built-in fs methods (instead of patching Node.js fs).

Example

import mock from 'mock-tmp'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'

// tmpDir is the path to a temporary directory
const tmpDir = mockTmp({
  // Create a single file (from a string)
  'hi.txt': 'hi there!',

  // Create an empty dir
  'henlo': {},

  // Create a (nested) dir containing multiple files
  'greetings/and/salutations': {
    'hey.json': JSON.stringify({ hey: 'friend' }),
    'yo.gif': Buffer.from([ 0, 1, 2, 3, 4 ]),
  },

  // Bring in an existing file
  'sup.jpg': mock.copy('/path/to/file.jpg'),

  // Bring in an existing folder recursively; optionally disable recursion
  'so-many-files': mockTmp.copy('/path/to/many-files', /* { recursive: false } */),
}) // /var/folders/.../mock-tmp-$random_string
readFileSync(join(tmpDir, 'hi.txt')) // 'hi there!'
readFileSync(join(tmpDir, 'greetings', 'and', 'salutations', 'hey.json')) // '{"hey":"friend"}'

mockTmp.reset()
existsSync(tmpDir) //false

Install

npm i mock-tmp -D

Usage

mock(resources)

mock() synchronously sets up the mock temp directory based on the object of resources it is passed.

  • Each property name represents the relative path to the generated resource
  • Each property value is written as the content for the generated resource
  • A nested object is interpreted as a folder with its own files
    • Note: folder nesting is only supported to one level. To nest deeper folders, create a corresponding top-level property.

Calling this method returns the tmp directory created containing all generated resources.

When called, if an existing temp directory is found from a previous instantiation, mock-tmp will destroy it. Additionally, mock-tmp will also register a handler to destroy its temp directory upon process exit.

mock.copy(path, options)

Alias: mock.load(path, options)

mock.copy() synchronously copies a file or folder to the corresponding property name; it accepts a source path and optional options object.

The options object accepts the following parameters

  • recursive (boolean) [default=true]
    • Disable recursive directory copying (if copying a folder)

mock.reset()

Alias: mock.restore()

mock.reset() attempts to synchronously destroy the mock temp directory. mock.reset() not guarantee the mock temp directory will be destroyed (such as in the case of existing file locks that may prevent deletion).

Note: calling mock() cleans up after itself, and automatically calls mock.reset() if it has not been manually called. Still, it is good hygiene to explicitly call mock.reset(), especially so others may more easily read your tests.

Coming from mock-fs

I love and have long relied on mock-fs – it's a simple, elegant library that has made implementing test mocks so easy. Unfortunately, Node.js 20.x introduced a number of breaking changes to fs methods, which broke a lot of core mock-fs functionality, necessitating the creation of mock-tmp.

mock-tmp was authored to be a future-proof, (mostly) drop-in replacement for mock-fs, including supporting mock-fs's mock.load() and mock.restore() methods.

However, at this time some mock-fs features are not (yet) supported, including specifying file and folder properties (mode, uid, gid, atime, ctime, mtime, birthtime). PRs are welcome if folks need such functionality for their projects!