Coder Social home page Coder Social logo

justinbourb / jstinytest_dom_edition Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joewalnes/jstinytest

0.0 1.0 0.0 293 KB

Tiniest simplest unit testing library for JavaScript. With jstinytest_dom_edition there's no need to look in the JavaScript console for messages, because pressing ctrl+shift+j is just too much work.

License: MIT License

HTML 3.49% JavaScript 96.51%
javascript

jstinytest_dom_edition's Introduction

tinytest_dom_edition JavaScript unit testing library

Now featuring a dark theme & synchronous test execution via the await command.

This is an in-browser JavaScript library I've been using for years. It's so small and simple that it never occurred to me to open source it until I saw all the overly complicated alternatives that are out there.

If you're looking for a JavaScript library full of features or install guides that say things like grunt, npm or bower, you've come to the wrong place. Sorry, this probably isn't for you. Move along now.

If you're looking for a quick way to unit-test a JavaScript function/object in a web-page and don't want to get bogged down in frameworks, you've come to the right place. Take a seat... no scratch that, you'll have everything you need in a few seconds so you may as well remain standing.

10 second tutorial

Download tinytest_dom_edition.js and put it somewhere in your web directory.

Let's say you have a function in adder.js:

function add(a, b) {
  return a + b;
}

Create a test page called adder-test.html (you can name it anything). This includes your code under test, tinytest_dom_edition.js and defines your tests:

<script src="tinytest_dom_edition.js"></script>
<script src="adder.js"></script>
<script>
 tests({
  
  'synchronous test example': async function() {
    await new Promise(resolve => {
        setTimeout(function() { 
          resolve('resolved');
        }, 4000);
      })
    console.log('waited 4 seconds for this test to finish');
    //eq() should be outside the setTimeout/Promise
    eq(6, add(2, 4));
  },

   'adds numbers': function() {
     eq(6, add(2, 4));
     eq(6.4, add(2.4, 4));
   },

   'subtracts numbers': function() {
     eq(-2, add(2, -4));
   },

 });
</script>

Open the page in your browser. Green is good. Red is bad. With jstinytest_dom_edition there's no need to look in the JavaScript console for messages, because pressing ctrl+shift+j is just too much work.

That's it!

Don't believe me? Here's the source and result.

What else?

If your tests fail, you'll get stack traces:

Function reference

// Force a failure
fail(reason);

// Assert expression is truthy (fail with reason)
assert(expression, reason);

// Assert expected == actual
assertEquals(expected, actual)

// Assert expected === actual
assertStrictEquals(expected, actual)
eq(expected, actual) // Alias for assertStrictEquals

Errm that's it. Now stop wasting time - go test that function.

But, but, but. What about feature X?

It probably doesn't have it. If you need that, you'll probably find it in one of the many more sophisticated frameworks out there.

Projects using TinyTest

Other stuff

Justin Bourbonniere (jstinytest_dom_edition) Check out Justin's other GitHub projects.

Joe Walnes (jstinytest) Joe also has TinyTest for C that follows similar principles of simplicity.

Check out Joe's other GitHub projects and follow @joewalnes on that Twitter thing.

jstinytest_dom_edition's People

Contributors

justinbourb avatar joewalnes avatar

Watchers

James Cloos 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.