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

rescript-test

v7.0.1

Published

> A lightweight test framework for ReScript

Downloads

7,301

Readme

ReScriptTest

A lightweight test framework for ReScript

Check out the documentation!

Installation

Run the following in your console:

$ yarn add --dev rescript-test

Then add rescript-test to your bsconfig.json's bs-dev-dependencies:

 {
   "bs-dev-dependencies": [
+    "rescript-test",
   ]
 }

Usage

$ # All tests
$ retest 'test/**/*.bs.js'
$ # Single file
$ retest 'test/MyTest.bs.js'

Testing with DOM

The test framework can simulate a browser using JSDOM, to activate it, simply add the --with-dom option.

$ # All tests
$ retest --with-dom 'test/**/*.bs.js'
$ # Single file
$ retest --with-dom 'test/MyTest.bs.js'

Basics

open Test

let intEqual = (~message=?, a: int, b: int) =>
  assertion(~message?, ~operator="intEqual", (a, b) => a === b, a, b)

let stringEqual = (~message=?, a: string, b: string) =>
  assertion(~message?, ~operator="stringEqual", (a, b) => a == b, a, b)

test("Equals", () => {
  let a = 1
  let b = 1
  intEqual(a, b)
})

let isCharCode = (a, b) => {
  a->String.charCodeAt(0) === b
}

test("Custom comparator", () => {
  let a = "a"

  assertion(~message="Char code should match", ~operator="isCharCode", isCharCode, a, 97.0)
  assertion(~message="Char code should match", ~operator="isCharCode", isCharCode, a, 98.0)
})

type user = {username: string, id: string}

let userEq = (a, b) => a.id === b.id
let userEqual = (~message=?, a: user, b: user) =>
  assertion(~message?, ~operator="userEqual", userEq, a, b)

test("DeepEquals", () => {
  let a = {username: "user", id: "a"}
  let b = {username: "user", id: "a"}
  stringEqual(a.username, b.username)
  userEqual(a, b)
})

Outputs:

1/4: Equals
  PASS - No message
2/4: Custom comparator
  PASS - Char code should match
  FAIL - Char code should match
    ---
    operator: isCharCode
    left:  a
    right: 98
    ...
3/4: DeepEquals
  PASS - No message
  PASS - No message
4/4: Async
  PASS - No message

# Ran 4 tests (6 assertions)
# 3 passed
# 1 failed

API

Tests

  • test(name, body)
  • createTestWith(~setup: unit => 'a, ~teardown: 'a => unit=? -> test
  • testAsync(name, body)
  • createTestAsyncWith(~setup: unit => 'a, ~teardown: 'a => unit=?, name, body: ('a, cb) => unit) -> testAsync

Operators

  • throws(func, ~message: string=?, ~test: exn => bool=?)
  • doesNotThrow(func, ~message: string=?)
  • assertion(comparator, a, b, ~operator: string=?, ~message: string=?)
  • pass()
  • fail()
  • todo(string)

Creating assertion shorthands

let stringMapEqual = (~message=?, a, b) =>
  assertion(
    ~message?,
    ~operator="stringMapEqual",
    (a, b) => Belt.Map.String.eq(a, b, (a, b) => a === b),
    a,
    b,
  )

Those that be useful to transition from an existing testing library, but we do not recommend polymorphic checks.

let equal = (~message=?, a, b) => assertion(~message?, ~operator="equal", (a, b) => a === b, a, b)

let deepEqual = (~message=?, a, b) =>
assertion(~message?, ~operator="deepEqual", (a, b) => a == b, a, b)

Create tests with setup and teardown

The setup function returns a clean context for the test, the teardown function resets it.

let testWithRef = createTestWith(~setup=() => ref(0), ~teardown=someRef => someRef := 0)

testWithRef("Setup & teardown", someRef => {
  incr(someRef)
  equal(someRef.contents, 1)
})

If you want to test with React, you can add the following helper as your test utility file:

@bs.val external window: {..} = "window"
@bs.send external remove: Dom.element => unit = "remove"

let createContainer = () => {
  let containerElement: Dom.element = window["document"]["createElement"]("div")
  let _ = window["document"]["body"]["appendChild"](containerElement)
  containerElement
}

let cleanupContainer = container => {
  ReactDOM.unmountComponentAtNode(container)
  remove(container)
}

let testWithReact = createTestWith(~setup=createContainer, ~teardown=cleanupContainer)
let testAsyncWithReact = createTestAsyncWith(~setup=createContainer, ~teardown=cleanupContainer)

And then use it:

testWithReact("Can render", container => {
  act(() =>
    ReactDOMRe.render(
      <div> {"hello"->React.string} </div>,
      container,
    )
  )

  let div = container->DOM.findBySelectorAndTextContent("div", "hello")
  isTrue(div->Option.isSome)
})

Async assertion count plan

testAsync("My test", (cb) => (
  // your tests
  cb(~planned=2, ())
))

Env file

Add a retest.env.js to your project root directory for any setup.