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

@gabriel-dias/light-test

v1.1.0

Published

Light test provides some functions to write tests and mocks in a simple and easy way.

Downloads

1

Readme

Light Test

A light and simple test tool

Light test provide some functions to write tests and mocks in a simple and easy way.

Features

  • Write test files as you are writing a super cool text!
  • Some built-in functions to check your results!
  • Last, but not less important, functions to mock your code & values!

How to install

After installing it, using

npm install -g @gabriel-dias/light-test

you are almost ready to go! You already can start writing your tests, but why not write down some scripts to run our tests?

Inside your package.json add:

  "scripts": {
    "test": "testing --folder ./src/tests"
  },

or

  "scripts": {
    "test": "testing file.js"
  },

You always can keep it simple and use the node file.js command.

Main functions

Lets do a brief summary of the functions available:

testing(description: string, runBlockOfTest: () => void): void

If you want to be descriptive and divide a file into a different block of tests, testing is the perfect choice for it, each block of test will have its own description and code to run.

when(description: string, runBlockOfTest: () => void): void

To create some test cases and organize your code and output, you can divide your tests inside this block of code. It improves the output message as well.

receive(receiveValue: any): TestData

This is where the magic happens, you pass the value and it will create a super cool object with all the available test options! You should have the following options:

  • expect(expect: any) - Passing the expected value as a parameter.
  • expectTruthy() - Compare if the received value is Truthy.
  • expectFalsy() - Compare if the received value is Falsy.
  • expectError() - Check if the function throws an error while running.
  • expectToHaveBeenCalled() - Verify if the function was called.
  • expectCalledTimes(times: number) - Compare if the number of times that the function was called is equal to the expected value.
createListener(moduleToListen: ObjectInput, key: string): Listener

If you like to mock things, so this is the perfect choice for you. Passing the module/file that you want to mock, will return a listener with some methods to mock the behavior of the function or the return value.

How to write your tests

Let's say you have a file to be tested: myFile.js

function checkUserAge(age) {
  if (age < 18) return "You must have 18 years old";

  return "Hello!";
}

You can create a file:

myTest.test.js or even myTest.js

import { testing, receive } from "light-test/lightTest.js"; // Import relative to your package.
import { checkUserAge } from "myFile.js";

testing("Function checkUserAge", () => {
  const success = "Hello!";
  const failed = "You must have 18 years old";

  receive(checkUserAge(18)).expect(success);
  receive(checkUserAge(17)).expect(failed);
});

or if you want to be more descriptive and organize your test better:

import { testing, receive } from "light-test/lightTest.js"; // Import relative to your package.
import { checkUserAge } from "myFile.js";

testing("Function checkUserAge", () => {
  const success = "Hello!";
  const failed = "You must have 18 years old";

  when("The age is +18, should have a success message", () => {
    receive(checkUserAge(18)).expect(success);
  });

  when("The age is under 18, should have a fail message", () => {
    receive(checkUserAge(17)).expect(failed);
  });
});

You can find some usage examples in the examples folder, check it to see more complex cases and other functions.

Code:

Check the code on the GitHub: https://github.com/Gabriel-Dias-Oliveira/light-test

How to be a contributor:

Feel free to help with the code and issues. This should be pretty simple:

  • Clone the repository.
  • npm install to install the dependecies.
  • You should be ready to go.

To test what you are doing:

  • ctrl + shift + b and build the typescript project. (Using VSCode).