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.
Simple asynchronous testing framework. Never again count your asserts! This tiny testing library is fully callback-based and has a rich API for asserts.
Simple async testing library for node.js. Better suited to asynchronous tests than other libraries since it uses callbacks to get results.
Now shows results in pretty colors!
Just run: $ npm install testing
Or add package testing to your package.json dependencies.
Version 3 is an ES6 module, and should be used at least with Node.js v16 or later. Versions 2 should be used at least with Node.js v8 or later.
Add asynchronous testing to your code very easily. Import the library:
import testing from 'testing';
Add a test function to your code, checking if results are what should be expected:
function testAdd(callback)
{
testing.assertEquals(add(1, 1), 2, 'Maths fail', callback);
testing.success(callback);
}
Run an async test to read the contents of a file and check it is not empty:
async function testAsync() {
const result = await fs.promises.readFile('file.txt')
testing.assert(result, 'Empty file', callback)
}
Or, with callbacks:
function testAsync(callback)
{
fs.readFile('file.txt', function(error, result)
{
if (error)
{
testing.failure('File not read', callback);
}
testing.assert(result, 'Empty file', callback);
testing.success(callback);
});
}
Pass an array to testing.run()
to run all tests sequentially:
testing.run([
testAdd,
testAsync,
], callback);
Usually tests are stored in a separate file.
Each test file will export its tests as a function,
e.g. testAdd()
.
An aggregate file will run all package tests directly:
import testing from 'testing'
import testAdd from './testAdd.js'
import testAsync from './testAsync.js'
/**
* Run package tests.
*/
function test(callback) {
var tests = [
testAdd,
testAsync,
];
testing.run(tests, callback);
};
test(testing.show);
Tests are run every time this aggregate file is invoked directly:
$ node test/all.js
Implementation is very easy, based around three functions.
Callbacks are used for asynchronous testing. They follow the usual node.js convention:
callback(error, result);
When no callback is passed, synchronous testing is performed.
Note success for the current test. An optional message is shown if there is no callback.
If there is a callback, then it is called with the message. Default message: true.
Example:
testing.success(callback);
Note failure for the current test.
If the callback is present, calls the callback with the error:
callback(message);
Otherwise the message is shown using console.error(). Default message: 'Error'.
Example:
testing.failure('An error happened', callback);
Alias to testing.failure()
.
Run a set of tests. The first parameter is an object containing one attribute for every testing function.
The tests are considered as a failure when a certain configurable timeout has passed. The timeout parameter is in milliseconds. The default is 2 seconds per test.
When the optional callback is given, it is called after a failure or the success of all tests. The callback has this signature, following the usual Node.js syntax:
function (error, result) {
...
}
error
will contain the results of the tests when they fail.
result
will contain the results when they succeed.
Example:
testing.run({
first: testFirst,
second: testSecond,
}, 1000, callback);
For each attribute, the key is used to display success; the value is a testing function that accepts an optional callback.
Note: testing uses async to run tests in series.
There are several utility methods for assertions.
Checks condition; if true, does nothing. Otherwise calls the callback passing the message, if present.
When there is no callback, just prints the message to console.log() for success, console.error() for errors. Default message: 'Assertion error'.
Example:
testing.verify(shouldBeTrue(), 'shouldBeTrue() should return a truthy value', callback);
Check that the given values are equal. Uses weak equality (==).
Message and callback behave just like above.
Example:
testing.equals(getOnePlusOne(), 2, 'getOnePlusOne() does not work', callback);
Inverse of the above, check that the given values are not equal. Uses weak inequality (!=).
Check that the container contains the piece. Works for strings and arrays.
Message and callback behave just like above.
Example:
testing.contains('Big string', 'g s', 'Does not contain', callback);
Check there are no errors. Almost the exact opposite of an assertion: if there is an error, count as a failure. Otherwise, do nothing.
Example:
testing.check(error, 'There should be no errors', callback);
Similar to over the following code:
testing.assert(!error, 'There should be no errors', callback);
But with the advantage that it shows the actual error message should there be one.
You can use your own function to show results. The library provides a premade callback:
Show an error if present, a success if there was no error.
Example:
testing.run(tests, testing.show);
This line can be run at the end of every code file to run its set of tests.
Like testing.show()
, but shows the complete hierarchical tree of tests.
Test information is therefore duplicated: once shown while running,
another after all tests.
Example:
exports.test(testing.showComplete);
Returns a function with a callback as parameter, that runs the tests, shows results and invokes the callback. Useful to insert in some callback loop.
Example:
var tasks = [testing.toShow(test1), testing.toShow(test2)];
async.parallel(tasks, function()
{
console.log('All tests run');
});
Runs a couple of tests in parallel, showing their results as they finish.
This library is tested using itself, check it out! https://github.com/alexfernandez/testing/blob/master/test.js
(The MIT License)
Copyright (c) 2013-2023 Alex Fernández alexfernandeznpm@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Simple asynchronous testing framework. Never again count your asserts! This tiny testing library is fully callback-based and has a rich API for asserts.
The npm package testing receives a total of 19,769 weekly downloads. As such, testing popularity was classified as popular.
We found that testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.