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

@lifeiscontent/to-mocked

v1.0.0

Published

A TypeScript utility for simplifying mocked data creation. It creates proxy objects of any non-primitive type, throwing an error when an undefined property is accessed. Ideal for streamlining testing workflows and ensuring test data is accurately represen

Downloads

3

Readme

@lifeiscontent/to-mocked

Overview

This repository contains a TypeScript utility function toMocked, a tool designed to streamline the creation of mocked data for testing. The main motivation for this package is to address the challenges of creating complicated mock data structures that represent specific types. Instead of manually building an entire object for testing, this function allows you to specify the object type and provide only the necessary data used in the test.

If application code changes and breaks the tests, toMocked provides immediate feedback about how those expectations have changed, making it easier to maintain and update your test suite. This function is particularly helpful when used with testing frameworks like Vitest.

Function Overview

function toMocked<T extends object>(value: any): T;

Function Explanation

The function toMocked takes one type argument:

  1. T: The expected return type which extends an object. It's worth noting that in TypeScript, object is a type that represents non-primitive types, thus you can use not only plain objects, but also other complex types like arrays, etc.

And one parameter:

  1. value: The object that you want to create a proxy for.

The function uses the JavaScript Proxy object to define custom behavior for property lookups. In the get trap provided to the Proxy, we first check if the accessed property exists on the target object. If it doesn't, an Error is thrown. If it does, the property is retrieved from the target. If this property is an object itself, it's wrapped in another level of proxy by recursively calling toMocked.

Usage

Here's an example of how you could use the toMocked function:

let obj = toMocked({
  prop1: "Hello",
  prop2: {
    nestedProp: "World"
  }
});

console.log(obj.prop1); // "Hello"
console.log(obj.prop2.nestedProp); // "World"
console.log(obj.nonExistentProp); // Throws Error: Property "nonExistentProp" does not exist on...

This approach ensures that your test data is as close to the real thing as possible, and that your tests fail fast when expectations change.

Please ensure you have TypeScript installed in your project and the file containing the toMocked function is properly imported.


Remember, a Proxy object is a very powerful tool. When used wisely, it can streamline your testing process by creating more realistic and maintainable mock data.