0.0.5 • Published 3 years ago

simut v0.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Simple-unit-test (simut)

To make super simple unit test on typescript.

Appendix

The idea was to make it as simple as possible to make some unit test in typescript or javascript. Was more thinking of using this for simple things like experiments where you need basic test to go faster or iterview tests, exercises like advent of code, etc

Tools like jest/tape are really complete and offer a lot but always require some setup (ts-node, babel, ...). I wanted something super easy to start with.

I usually use assert from node to do the tests but any testing library that throw on error should work.

To run the test i simple use esbuild-runner repo

esr tests/index.ts

Right now i only have one file, i'm looking into making this work for multiple files.

It's mostly a experiment and me trying to kill time while i'm looking for a job.

If you need production level testing, please use Jest, Mocha, Tape or your favorite battle tested test runner.

screnshot

Installation

yarn add simut

Peer dependencies

yarn add -D esbuild esbuild-runner

Usage/Examples

import fetch from 'node-fetch'
import { strict as assert } from 'assert'

import { test, run, group } from '../src'
import { lonelyNumber } from '../src/example'

group('Simple')

test('should return product', async () => assert.equal(lonelyNumber(1, 2, 3), 6, 'should return the product'))
test('should return the lonely number', async () => assert.equal(lonelyNumber(1, 1, 3), 3, 'should return the lonely number'))

group('Async')

test('should return the lonely number', async () =>
	new Promise((r, re) => {
		setTimeout(() => {
			try {
				assert.equal(lonelyNumber(1, 1, 4), 4, 'should return the lonely number')
				r()
			} catch (e) {
				re(e)
			}
		}, 1000)
	}))
test('should return product', async () => assert.equal(lonelyNumber(1, 2, 3), 6, 'should return the product'))

group('After Async')

test('should return 1', async () => assert.equal(lonelyNumber(3, 3, 3), 1, 'should return 1'))
test('should return the lonely number', async () => assert.equal(lonelyNumber(3, 1, 3), 1, 'should return the lonely number'))
test('should return the lonely number', async () => assert.equal(lonelyNumber(3, 1, 3), 3, 'should return the lonely number'))

group('Fetch first')

test('should return 1', async () =>
	fetch('https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1').then(() =>
		assert.equal(lonelyNumber(3, 3, 3), 1, 'should return 1')
	))

run()

API Reference

test

test('test name', async () => 'assertion returning a promise or throwing or error')
ParameterTypeDescription
titlestringRequired. Description of the test
fnfunctionRequired. function throwing of test error and returning a promise

Create a new test

only

only('test name', async () => 'assertion returning a promise or throwing or error')
ParameterTypeDescription
titlestringRequired. Description of the test
fnfunctionRequired. function throwing of test error and returning a promise

Create a new test and will filter all the other tests except the one using only.

useful if you want to focus on one test. Remember to switch back to test once you're done to run all of them again.

group

group('Group title')
ParameterTypeDescription
titlestringRequired. Name of you group

Create a group

run

run()

Run the tests and print the output in terminal

License

MIT

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago