# @vaclav-purchart/js-test

> Simple, zero-dependency test framework inspired by mocha and jest.

Latest version **1.1.0** (published 2022-07-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @vaclav-purchart/js-test
pnpm add @vaclav-purchart/js-test
yarn add @vaclav-purchart/js-test
bun add @vaclav-purchart/js-test
```

Provides the command `js-test`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-07-07 |
| First published | 2022-07-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vaclav Purchart |
| Maintainers | vaclav-purchart |
| Keywords | javascript, tests, unit testing, mocha, jest |

## Links

- npm: https://www.npmjs.com/package/@vaclav-purchart/js-test
- Repository: https://github.com/vaclav-purchart/js-test
- Homepage: https://github.com/vaclav-purchart/js-test#readme
- Issues: https://github.com/vaclav-purchart/js-test/issues
- npm.io page: https://npm.io/package/@vaclav-purchart/js-test

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2022-07-07
- 1.0.0 — 2022-07-04

## README

# @vaclav-purchart/js-test
Simple, zero-dependency test framework inspired by mocha and jest.

Test framework which allows to run both synchronous and asynchronous tests and to structure them into test suites.

## Usage
### ES6 modules
```javascript
import { describe, test, assert } from '@vaclav-purchart/js-test'

describe('my test suite', () => {
	test('my async test', async () => {
		const response = await fetch('https://github.com')
		assert.equals(response.status, 200, 'should return HTTP 200 status')
	})
})
```

### CommonJS modules
```javascript
const { describe, test, assert } = require('@vaclav-purchart/js-test')

describe('my test suite', () => {
	test('my async test', async () => {
		const response = await fetch('https://github.com')
		assert.equals(response.status, 200, 'should return HTTP 200 status')
	})
})
```

## Test definition API

### Test suite definition
Test suite can be defined with `describe` (alias `suite`).
```javascript
describe(name, handler)
```
- **name** - string - text identifier of the suite

- **handler** - function - sync or async function to init/define the test suite

### Test case definition
Test case can be defined with `test` (alias `it`).
```javascript
test(name, handler)
```
- **name** - string - text identifier of the suite

- **handler** - function - sync or async function to init/define the test suite

## Library API
- `setReporter` - function(string) - to print test results (default `console.log`)
- `globalBefore` - function(handler) - sync/async handler to perform action before all tests/suites
- `globalAfter` - function(handler) - sync/async handler to perform action after all tests/suites have ended

> Example:
```javascript
import {setReporter, globalBefore, globalAfter} from '@vaclav-purchart/js-test'

setReporter(sendTestResults)

globalBefore(async () => {
	await setupDbConnection()
})

globalAfter(async () => {
	await teardownDbConnection()
})

// ... test definitions
```

## Assertion library
`js-test` contains very simple assertion library (subset of chai.assert).
- `equal` - (realValue, expectedValue, [failedMessage]) - compares given values by strict equality check (`===`).
- `deepEqual` - (realValue, expectedValue, [failedMessage]) - deeply compares all nested values and all have to match. It is not able to compare cyclic objects.
- `isTrue` - (expression, [failedMessage]) - compares if the given expression has truthy value (`Boolean(value) === true`)
- `isFalse` - (expression, [failedMessage]) - compares if the given expression has falsy value (`Boolean(value) === false`)
- `fail` - ([failedMessage]) - will always fail

## CLI interface
It is possible to run multiple test files by following command:

```
js-test [my-test-cjs-file.js] [my-test-es6-file.mjs]
```
(Mixing of CommonJS and ES6 modules is supported.)

# TODO
- make reporters better (info, error, etc.)
- implement async timeouts

---
_Source: https://npm.io/package/@vaclav-purchart/js-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
