npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jt_test

v1.2.0

Published

Jeremythings testing library

Downloads

10

Readme

jt_test

Simple test library

Install

npm install jt_test

To use the command line tool jttest then install the package globally

npm install -g jt_test

Use just requiring the package:

Create a test file similar to below e.g. db_tests.js and simply run with node db_tests

const {
    addTwoNumbers,
} = require("samplemod")

const {
    addTest,
    testIsEqual,
    runTests,
} = require("jt_test")

/*
 * testIsEqual
 *
 */

addTest('add two numbers',
    (testkey) => {
        const result = addTwoNumbers(2, 4)
        testIsEqual(testkey, result, 6)
    }
)

runTests()

You should see output:

add two numbers - fail: 0 pass: 1

Tests 1 Failed 0 Passed 1

Using the command line tool

If you install the package globally you can use the command line tool jttest to execute all of the test files it can find

It will look for a config file jttest.json in a folder .jttest either in the folder the command is executed in or in the home folder where it will determine where your tests are and the prefix used to identify the tests etc.

Options with defaults are:

{
  "prefix": "jttest_",       
  "location": "./jttests",
  "ignore": [],  
  "only": [],         
  "premessage": "Test Starting",  
  "prescript": "",
  "postmessage": "Tests Completed",
  "postscript": ""
}
  • prefix
    • All tests must start with this prefix
  • location
    • The folder where tests are
  • ignore
    • An array of test names not to be executed
  • only
    • Only run the tests in this array (overrides the ignore option)
  • premessage
    • A message displayed at the start of the tests
  • prescript
    • A script to run before the tests run (see note below)
  • postmessage
    • A message to be displayed after the tests have all run
  • postscript
    • A script to run after all tests have ran (see note below)

The ignore option is useful if you want to leave some tests out until the code you are testing is ready.

The only option is used when you only want to test one or more tests specifically, the only option overrides any ignore option values.

The pre/postscripts are useful for setting up tests and also doing additional testing, for example in the development of this module itself I need to test both passed and failed tests and so in my tests I wrote the output to a log for each test expected result and then when all of the tests had finished, I processed the log to match against the actual results.

Example jttest.json

{
  "prefix": "jttest_",
  "location": "./jttests",
  "ignore": [
    "jttest_notready1",
    "jttest_notready2"
    ],
  "only": [
    ],
  "prescript": "./.jttest/prescript.js",
  "postscript": "./.jttest/postscript.js",
  "postmessage": "You should see the following results:"

}

Note: you do not need to specify .js extension on the pre/postscripts as this is assumed but you can if you wish

Methods

Important

In some of the methods below there is use of testkey parameter, this is a reference to the test so that any failures can be tracked by the process. testkey is passed in by the process of running the test e.g.

addTest('add two numbers',
    (testkey) => {
        const result = addTwoNumbers(2, 4)
        testIsEqual(testkey, result, 6) 
    }
)

Control methods

  • addTest addTest(description, testfunction) Used to add tests to be executed. The description is displayed to identify the tests and the testfunction is the testcode to be executed.
  • runTests runTests() Run the tests added

Tests

  • testFail testFail(testkey, reason) Forces a fail result, giving the reason
  • testPass testPass(testkey) Forces a pass result
  • testIsTrue testIsTrue(testkey, value, reason) tests the value to be true, giving the reason if the test fails as not true
  • testIsFalse testIsFalse(testkey, value, reason) tests the value to be false, giving the reason if the test fails as not false
  • testIsEqual testIsEqual(testkey, value1, value2, reason) tests value1 equal to value2, failing with the reason if they are not equal
  • testNotEqual testNotEqual(testkey, value1, value2, reason) tests value1 not equal to value2, failing with the reason if they are equal
  • testIsInteger testIsInteger(testkey, value, reason) test the value is a valid integer, failing with the reason if not an integer
  • testIsString testIsString(testkey, value, reason) test the value is a valid string, failing with the reason if not an string

Utility methods

  • makeString makeString(length) makes a string of random characters of length
  • makeInteger makeInteger = (max, min) makes an integer between the max and min, if min is left out then it defaults to 0