New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

sweetest

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sweetest

Simple testing framework inspired by Jest

latest
Source
npmnpm
Version
0.0.2
Version published
Weekly downloads
3
Maintainers
0
Weekly downloads
 
Created
Source

Sweetest

Simple testing framework inspired by Jest.

This is not a fully functional testing framework, it's just a simple example of how you can create your own testing framework. Why would you do that? Because it's fun, duh!

Usage

Well, it's very similar to Jest, you have the same describe, it and expect functions, just with (much) less features:

import { describe, it, expect } from 'sweetest';

describe('My test suite', () => {
  it('should pass', () => {
    expect(1 + 1).toBe(2);
  });

  it('should fail', () => {
    expect(1 + 1).toBe(3);
  });

  describe('Nested suite', () => {
    it('should pass', () => {
      expect(null).toBeNull();
    });
  });
});

The output will be something like this:

❌ My test suite
  ✅ should pass
  ❌ should fail
  ✅ Nested suite
    ✅ should pass

Matchers

Currently, there is a limited list of built in matchers:

  • toBe(value): Strict equality with value
  • toBeNull(): Strict equality with null

But, you can still create your own matchers, just like in Jest. A "matcher" is a function that receives the value to be tested along with the expected value, and throws an AssertionError if the value is not as expected:

import { addMatcher, AssertionError, type Matcher } from 'sweetest';

const toBeGreaterThan: Matcher = (value: number, expected: number) => {
  if (value <= expected) {
    throw new AssertionError(`Expected ${value} to be greater than ${expected}`);
  }
};

addMatcher('toBeGreaterThan', toBeGreaterThan);

If you're using TypeScript, you'll also need to extend the Matchers interface using "Module Augmentation":

declare module 'sweetest' {
  interface Matchers {
    toBeGreaterThan(value: number, expected: number): void;
  }
}

Hooks

Same as in Jest, you can use hooks to run code before and after each test, or before and after each suite:

import { describe, it, beforeEach, afterEach, beforeAll, afterAll } from 'sweetest';

describe('My test suite', () => {
  beforeAll(() => {
    console.log('Before all tests');
  });

  beforeEach(() => {
    console.log('Before each test');
  });

  afterEach(() => {
    console.log('After each test');
  });

  afterAll(() => {
    console.log('After all tests');
  });

  it('should pass', () => {
    expect(1 + 1).toBe(2);
  });
});

Mocks

You can also create mock functions using the sw.fn function, and assert if they were called using the toHaveBeenCalled, toHaveBeenCalledTimes and toHaveBeenCalledWith matchers:

import { describe, it, expect, sw } from 'sweetest';

describe('My test suite', () => {
  it('should call the mock function', () => {
    const mock = sw.fn((count: number) => {
      return count * 2;
    });

    mock(1);
    mock(2);
    mock(3);

    expect(mock).toHaveBeenCalled();
    expect(mock).toHaveBeenCalledTimes(3);
    expect(mock).toHaveBeenCalledWith(1);
  });
});

FAQs

Package last updated on 31 Aug 2024

Did you know?

Socket

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