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

@dialo/inline-test-ppx

v0.0.1-beta.3

Published

A PPX to write inline tests in rescript

Downloads

58

Readme

inline-test-ppx

Actions Status NPM Version

A PPX to write inline tests in ReScript.

// Math.res
let sum = (a, b) => {
  a + b
}

@inline_test
test("adds 1 + 2 to equal 3", () => {
  expect(sum(1, 2)).toBe(3);
});

The test will be erased in compilation phase for production builds.

Installation

We provide an NPM package with prebuilt binaries.

yarn add @dialo/inline-test-ppx
# Or
npm install --save @dialo/inline-test-ppx

And add the PPX in your bsconfig.json file:

{
  "ppx-flags": [
    "ppx-flags": ["inline-test-ppx"]
  ]
}

Usage

  1. Install the testing framework of your choice (like jest, zora, rescript-test or other)
  2. Write some inline tests:
    // Math.res
    let sum = (a, b) => {
      a + b
    }
       
    @inline_test
    Jest.test("adds 1 + 2 to equal 3", () => {
      expect(sum(1, 2)).toBe(3);
    });

    Note: our PPX assumes that the testing framework is designed similarly to jest or zora. I.e. that tests are simply defined as top level expressions in arbitrary files. At the time of writing this readme we are not aware of testing frameworks for rescript that are designed differently.

  3. Make test runner aware of the tests. All tests are indirectly accessible through lib/bs/__inline_test.*:

    Jest

    In your jest config
    testMatch: [
      "lib/bs/__inline_test.*.cjs", // use .mjs if you have es6 output configured in bsconfig
      // other test locations ...
    ]

    Zora (with pta):

    // scripts in package.json
    "test": "pta 'lib/bs/__inline_test.*.cjs'" // add other locations as you see fit

    Others

    Other frameworks usually have similar configurations which allow you to point to the location of JavaScript files containing case. In the case of our ppx all tests are referenced by artifacts located in lib/bs/ prefixed with __inline_test.
  4. Compile your sources with INLINE_TEST=1 to compile the tests
    // scripts in package.json
    "watch": "INLINE_TEST=1 rescript build  -with-deps -w"

    Caveat

    Beaware that now all of the modules which contain inline tests will have inline tests present in .js artifacts. If you run:
    INLINE_TEST=0 yarn rescript build
    your modules will not be recompiled. In order to get rid of the testing code in js files clean the project first using:
    yarn rescript clean
    and then recompile them with the INLINE_TEST environment variable simply undefined or defined as 0 or false.

How it works

The PPX compiles any top level expression in a module conditionally based on the existence of INLINE_TEST env variable in the build environment. It means that:

@inline_test
Js.Console.log("hello")

will be only compiled by rescript if the variable is present. Otherwise it's simply ommited and not present in the resulting .js file.

Contributing

Take a look at our Contributing Guide.