Giter Site home page Giter Site logo

floofies / js-test Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 27 KB

Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.

License: MIT License

TypeScript 56.35% JavaScript 43.65%
bdd expect testing unit-test

js-test's Introduction

js-test

Low budget expect-based unit tests. Isolates between invocations and safely contains everything that can go wrong.

Usage

  1. Import and instantiate the UnitTest class.
  2. Invoke method test, supply a description and callback with an expect parameter.
  3. Invoke the supplied expect function, supply the test data.
  4. Invoke the BDD testing methods of the returned object.
import UnitTest from "js-test";

const myTest = new UnitTest();

const testPromise = myTest.test("My Test", expect => {
	expect(2+2).toBe(4);
	expect(2+2).toNotBe(5);
});

testPromise.then(testStatus => {
	if(testStatus)
		console.log("Test passed!");
	else
		console.log("Test failed!");
});

Console output:

Test "My Test":
        ƒ Test started 2024-03-21T02:35:44.098Z
        ✓ Test PASSED in 0.081ms

Test passed!

UnitTest Class

new UnitTest();

The class UnitTest is used to execute tests, organize test settings, and store test logs.

Instance Methods

UnitTest.prototype.test Method

test( description: string, testFunction: (expect) => any ): Promise<boolean>

Calls the given testFunction function and supplies an expect function as the input parameter.

Returns a Promise which resolves to a boolean which indicates if the test passed or failed.

expect returns a new Expectation class instance. (See Expectation Class).

Here is an example of a valid testFunction:

function(expect) {
	expect(anyValue).toBe(anyValue);
}

Instance Properties

history Property

  • Type string[][]

The history property contains logs of past tests. Is an Array which contains Arrays of strings.

emit Property

  • Type boolean
  • Default = true

The emit property is a boolean which ndicates if test progress and results should be logged to console.

Expectation Class

new Expectation(anyValue: any);

The test callback is supplied with an expect factory function which returns an Expectation instance.

Instance Properties

actualValue Property

  • Type: any

The value given to expect, regarded as the "actual data" and not the expected data.

Instance Methods

Each of the following methods throws an ExpectError when its condition fails:

Method Expect Input Method Input Condition Description
toBe any any Passes based on the expected value being strictly equivalent to the given value.
toNotBe any any Opposite of toBe. Passes based on the expected value being strictly equivalent to the given value.
toReturn Function any Passes based on the expected function return value being strictly equivalent to the given value.
toNotReturn Function any Opposite of toReturn. Passes based on the expected function not returning the given value, or none at all.
toThrow Function any Passes based on the expected function throwing an instance of the given error, or at all.
toNotThrow Function any Opposite of toThrow. Passes based on the expected function not throwing an instance of the given error, or none at all.

js-test's People

Contributors

floofies avatar

Watchers

 avatar

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.