Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
QUnit is a powerful, easy-to-use JavaScript unit testing framework. It is used to test any generic JavaScript code, including code running in the browser or in Node.js. QUnit is especially useful for testing jQuery projects.
Basic Test
This feature allows you to write a basic test case. The `QUnit.test` function defines a test with a name and a callback function. The `assert.ok` method checks if the given expression is true.
QUnit.test('hello test', function(assert) {
assert.ok(1 == '1', 'Passed!');
});
Asynchronous Testing
QUnit supports asynchronous testing. You can use async/await to handle asynchronous operations within your tests. The test will wait for the promise to resolve before making assertions.
QUnit.test('asynchronous test: async and await', async function(assert) {
const result = await new Promise(resolve => setTimeout(() => resolve('done'), 1000));
assert.equal(result, 'done', 'Passed!');
});
Module Grouping
QUnit allows you to group related tests using `QUnit.module`. You can also define setup and teardown logic using hooks like `beforeEach` and `afterEach`.
QUnit.module('group a', function(hooks) {
hooks.beforeEach(function(assert) {
assert.ok(true, 'beforeEach called');
});
QUnit.test('a basic test example', function(assert) {
assert.ok(true, 'this test is fine');
});
});
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Compared to QUnit, Mocha is more flexible and can be paired with various assertion libraries like Chai.
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works out of the box for most JavaScript projects and provides a rich API for writing tests. Jest includes built-in assertion libraries and mocking capabilities, making it a more integrated solution compared to QUnit.
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks and does not require a DOM. Jasmine is known for its easy-to-read syntax and is often compared to QUnit for its simplicity and ease of use.
http://github.com/jquery/qunit
Using Node Package Manager:
npm install qunit
From git:
git clone https://github.com/kof/node-qunit.git
git submodule init
git submodule update
// Add a test to run.
test( name, expected, test )
// Add an asynchronous test to run. The test must include a call to start().
asyncTest( name, expected, test )
// Specify how many assertions are expected to run within a test.
expect( amount );
// Separate tests into modules.
module( name, lifecycle )
// A boolean assertion, equivalent to JUnit's assertTrue. Passes if the first argument is truthy.
ok( state, message )
// A comparison assertion, equivalent to JUnit's assertEquals. Uses "==".
equals( actual, expected, message )
// A comparison assertion. Uses "===".
strictEqual( actual, expected, message )
// A deep recursive comparison assertion, working on primitive types, arrays and objects.
same( actual, expected, message )
// Assertion to test if a callback throws an exception when run.
raises( actual, message )
// Start running tests again after the testrunner was stopped.
start()
// Stop the testrunner to wait to async tests to run. Call start() to continue.
stop( timeout )
Some usage examples, read full cli api doc using "--help":
$ qunit -h
$ qunit -c ./code.js -t ./tests.js
$ qunit -c ./code.js -t ./tests.js -p /path/for/require /path1/for/require --cov false
By default, code and dependencies are added to the global scope. To specify requiring them into a namespace object, prefix the path or module name with the variable name to be used for the namespace object, followed by a colon:
$ qunit -c code:./code.js -d utils:utilmodule -r ./time.js
var testrunner = require( "qunit" );
Defaults:
{
errorsOnly: false, // set it to true if you want to report only errors
errorStack: true, // set it to false if you want to get error stack in report
summary: true, // print a summary about all tested stuff after finish
coverage: true, // display coverage
paths: null // add paths to require of test environment
}
// to change any option - change it :)
testrunner.options.optionName = value;
// one code and tests file
testrunner.run({
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
});
// require code into a namespace object, rather than globally
testrunner.run({
code: {path: "/path/to/your/code.js", namespace: "code"},
tests: "/path/to/your/tests.js"
});
// one code and multiple tests file
testrunner.run({
code: "/path/to/your/code.js",
tests: ["/path/to/your/tests.js", "/path/to/your/tests1.js"]
});
// array of code and test files
testrunner.run([
{
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
},
{
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
}
]);
// using testrunner callback
testrunner.run({
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
}, function( report ) {
console.dir(report);
});
// specify dependency
testrunner.run({
deps: "/path/to/your/dependency.js",
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
});
// dependencies can be modules or files
testrunner.run({
deps: "modulename",
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
});
// dependencies can required into a namespace object
testrunner.run({
deps: {path: "utilmodule", namespace: "utils"},
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
});
// specify multiple dependencies
testrunner.run({
deps: ["/path/to/your/dependency1.js", "/path/to/your/dependency2.js"],
code: "/path/to/your/code.js",
tests: "/path/to/your/tests.js"
});
Use stderr if you want to debug something while running via qunit. console.error("This will not brake qunit report"); // or require("util").debug("This will not brake qunit report");
QUnit API and code which have to be tested are already loaded and attached to the global context.
Because nodejs modules reserved "module" namespace we have to redefine it from QUnit namespace.
module = QUnit.module;
Basically QUnit API can ba accessed directly from global object or optional via "QUnit" object.
QUnit.test;
Some tests examples
test("a basic test example", function() {
ok( true, "this test is fine" );
var value = "hello";
equals( "hello", value, "We expect value to be hello" );
});
module("Module A");
test("first test within module", 1, function() {
ok( true, "all pass" );
});
test("second test within module", 2, function() {
ok( true, "all pass" );
});
module("Module B", {
setup: function() {
// do some initial stuff before every test for this module
},
teardown: function() {
// do some stuff after every test for this module
}
});
test("some other test", function() {
expect(2);
equals( true, false, "failing test" );
equals( true, true, "passing test" );
});
module("Module C", {
setup: function() {
// setup a shared environment for each test
this.options = {test: 123};
}
});
test("this test is using shared environment", 1, function() {
same( {test:123}, this.options, "passing test" );
});
asyncTest("this is an async test example", 2, function() {
setTimeout(function() {
ok(true, "finished async test");
strictEqual( true, true, "Strict equal assertion uses ===" );
start();
}, 100);
});
$ make test
Using JSCoverage 0.3.1 (from the ubuntu universe repositories) resulted in a "jscoverage: unknown file type" error. Updating to the JSCoverage 0.5.1 from http://siliconforks.com/jscoverage/ resolves this issue.
FAQs
The powerful, easy-to-use testing framework.
We found that qunit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.