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

assert-apex

v1.0.2

Published

Generate Chai assertions from API response objects

Downloads

179

Readme

Assert Apex: Effortless Chai Assertion Generation

npm version
license

Generate Chai assertions effortlessly from JSON objects or API response payloads. Simplify testing by letting assert-apex handle repetitive assertion generation tasks.


✨ Features

  • Automatically generate Chai assertions from any JSON structure.
  • Supports deep nested objects and arrays.
  • Configurable maximum depth for traversing nested structures.
  • Intelligent type-checking with detailed validation messages.
  • Reduces boilerplate code in tests and speeds up development.

📦 Installation

Install the library using npm:

npm install assert-apex

🚀 Quick Start

// Import the library
const AssertApex = require('assert-apex');

// Initialize the generator with optional configurations
const generator = new AssertApex({
  maxDepth: 10 // Maximum depth to traverse for nested objects (default: 10)
});

// Example API response or JSON object
const response = {
  userId: 1,
  title: "Test",
  completed: false
};

// Generate Chai assertions
const assertions = generator.generate(response);

// Output the generated assertions
console.log(assertions);

Output:

expect(response.userId, "userId should exist").to.exist;
expect(response.userId, "userId should be a number").to.be.a('number');
expect(response.title, "title should exist").to.exist;
expect(response.title, "title should be a string").to.be.a('string');
expect(response.completed, "completed should exist").to.exist;
expect(response.completed, "completed should be a boolean").to.be.a('boolean');

🛠 Configuration Options

You can configure the generator to suit your needs by passing an options object to the constructor:

| Option | Type | Default | Description | |---------------|------------|-------------|---------------------------------------------------------------------------------| | maxDepth | number | 10 | Specifies the maximum depth to traverse nested objects and arrays. |


💡 How It Works

The library intelligently traverses your JSON structure and generates:

  • Existence checks: Ensures every key in the object exists.
  • Type assertions: Validates data types for all keys.
  • Structure validation: Handles nested objects, arrays, and mixed types.

Supported Assertions

  • Existence: to.exist, to.be.null
  • Type: to.be.a('string'), to.be.an('array')
  • Structure: Validates non-emptiness and consistent array types.

🖋 Example Scenarios

1. Nested Object

const response = {
  user: {
    id: 42,
    profile: {
      name: "John Doe",
      age: 30
    }
  }
};

console.log(generator.generate(response));

Output:

expect(response.user, "user should exist").to.exist;
expect(response.user, "user should be an object").to.be.an('object');
expect(response.user.id, "id should exist").to.exist;
expect(response.user.id, "id should be a number").to.be.a('number');
expect(response.user.profile, "profile should exist").to.exist;
expect(response.user.profile, "profile should be an object").to.be.an('object');
expect(response.user.profile.name, "name should exist").to.exist;
expect(response.user.profile.name, "name should be a string").to.be.a('string');
expect(response.user.profile.age, "age should exist").to.exist;
expect(response.user.profile.age, "age should be a number").to.be.a('number');

2. Arrays with Mixed Types

const response = {
  items: [1, "string", { key: "value" }]
};

console.log(generator.generate(response));

Output:

expect(response.items, "items should exist").to.exist;
expect(response.items, "items should be an array").to.be.an('array');
expect(response.items[0], "items[0] should be a number").to.be.a('number');
expect(response.items[1], "items[1] should be a string").to.be.a('string');
expect(response.items[2], "items[2] should be an object").to.be.an('object');

🎨 Advanced Customization

  • Modify the maxDepth option for large or complex JSON structures.
  • Add support for custom validation logic by extending the addAssertion method.

🤝 Contributing

We welcome contributions! Check out the contribution guidelines for more details.


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.


💬 Support

For questions or issues, please open an issue on GitHub.