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

pineapple

v0.21.2

Published

Make your testing sweet!

Downloads

311

Readme


What is this?

npm version Node.js CI Coverage Status

Pineapple is a test framework designed to remove the cruft from writing unit tests and creating snapshots.

/**
 * Adds numbers together for the sake of demonstrating pineapple.
 * @test 1, 2 returns 3
 * @test '1', 2 throws "Not a number"
 * @param {number} a 
 * @param {number} b
 */
export function add(a, b) {
    if (typeof a !== 'number' || typeof b !== 'number') throw new Error('Not a number')
    return a + b
}

Pineapple allows you to embed a few example test-cases in your JSDocs, making it easier to focus on your code and less on defining it & expect chains.

Documentation

Visit our documentation here.

Highlights

Snapshots

When you omit conditions from your test cases, Pineapple will automatically capture the result of your test & snapshot it, making it easier to preserve expected behavior in your applications, and even easier for users to find examples on how to call your code.

Property Based / Fuzz Testing

By leveraging fast-check, Pineapple makes it simple to fuzz-test your functions with simple clauses.

This pairs extremely well with the snapshot testing for capturing a variety of test-cases that would've taken time to generate by hand.

/**
 * Computes the sum of numbers.
 * @test #array(#integer)
 */
export function sum(arr: number[]) {
    return arr.reduce((a,b) => a + b, 0)
}

Language-Idiomatic Test Code

Even in cases where you might need to write a more complex unit test, Pineapple encourages you to write normal JavaScript functions.

Continuous Test Runner

By enabling the -w or --watch-mode flag, Pineapple will watch your project for changes. Any changes to a file will kick off all tests that could be potentially impacted by your change.

And Much More

To Install

npm i pineapple --save-dev

or

yarn add pineapple --dev

Alternatively, you may install the runner globally (add a -g flag).

To Run

Usage: pineapple [options]

Options:
  -V, --version                  output the version number
  -i, --include <files...>       Comma separated globs of files to include.
  -e, --exclude <files...>       Comma separated globs of files to exclude.
  -w, --watch-mode               Will run tests only when a file is modified.
  -a, --accept-all               Accept all snapshots.
  -u, --update-all               Update all snapshots.
  -t, --transpile                Enables transpilation.
  --typescript                   Enables transpilation for TypeScript. (legacy flag)
  --timeout <milliseconds>       The timeout for each test. (default: "5000")
  --strict                       Enables additional checks to enforce better testing, namely validating that all snapshots are used.
  --clean                        Cleans up unused snapshots.
  --omit-snapshot-inputs         Omits input from being captured in snapshots for fuzz testing.
  --only <lines...>              Allows you to specify which tests you would like to run.
  --fuzz-runs <amount>           The number of runs that fuzz tests perform. (default: "100")
  --snapshot-fuzz-runs <amount>  The number of runs that fuzz tests perform on a snapshot. (default: "10")
  -f, --format <format>          The output format (choices: "json", "console", default: "console")
  --bun                          Uses Bun as the test runner.
  -h, --help                     display help for command

Example

pineapple -i src/**/*.js

Continuous Integration

If process.env.CI / the environment variable CI is set, it will automatically fail if a snapshot is not set or if the snapshot does not match.

Q&A

Does Pineapple support TypeScript?

Yes! Out of the box, Pineapple supports JavaScript, TypeScript, JSX & TSX.

It also supports both CommonJS and ES Modules, and a few other common module mechanisms.

By using -t or --transpile, you can enable the bundling functionality.

Does Pineapple support alternative builds / Babel?

Yes. If you're using a Flow or another JavaScript framework that might need some additional transpilation, Pineapple will allow you to register a custom transpiler with the @pineapple_transpile annotation.

Should I put pineapple on my pizza?

The author of this project holds no opinion on this subject, but you might want to consider that Pineapple doesn't necessarily go well with every use case, so for certain types of tests we actively encourage you to choose a framework better suited for that style of test.

Pineapple is oriented towards simplifying unit tests & making it easy to run multiple test cases against functions, but if you're trying to test a series of complex processes against something stateful (transactions against a rewards points system, combat, etc), scenario tests are likely your better bet.

Because of this, Pineapple does include a scenario testing framework similar to Cucumber.js, but if you're trying to implement end-to-end UI tests or integration tests, Newman or Playwright or Cypress might fit the bill better.