@basd/testr

0.1.6 • Public • Published

Testr

gitr done, testr first

npm pipeline license downloads

Gitlab Github Twitter Discord

Testr is a flexible, lightweight and based testing library for JS. It's designed to work seamlessly in both browser and Node.js environments. It supports a variety of features such as nesting, asynchronous execution, and exclusion/inclusion of test cases to streamline your testing workflow.

Installation

To install Testr, use npm:

npm i -D @basd/testr

Usage

Basic Usage

Here is a simple example of using Testr:

const Testr = require('@basd/testr')

const testr = new Testr()

testr.describe('A test suite', () => {
  testr.it('A test case', () => {
    if (1 + 1 !== 2) {
      throw new Error('Math is broken!')
    }
    expect(2 + 2).to.equal(4)
  })
})

testr.run()

Or you can instantiate and destructure the functions you need:

const { describe, it, before, after, expect, assert, run, testr } = new Testr()
// or as a function
const { describe, it, before, after, expect, assert, run, testr } = Testr()
// which means you can do this:
const { describe, it, before, after, expect, assert, run, testr } = require('@basd/testr')()

In this example, we create a test suite with describe(), add a test case with it(), and run our tests with run().

Skipping and Only Running Certain Tests

You can also mark test cases or suites to be skipped with describe.omit() and it.omit(), or to be the only ones run with describe.only() and it.only().

describe.omit('A skipped test suite', () => {
  it('A skipped test case', () => {})
})

describe.only('A test suite to run alone', () => {
  it.only('A test case to run alone', () => {})
})

Before and After Hooks

You can use before.each(), after.each(), before.all(), and after.all() to set up and tear down for tests:

describe('A test suite', () => {
  let counter = 0
  
  before.each(() => {
    counter = 1
  })
  
  it('A test case', () => {
    if (counter !== 1) {
      throw new Error('Counter not initialized!')
    }
  })
  
  after.each(() => {
    counter = 0
  })
})

Node.js Environment

For Node.js environments, TestrNode provides additional functionality:

const TestrNode = require('@basd/testr')

TestrNode.explode('path/to/tests', ['path/to/tests/to/ignore'])

In this example, TestrNode will automatically find and require test files in the specified directory that end with .test.js, and ignore the ones in the directories to ignore.

CLI Usage

If you have installed Testr globally or in your project, you can use the testr command to run your tests from the command line:

# Run all test files in a directory
testr path/to/tests

# Run all test files, except those in certain directories (coming soon..)
testr path/to/tests --ignore path/to/tests/to/ignore

This will automatically find and run any files ending with .test.js.

Contributing

Contributions are welcome! Please open an issue if you encounter any problems, or a pull request if you make a change.

Donations

If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!

Bitcoin (BTC):

1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF

Monero (XMR):

46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ

License

@basd/testr is MIT licensed.

Package Sidebar

Install

npm i @basd/testr

Weekly Downloads

4

Version

0.1.6

License

MIT

Unpacked Size

22.3 kB

Total Files

9

Last publish

Collaborators

  • basedwon