Giter VIP home page Giter VIP logo

tepper's Introduction

Tepper

GitHub Workflow Status codecov npm MIT License PRs Welcome

Modern HTTP expectation library inspired in supertest

About

This library is a modern implementation of supertest, typescript-first and promise first for the modern ages. It supports jest and vitest.

Installation

Install tepper as an npm module and save it to your package.json file as a development dependency:

npm install --save-dev tepper

Example

You may pass an http.Server, or a Function to tepper() - if the server is not already listening for connections then it is bound to an ephemeral port for you so there is no need to keep track of ports.

import tepper from "tepper"
import express from "express"

const app = express()

app.get("/user", (req, res) => {
  res.status(200).json({ name: "john" })
})

const { body } = await tepper(app).get("/user").expect(200).run()

console.log(body)

Notes

Inspired by supertest.

License

MIT

tepper's People

Contributors

renovate[bot] avatar renovate-bot avatar danielramosacosta avatar gustavogr avatar tairosonloa avatar

Stargazers

 avatar v1rtl avatar Cristóbal avatar  avatar Javier avatar Fernando García Hernández avatar Jaume Moreno Cantó avatar Bata Zoltán avatar Carlos Díaz avatar Miguel G. Rubin avatar Aarón Pérez avatar  avatar Raúl Anatol avatar Daniel Gak Anagrov avatar

Watchers

James Cloos avatar  avatar  avatar

tepper's Issues

Improve documentation

We should improve documentation about the Tepper API. There are a lot of functions and options that are not documented and can be missed by the user.

We should are more Markdown files or make a kind of documentation website. Those made with pure Markdown and hosted freely in GitHub.

[Discussion] Types problems in comparation with supertest

I came across a problem due to a stricter typing in this library compared to supertest:

The signature for send() method is

send(body: Record<string, unknown> | Array<unknown>)

opposed to supertest

send(data?: string | object)

This leads to Index signature is missing in type error when passing classes as parameters.

For example:

class CreateUserDto {
  // ....
}

createUserTepper = async (body: CreateUserDto) => {
    await tepper(app).post('/users').send(body) // Error: Index signature is missing in type
}

createUserTepperV2 = async (body: CreateUserDto) => {
    await tepper(app).post('/users').send(body as unknown as Record<string, unknown>) // Success, but verbose
}

createUserSupertest = async (body: CreateUserDto) => {
    await supertest(app).post('/users').send(body) // Success
}

I propose to relax the typing of the send() methods. My arguments are mainly two:

  • It will speed up tests development while using this library and remove verbosity.
  • Record<string, unknown> and object are equivalent types at runtime, but a big part of TypeScript's safety comes from the fact that these aren't the same, and that it'll only let you treat an object as a dictionary if it knows it's explicitly intended as one. This is what throws Index signature is missing in type error, and it's thought to stop you from accidentally reading and writing totally non-existent properties on objects. However, for our use case, is type safe enough to just use object instead of Record<string, unknown>.

I could work on this issue. Please, assign it to me if you agree with me in this type relaxation.

Further reading: microsoft/TypeScript#15300

Doesn't work with Bun (`bun:test`)

I'm hitting this error:

      at processTicksAndRejections (:61:76)
 Unless Middleware custom parameter test > should execute if function returns false [6.72ms]
120 |             else {
121 |                 expect(result.body).toEqual(config.expectedBody);
122 |             }
123 |         }
124 |         if (config.expectedStatus) {
125 |             expect(result.status).toEqual(config.expectedStatus);
                ^
ReferenceError: Can't find variable: expect
      at runExpectations (/home/v1rtl/Coding/tinyhttp/unless/node_modules/tepper/dist/TepperRunner.js:125:12)
      at /home/v1rtl/Coding/tinyhttp/unless/node_modules/tepper/dist/TepperRunner.js:81:8
      at processTicksAndRejections (:61:76)
✗ Unless Middleware custom parameter test > should not execute if function returns true [6.28ms]

My test case is this:

import { describe, it } from 'bun:test'
import { type IncomingMessage, type ServerResponse, createServer } from 'node:http'
import tepper from 'tepper'

function InitAppAndTestUnless(options: UnlessMiddlewareOptions | CustomUnless) {
  const app = createServer<typeof IncomingMessage, typeof ServerResponse>((req, res) => {
    unless({ handler: exampleMiddleware, type: 'mw' }, options)(req, res, () => {
      return
    })
    res.statusCode = 200
    res.end()
  })

  const fetch = tepper(app)

  return fetch
}

it("should execute if path string doesn't match", async () => {
  const fetch = InitAppAndTestUnless({ path: '/test' })

  await fetch.get('/').expectStatus(200).run()
})

I can create a reproduction snippet if needed

ESM support

If importing in TS with Node16 / NodeNext set I get this:

image

Querystring built-in API is legacy

Node built-in API querystring is legacy and the recommended one is URLSearchParams.

This is important due to how arrays are handled.

With querystring:

{test: ['a', 'b']} => test=a&test=b

With URLSearchParams

{test: ['a', 'b']} => test=a,b

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4
.github/workflows/npm-publish.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
npm
package.json
  • form-data-encoder ^4.0.0
  • formdata-node ^6.0.0
  • mime-types ^2.1.34
  • @types/cookie-parser 1.4.7
  • @types/express 4.17.21
  • @types/mime-types 2.1.4
  • @types/multer 1.4.11
  • @types/node 20.14.2
  • @types/node-fetch 2.6.11
  • @typescript-eslint/eslint-plugin 7.13.0
  • @typescript-eslint/parser 7.13.0
  • @vitest/coverage-v8 ^1.1.0
  • cookie-parser 1.4.6
  • eslint 8.57.0
  • eslint-config-prettier 9.1.0
  • eslint-plugin-import ^2.29.0
  • eslint-plugin-node 11.1.0
  • eslint-plugin-prettier 5.1.3
  • eslint-plugin-promise 6.2.0
  • express 4.19.2
  • multer 1.4.4
  • node-fetch ^3.3.2
  • prettier 3.3.2
  • rimraf 5.0.7
  • typescript 5.4.5
  • vitest 1.6.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.