Socket
Socket
Sign inDemoInstall

jest

Package Overview
Dependencies
0
Maintainers
1
Versions
354
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    jest

JavaScriptational State Transfer. JS restful API layer with Mongoose based resources. Inspired by python Tastypie


Version published
Maintainers
1
Install size
4.51 MB
Created

Package description

What is jest?

Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue, and more. It's designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar, and feature-rich API that gives you results quickly.

What are jest's main functionalities?

Unit Testing

Jest allows you to write unit tests for your JavaScript functions. In this example, a simple test is written to check if the addition of 1 and 2 equals 3.

test('adds 1 + 2 to equal 3', () => {
  expect(1 + 2).toBe(3);
});

Mocking

Jest provides a powerful mocking library that lets you mock objects, functions, and modules. In this example, an HTTP request is mocked to test the behavior of a function that fetches user data.

jest.mock('../api');

test('should fetch users', () => {
  const users = [{name: 'Bob'}];
  const resp = {data: users};
  axios.get.mockResolvedValue(resp);

  return Users.all().then(data => expect(data).toEqual(users));
});

Snapshot Testing

Snapshot testing enables you to test your UI or any serializable value to ensure it does not change unexpectedly. This example demonstrates testing an object against a snapshot, with some fields expected to match specific types rather than values.

test('will check the matchers and pass', () => {
  const user = {
    createdAt: new Date(),
    id: Math.floor(Math.random() * 20),
    name: 'LeBron James'
  };

  expect(user).toMatchSnapshot({
    createdAt: expect.any(Date),
    id: expect.any(Number)
  });
});

Asynchronous Testing

Jest supports testing of asynchronous code. In this example, an asynchronous function `fetchData` is tested to ensure it resolves with the expected value 'peanut butter'.

test('the data is peanut butter', async () => {
  await expect(fetchData()).resolves.toBe('peanut butter');
});

Other packages similar to jest

Readme

Source

Jest

JavaScriptational State Stasfer for node.js with easy generating resource from Mongoose ORM

introduction

This module provides:

  • Resource base class with:
  • Authentication
  • Authorization
  • Pagination
  • Cache
  • Throttling
  • Validation
  • MongooseResource
  • Resources listing

synopsis

var express = require('express')
	, app = express.createServer(),
	, mongoose = require('mongoose')
	, Jest = require('jest');

var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/app');

// create mongoose model

var User = mongoose.model('user', new Schema({
    username: {type: String, required: true},
    email: String,
    password: {type: String, validate: [function(v) { return true}, 'custom validate']},
    credits: {type: Number, min: 1, max: 230},
    role: {type: String, 'default': 'user' ,enum: ['user', 'admin']},
    date: {type:Date, 'default': Date.now},
    groups: [{name:String, permissions: [{name:String, expires:Date}]}]
}));

// create mongoose resource for User model

var UserResource = Jest.MongooseResource.extend({
	init: function(){
		// call Jest.Resource constructor
		// passing the Model User we created
		this._super(User);

		// use array to decide which fields will be visible by API
		// this.fields = ['username','credits'];
		// use tree object to decide recursively which fields to expose
		this.fields = {'username': true, 'credits': true, groups: {name: true, permissions: {name: true} }};

		// use list or
		this.update_fields = ['email', 'password'];

		// specify base query for the model
		this.default_query = function(query){
			return query.where('credits').gte(10);
		};

		// specify which fields can be used to filter
		this.filtering = {'credits': true};

		// which http methods are allowed
		this.allowed_methods = ['get', 'post', 'put'];
	}
})

var api = new Jest.Api('api', app);

api.register('users', new UserResource());

installation

$ npm install jest

documentation

There is none. But there is an example, and a test.

And maybe one day will be...

Keywords

FAQs

Last updated on 19 Apr 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc