Skip to content

sjha2048/assert-apex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

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.


About

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

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors