Giter Site home page Giter Site logo

qunit-once's Introduction

qunit-once

QUnit plugin that adds setupOne and teardownOnce to modules

Test page

NPM

Build status dependencies devdependencies

endorse

Available through bower and npm as qunit-once.

Tested in the browser by QUnit and under nodejs by qunit-node. Safe to use with gt, which includes setupOnce and teardownOnce already.

API

QUnit.module('Example', {
  setupOnce: function () {
    // runs once before anything else in the module
  },
  setup: function () {
    // runs before each unit test
  },
  teardown: function () {
    // runs after EACH unit test in this module
  },
  teardownOnce: function () {
    // runs once after all unit tests finished (including teardown)
  }
});

Use

install

Include qunit-once.js after qunit.js in HTML

<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="qunit-once.js"></script>
<script src="tests.js"></script>

Or install qunit-once via npm install qunit-once --save-dev under node.

Write unit tests

  // tests.js
  var counter = 0;

  QUnit.module('Example', {
    setupOnce: function () {
      counter = 1;
    },
    setup: function () {
      counter += 1;
    }
  });

  QUnit.test('first', function () {
    QUnit.equal(counter, 2, 'setupOnce followed by setup');
    counter = 0;
  });

  QUnit.test('second', function () {
    QUnit.equal(counter, 1, 'first test followed by setup');
  });

Run under node using node-qunit or gt

npm install -g qunit
qunit -d qunit-once.js -c tests.js -t tests.js

You don't need to include this module when running with gt, it already supports setupOnce and teardownOnce methods.

Related

Run QUnit module setup once, Testing code with async setup, qunit-promises

Small print

Author: Gleb Bahmutov © 2013 @bahmutov

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

qunit-once's People

Contributors

bahmutov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nrego

qunit-once's Issues

Describe usage

  • when used from QUnit test page
  • when using with node-qunit
  • when using with gt

teardownOnce is not once per module

Created a simple test and noticed that teardownOnce does not occur at the end of each QUnit module, instead only occurs when QUnit is finished. Sample code:

    QUnit.module("Module A", {
        setupOnce: function onSetupOnce() {
            console.log("Module A setupOnce");
        },
        teardownOnce: function onTeardownOnce() {
            console.log("Module A teardownOnce");
        }
    });

    QUnit.test("Module A Test 1", function () {
        QUnit.ok(true, "Assertion for Test 1");
    });

    QUnit.test("Module A Test 2", function () {
        QUnit.ok(true, "Assertion for Test 2");
    });

    QUnit.module("Module B", {
        setupOnce: function onSetupOnce() {
            console.log("Module B setupOnce");
        },
        teardownOnce: function onTeardownOnce() {
            console.log("Module B teardownOnce");
        }
    });

    QUnit.test("Module B Test 1", function () {
        QUnit.ok(true, "Assertion for Test 1");
    });

    QUnit.test("Module B Test 2", function () {
        QUnit.ok(true, "Assertion for Test 2");
    });

Console output:
Module A setupOnce
Module B setupOnce
Module B teardownOnce

I would expect Console output to be:
Module A setupOnce
Module A teardownOnce
Module B setupOnce
Module B teardownOnce

I am very interested in the concept and use of qunit-once if it works the way I thought and expected it to work. Please let me know if my thoughts are incorrect or if there is something that I am doing wrong that is preventing my expected output from happening. Thanks!

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.