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

jest-dot-http-files

v0.0.26

Published

Jest transformer for .http files

Downloads

1,365

Readme

Jest transformer for .http files

Literature and references

About jest transformers

https://jestjs.io/docs/code-transformation

About HTTP Files

  • https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/test/http-files.md
  • https://marketplace.visualstudio.com/items?itemName=humao.rest-client
  • https://www.jetbrains.com/help/idea/exploring-http-syntax.html

Usage

Install the npm package as dev dependency: jest-dot-http-files

It works by exporting a jest transformer. Here is a simplified way to use it:

// jest.config.js

export default {
  moduleFileExtensions: ['http'],
  testMatch: ['**/*.http'],
  transform: {
    '\\.http$': 'jest-dot-http-files/transformer'
  }
}

Deviations from the existing literature

Global variables

Variables starting with @@ are held in the global scope, meaning they are available to all requests.

Meta variables

Meta variables are defined in the request scope within comments, similarly to jsdoc.

The @title meta variable

Overrides the test title. This variable is optional.

# @title My meaningful test title.
GET http://foo/bar

The @name meta variable

Used to name a request. Named requests can be used in interpolations. This variable is optional.

# @name foo
GET http://foo/bar

Accessing the named request:

GET http://foo/{{foo.$.response.body.id}}

The @only meta variable

Meaning: runs only this request.

# @only
GET http://foo/bar

The @skip meta variable

Meaning: don't run this request.

# @skip
GET http://foo/bar

Functions

Functions are used within interpolations {{...}}.

The $guid function

Generates a random v4 UUID.

The $uuid function

Alias to $guid.

The $randomInt function

Generates a random integer number between min and max.

GET http://foo/{{$randomInt 10 20}}

The $datetime function

Generates a date/time string: {{$datetime <format> [offset unit]}}

Format:

  • rfc1123:
  • iso8601:
  • or a custom format, e.g: "dd-MM-yyyy"

Offset unit:

  • y = Year
  • M = Month
  • w = Week
  • d = Day
  • h = Hour
  • m = Minute
  • s = Second

Example:

# adds one year to current date
GET http://foo/{{$datetime iso8601 1 y}}

A note on "assertions"

Assertions are made using jest snapshots.

The @ignoreHeaders meta variable

A regex pattern string.

Use it to specify what headers (both request and response) to ignore for snapshot assertion. The "age" and "date" headers are always ignored.

# @ignoreHeaders ^(x-request-id|x-vendor-.*)
GET http://foo/bar

The @ignore meta variable

Use it to specify what json-paths to ignore for snapshot assertion. The json-path is related to the named-request's request/response.

This variable is accumulative and forms a list, meaning you can ignore more than one path per request.

# @ignore $.response.body.id
# @ignore $.response.body.completed
GET http://foo/bar

The @expect meta variable

Asserts a json-path against a constant. The json-path is related to the named-request's request/response.

This variable is accumulative and forms a list, meaning you can have many assertions.

# @expect $.response.status 200
# @expect $.response.statusText "OK"
GET http://foo/bar

The @status meta variable

Alias to @expect $.response.status <statusCode> and ( optionally ) @expect $.response.statusText "<statusText>"

# @status 200 "OK"
GET http://foo/bar

or the status code only

# @status 200
GET http://foo/bar

The @throws meta variable

# @throws
GET http://foo/bar

or with a regex pattern

# @throws .*error.*
GET http://foo/bar

Expects the request to throw an error.

This is helpful to test negative cases, for example:

# @titles Tests that the response is not 500
# @status 500
# @throws
GET http://foo/bar

See also

  • Language support for vscode:
    • Repository: https://github.com/andreventuravale/vscode-dot-http-files
    • Marketplace: https://marketplace.visualstudio.com/items?itemName=AndreValeOutlook.vscode-dot-http-files