testnow

Type-checked tests. Easy to write, fast to execute.

npm install --save-dev @click-click/testnow

Example

Assuming this code in file ts/sources/pathname.ts:

export function normalizePathname(pathname: string): string {
  // => no backslash in pathname, only slashes
  return pathname.replace(/\\/gu, '/')
}

You can define your tests in ts/tests/pathname/normalizePathname.ts like this:

import { normalizePathname } from "../../sources/pathname"

$(normalizePathname, '').equals('')
$(normalizePathname, 'foo\\').equals('foo/')
$(normalizePathname, 'foo\\bar').equals('foo/bar')
$(normalizePathname, 'foo\\bar\\').equals('foo/bar')
/*-------------------------------  ------ ---------
            ^                         ^       ^
        call specification          check     expected value
*/

Further techniques

There is no more API. Refer to the HOW-TO for advanced topics:

  1. registration of test cases
  2. proxy functions
  3. interactive object

The testnow may seem limited but it is actually as powerful as regular programming since it is only about registering how to call a function.

I would recommend though to avoid being too sophisticated with your testing utility code. Long is fine —even expected— because of all the logistics such code has to manage. Nothing forces it to be convoluted nor abstract so keep it simple. The last thing you want is to have to debug your test code itself!

Limitations

There are no particular ways to mock functions nor to test classes or time-dependent code. I found out that I was just fine with the above approach but you may want a test framework that provides more. I would recommend Jest.