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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@midnight-ntwrk/midnight-js-testing

v2.0.2

Published

Testing utilities for MidnightJS and beyond

Readme

What is this?

A comprehensive testing solution for your Midnight.JS applications, providing seamless integration with various test environments.

This package was created for the Midnight network.

Please visit the Midnight Developer Hub to learn more.

Use only in Midnight test environments

Image exclusively for Midnight test environments use.

Agree to Terms

By downloading and using this image, you agree to Midnight’s Terms and Conditions, which includes the Privacy Policy.

License

The software provided herein is licensed under the Apache License V2.0.

Table of Contents

  1. Installation
  2. Getting Started
  3. Features
  4. Limitations
  5. Examples of Usage

Installation

Before using the testing library, ensure you have the following prerequisites:

  1. Node.js and yarn installed
  2. Docker Desktop (optional for local dockerized tests)
  3. Midnight.JS project set up

Install the testing library by running the following command in your terminal:

yarn add -D @midnight-ntwrk/midnight-js-testing

Getting Started

To use the testing library, create a new file called midnight.test.js in the __tests__ directory of your project. In this file, import the necessary dependencies:

import { getTestEnvironment } from '@midnight-ntwrk/midnight-js-testing';

beforeAll(async () => {
  testEnvironment = getTestEnvironment(logger);
  environmentConfiguration = await testEnvironment.start();
  walletProvider = await testEnvironment.getMidnightWalletProvider();
});

afterAll(async () => {
  await testEnvironment.shutdown();
});

Features

Set of functions that simplify testing of DApps in Midnight

  • [x] Standard Dockerized environment setup - both live and local
  • [x] Predefined Midnight environments configuration
  • [x] Proof server control
  • [x] Wallets management
  • [x] Fund wallets
  • [x] Check system health

Environment variables

Environment selection:

  • MN_TEST_ENVIRONMENT controls the environment to be used for testing. It can take one of these values:
    • undeployed
    • devnet
    • testnet
    • env-var-remote

If MN_TEST_ENVIRONMENT is not set, the default value will be undeployed. If MN_TEST_ENVIRONMENT is set to undeployed, the testing environment will be deployed locally using Docker. If MN_TEST_ENVIRONMENT is set to devnet,testnet, env-var-remote the testing environment will the corresponding live network, with proof server setup using predefined NETWORK_ID. If MN_TEST_ENVIRONMENT is set to env-var-remote, below environment variables must be set:

  • MN_TEST_NETWORK_ID - Proof server NETWORK_ID
  • MN_TEST_INDEXER - Indexer URL
  • MN_TEST_INDEXER_WS - Indexer WebSocket URL
  • MN_TEST_NODE - Node URL
  • MN_TEST_FAUCET - Faucet URL

Wallet setup:

  • MN_TEST_WALLET_SEED can be used to set a specific seed phrase for the wallet. If not set, a random seed phrase will be used.

Limitations

  • Localnet wallets limit count is 4

Examples of Usage

1. Selecting Different Environments

You can control the test environment using the MN_TEST_ENVIRONMENT environment variable. Here's how you can set it:

const testEnvironment = getTestEnvironment(logger);
environmentConfiguration = await testEnvironment.start();
# Example: Set the environment variable before initializing the test environment
MN_TEST_ENVIRONMENT='devnet'; yarn test

This allows you to easily switch between predefined environments like devnet, testnet, and others. Default (undefined) value is undeployed which will deploy the testing environment locally using Docker.


2. Creating and Managing Wallets

Here's an example of creating wallets in your test environment:

// Example: Create multiple wallets in a test environment
const testEnvironment = getTestEnvironment(logger);
await testEnvironment.start();

// Create 2 wallets
const wallets = await testEnvironment.startMidnightWalletProviders(2);

// Verify wallet properties
expect(wallets).toHaveLength(2);
wallets.forEach(async (wallet) => {
  expect(wallet.coinPublicKey).not.toBeUndefined();
});

// Shutdown the environment after testing
await testEnvironment.shutdown();

This demonstrates how to create wallets and verify their properties, such as the coinPublicKey.


3. Setting Wallet Seeds

If you need to use specific wallet seeds for testing, you can do the following:

// Example: Create a wallet with a predefined seed phrase
const wallet = await testEnvironment.getMidnightWalletProvider();

expect(wallet.coinPublicKey).not.toBeUndefined();
# Example: Set the environment variable before initializing the test environment
MN_TEST_WALLET_SEED='00000000000000000000000000000042'; yarn test

This allows you to test specific scenarios using known wallet seeds.


4. Handling Environment Configuration

You can also customize the test environment configuration by modifying the defaultContainersConfiguration object:

import {
  defaultContainersConfiguration,
  getContainersConfiguration,
  setContainersConfiguration
} from '../configuration';

// Example: Modify default environment configuration before starting
const config: ContainersConfiguration = {
  ...defaultContainersConfiguration,
  proofServer: {
    ...defaultContainersConfiguration.proofServer,
    fileName: 'proof-server.yml'
  }
};
setContainersConfiguration(config);

This gives developers flexibility in configuring the test environment according to their needs.


5. Error Handling

Here's an example of handling errors when setting up wallets:

// Example: Test for maximum wallet limit exceeded
process.env.MN_TEST_ENVIRONMENT = undefined; // Use local environment
const testEnvironment = getTestEnvironment(logger);
await testEnvironment.start();

try {
  await testEnvironment.startMidnightWalletProviders(5); // Assuming max is 4
} catch (error) {
  expect(error.message).toContain('Maximum supported number of wallets for this environment reached');
}

await testEnvironment.shutdown();

This demonstrates how to handle cases where the wallet limit is exceeded.


6. Advanced Usage with Proof Server

Here's an example of integrating with the proof server:

// Example: Start a proof server with networkd ID = testnet and ID = 123
const proofServer = await DynamicProofServerContainer.start(logger, '123', 'testnet');

//stop the proof server
await proofServer.stop();

This shows how to integrate with and customize the proof server for testing.


7. Customized test environments

Library is provided with set of predefined environment configurations i.e.:

  • LocalTestEnvironment
  • Testnet2TestEnvironment

By using getTestEnvironment(logger); based on environment variable MN_TEST_ENVIRONMENT test environment configuration is provided. However, you can either create your own class defining the environment endpoints or use below enviroment variables.

Here's an example of fully customized endpoints of the test environment, that you can provide using environment variables:

MN_TEST_ENVIRONMENT="env-var-remote" \
MN_TEST_NETWORK_ID="undeployed" \
MN_TEST_INDEXER="http://localhost:3085/api/" \
MN_TEST_INDEXER_WS="ws://localhost:3085/ws/" \
MN_TEST_NODE="http://localhost:3086" \
yarn test

8. System health check before tests

For the remote test environemnts (testnet-02, ...) simple health check is performed for each of the components to check their state before test.