Skip to content

Instantly share code, notes, and snippets.

@Kotauror
Last active March 6, 2018 19:47
Show Gist options
  • Save Kotauror/242e9f881d3a60ea58ec5a253c31799f to your computer and use it in GitHub Desktop.
Save Kotauror/242e9f881d3a60ea58ec5a253c31799f to your computer and use it in GitHub Desktop.
Setting up a testing framework in JS

Setting up a JS testing framework from scratch.

Without any libraries.

// This note is a summary of work of the Moisty team, week 7 of Makers Academy. Thank you Laura, Daniel, Josue, Josh and Terry.

// Check out the full repo here: https://github.com/lwkchan/moisty.

Files

expect.js

This file contains the matchers (toEqual, toInclude etc.).

const expect = { //vars are accessible everywhere, even when module.
  toEqual: function(assert, expect) {
    return (assert === expect ? console.log('%c Equality test passed.', 'color: green') : (console.log('%c The test failed as the assertion does not equal the expectation', 'color: red')))
  },
  toInclude: function(assert, expect) {
    return (assert.includes(expect) ? console.log('%c Inclusion test passed', 'color: green') : console.log('%c The test failed as the tested object does not include the expected content', 'color: red' ))
  },
  toBeTrue: function(assert) {
    return (assert === true ? console.log('%c Truth test passed', 'color: green') : console.log("The statement is not truthy. The test failed."))
  },
  toBeEmpty: function(assert) {
    return (assert.length === 0 ? console.log('%c The assertion is empty, test passed.', 'color: green') : console.log('%c The assertion is not empty, test failed.', 'color: red'))
  },
}

describeit.js

This file contains definitions of describe, before and it blocks.

var Before = function() {};

Before.prototype.beforeEach = function(callback) {
 this.callback = callback;
};

var before = new Before();

(function(exports) {
 function describe(string, callback) {
   console.log('%c' + string, 'color: darkblue');
   callback();
 }

 function it(string, callback) {
   console.log(string);
   if (before.callback !== undefined) {
     before.callback();
   }
   callback();
 }

 exports.describe = describe; // exports it to global scope
 exports.it = it; // exports it to global scope
})(this);

notepad-tests.js

This file contains drafts of tests

describe('Notepad', function() {
  var notepad;
  before.beforeEach(function() {
    notepad = new Notepad();
  });
  it('----> it is empty*', function() {
    expect.toBeEmpty(notepad.content);
  });
  it('----> it is empty**', function() {
    notepad.content.push('ehehhehhe');
    expect.toBeEmpty(notepad.content);
  });
  it('----> it is empty***', function() {
    expect.toBeEmpty(notepad.content);
  });
  it('----> it includes hehehe', function() {
    notepad.content.push('hehehe');
    expect.toInclude(notepad.content, 'hehehe');
  });
  it('----> it includes hihihi', function() {
    notepad.content.push('hehehe');
    expect.toInclude(notepad.content, 'hihihi');
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment