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

@michaelhelvey/microtest

v2.1.2

Published

Simple fetch-based integration testing library for Node.js

Downloads

112

Readme

microtest

build npm version License: MIT

microtest is a simple API integration testing library for Node.js

microtest encourages you to test your APIs the same way you consume them, increasing your confidence in your tests while decreasing the number of lines of code you need to write.

It works with all frameworks, all test runners, and has a a simple, easy-to-read codebase with very few dependencies.

Quickstart

Installation

pnpm add -D @michaelhelvey/microtest

Or use the package manager of your choice.

Usage

// Import your application.  To use microtest, you just need to point it at a
// server, so do whatever you need to get that started.  For this example, we'll
// assume you're testing an express application.
import { app } from '../app'
import http from 'node:http'
import { microtest } from '@michaelhelvey/microtest'

/*
 * Start your application server, using whatever mechanisms your server
 * framework and your test runner provide:
 */

let server: http.Server

beforeAll(async () => {
	return new Promise((resolve) => {
		server = app.listen(9999, resolve)
	})
})

afterAll(async () => {
	server.close()
})

/**
 * Use microtest to make requests to your API and parse the responses:
 */

const request = microtest('http://localhost:9999')

test('my api integration test', async () => {
	// In this example, we make a request to the /foo endpoint with a JSON payload
	const response = await request((ctx) => ctx.post('/foo').json({ a: 'b' }))
		// then assert that the response has a 200 status:
		.status(200)
		// and assert + decode the response as json
		.json<{ message: string }>()

	// then we can use our testing framework to make further assertions
	expect(response.message).toEqual('the message')
})

While this simple example, along with typescript autocomplete, is probably enough to get started, for more information, see the full API documentation.

Re-creating the application on each test

Occassionally, your server will be constructed in such a way that you want to create, listen, and then tear down the server on every request, rather than starting and stopping your server in before & after each hooks. To accomodate this use-case, microtest provides the withApp higher order function:

test('my api integration test', async () => {
	// This single change will cause microtest to start and stop your server on
	// a random port for each request.
	const request = withApp(app)({
		/* normal microtest configuration args */
	})

	// The rest of your test is identical:
	const response = await request((ctx) => ctx.post('/foo').json({ a: 'b' }))
		.status(200)
		.json<{ message: string }>()

	// by this point in your test, the server will have been stopped
	expect(response.message).toEqual('the message')
})

Contributing

Contributions through pull requests are welcome. If you make a pull request, feel free to add yourself to the Contributors section below. Thank you!

Local Development

  • pnpm test (or pnpm test:coverage) for running unit tests
  • pnpm build to build the library for publishing
  • pnpm publish to publish a new version (if you have permissions)

Contributors

License

MIT