Giter VIP home page Giter VIP logo

vtest's Introduction

Vtest - A very simple test library

Thinking about how a test library works I decided to create my own test library using TypeScript, taking inspiration from the functionality and design of existing libraries such as Jest. The code is available for anyone to clone and learn from.

Run Locally

Clone the project

git clone https://github.com/Andre0n/vtest

Go to the project directory

cd vtest

Install dependencies

npm install

Compiling the code

npm run build

Usage/Examples

Running the tests.

npm test

Now let's dive into the tests.js file.

Basic usage of this lib

  • Create a basic test suite
const vtest = require('./vtest');

const my_suite = new vtest.TestSuite("Test Suite Example");
  • Use the function add_test to create a new test
my_suite.add_test("1 + 1 should be 2");
  • This function returns the expect() function that returns matchers like toBe() that compares whether two elements are equals.
my_suite
  .add_test(" 1 + 1 should be 2")
  .expect(() => {
    return 1 + 1;
  })
  .toBe(2);

my_suite
  .add_test(" 'aabb' == 'aabb' ")
  .expect(() => {
    return "aabb";
  })
  .toBe("aabb");
  • The toBe has a variant the notBe
my_suite
  .add_test("2 - 1 should not be 3")
  .expect(() => {
    return 2 - 1;
  })
  .notBe(3);

Other important matchers

  • The toBeDefined
my_suite
  .add_test("check if was defined")
  .expect(() => {
    return {};
  })
  .toBeDefined();
  • The toBeCloseTo
my_suite
  .add_test("check if 0.1 + 0.2 is close to 0.3")
  .expect(() => {
    return 0.1 + 0.2;
  })
  .toBeCloseTo(0.3);
  • The toBeNull
my_suite
  .add_test("check if is null")
  .expect(() => {
    return null;
  })
  .toBeNull();
  • The notBeNull
my_suite
  .add_test("check if is not null")
  .expect(() => {
    return null;
  })
  .notBeNull();

Checking the results

To print test results the TestSuite object has the report() method.

my_suite.report();

Output

Our example will produce the following result

Test Suite Example
  ✔  1 + 1 should be 2
  ✔  'aabb' == 'aabb'
  ✔ 2 - 1 should not be 3
  ✔ check if was defined
  ✔ check if 0.1 + 0.2 is close to 0.3
  ✔ check if is null
  ✖ check if is not null  notBeNull: The object is null
         >> Expected: any  Got: null <<

Tests:  Passed: 6, Failed: 1, Total: 7

License

MIT

Authors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.