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

rest-test2

v1.0.5

Published

Rest test is an automated tool to help test an API in the cli. It as been made as an alternative to postman to simplify git workflow and assertions/readability

Downloads

360

Readme

=====================================================
    ____  _________________   _______________________
   / __ \/ ____/ ___/_  __/  /_  __/ ____/ ___/_  __/
  / /_/ / __/  \__ \ / /      / / / __/  \__ \ / /   
 / _, _/ /___ ___/ // /      / / / /___ ___/ // /    
/_/ |_/_____//____//_/      /_/ /_____//____//_/     

TEST() && REST() || DIE()                      V1.0.0                       

=====================================================

Rest test is an automated tool to help test an API in the cli. It as been made as an alternative to postman to simplify git workflow and assertions/readability.

This package is used in production code but ⚠️ open sourcing is in progress ⚠️. Please feel free to get in touch if you are interrested (via creating an issue for example)

Installation

You have to install bun for it to work

Here is an example start script in package.json

rest-test --configPath=./path/to/my/config --testFlowPath=./path/to/my/test/flow

Writing a test flow

import { TestFlow } from './Path/to/my/config'
import { TestItem, assert } from 'rest-test'



const testFlow = {
    name: 'login test flow',
    solo: true, // when working with multiple test flows, you can run only this one by using a filter in CLI params or putting solo to true
    priority: 10, // when working with multiple test flows, you can set a priority to sort test, the lower, the prior
    items: [
        // EXPLICIT way
        {
            description: `As user A I can't get userB infos`,
            route: 'my/route', // server url will be appended by default
            apiKey: 'userA', // this is typed and will be passed for the config to retrieve apiKey
            // Or you can use
            as: 'userA',
            status: 403,
            after(env, data) {
                // use snippets to generate assertions
                // when asserting, the assertion message will be automatically computed
                assert(data.errorMessage, 'data.errorMessage', { type: 'string' }) // type
                assert(data.errorMessage, 'data.errorMessage', 'no permission') // equality
            }
        },
        // SHORT WAY
        {
            d: [403, 'userA', `I can't get userB infos`], // read it like 403, as userA...
            route: 'my/route',
            after(env, data) {
                // ...
            }
        },
    ]
} satisfies TestFlow<TestEnv>

export default testFlow

Config

Here is an example config



import { RestTestConfig, TestFlow as TestFlowRaw, TestItem as TestItemRaw, assert } from 'rest-test'
import { assert } from 'rest-test'


export const restTestEnv = {
    routes: allRoutes, // here all routes are put in env so that we have autocomplete on possible routes
}

/** Allow shorcut when testing on multiples servers */
export const servers = {
    default: generalConfig.serverLiveUrl,
}


type ConnexionInfos = { email: string, password: string }
type TestUserNames = keyof typeof testUsers

export const restTestConfig: RestTestConfig<'userA' | 'userB', { baseEnvType: true }, TestUserNames, ConnexionInfos> = {
    servers: {
        default: generalConfig.serverLiveUrl,
    },
    disableSolo: generalConfig.env === 'ci', //
    apiKeys: generalConfig.apiKeys as any,
    env: restTestEnv,
    //----------------------------------------
    // BEFORE ALL TESTS
    //----------------------------------------
    async onBeforeAllTests({ 
        env,
        isReload // was it the first run or is it just a hot reload OR retry
    }) {
        // INIT BACKEND SDK
        $.init('http://localhost:9086')

        if (!isReload) {
            C.info('CLEARING THE DATABASE AND SEEDING')
        }
    },
    //----------------------------------------
    // BEFORE / AFTER EACH TEST
    //----------------------------------------
    async onBeforeTest({ env, as, apiKey, headers }) {
        // here you have all the test config, so you can modify headers the way you want for example
        if (apiKey || as) {
            // login with apiKey
            headers.Authorization = apiKeys[apiKey || as]
        }
    },
    onAfterTest() {
        $.setHeaders({ Authorization: null }, true)
        $.setAuthorization(null)
    },
}

// exports so you can use them with the right type
export type TestFlow<EnvFromUser> = TestFlowRaw<TestUserNames, ConnexionInfos, typeof restTestConfig & { env: typeof restTestConfig['env'] & typeof restTestEnv & EnvFromUser }>

export type TestItem<EnvFromUser> = TestItemRaw<TestUserNames, ConnexionInfos, typeof restTestConfig & { env: typeof restTestConfig['env'] & typeof restTestEnv & EnvFromUser }>