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

@universal-packages/fetch-jest

v1.2.1

Published

Jest tooling for fetch

Downloads

9,569

Readme

Fetch Jest

npm version Testing codecov

Jest tooling for fetch. This library does not mock fetch but instead provides a way to actually test an http socket using the fetch api, providing quicker ways to reach the endpoints.

By default all request are made to http://localhost:<4000 + JEST_WORKER_ID>, see Configurations.

Install

npm install @universal-packages/fetch-jest

Setup

Add the following to your jest.config.js or where you configure Jest:

module.exports = {
  setupFilesAfterEnv: ['@universal-packages/fetch-jest']
}

Jest Globals

Fetchers

fDelete([path: string])

Makes a DELETE request to the given path.

it('should delete the todo', async () => {
  await fDelete('/todos/1')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fGet([path: string, query: Object])

Makes a GET request to the given path.

it('should return the todo list', async () => {
  await fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
  expect(fResponseBody).toEqual({ todos: [{ id: 1, text: 'Buy milk' }] })
})

fPatch([path: string, body: Object])

Makes a PATCH request to the given path.

it('should update the todo', async () => {
  await fPatch('/todos/1', { text: 'Buy milk and eggs' })

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fPost([path: string, body: Object])

Makes a POST request to the given path.

it('should create a todo', async () => {
  await fPost('/todos', { text: 'Buy milk' })

  expect(fResponse).toHaveReturnedWithStatus('OK')
  expect(fResponseBody).toEqual({ id: 1, text: 'Buy milk' })
})

fPut([path: string, body: Object])

Makes a PUT request to the given path.

it('should update the todo', async () => {
  await fPut('/todos/1', { text: 'Buy milk and eggs' })

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fBuildFormData([body: Object, files: Object])

Builds a FormData object with the given body and files, the files values are paths to the local files to be uploaded.

it('should upload the file', async () => {
  const formData = fBuildFormData({ text: 'Buy milk' }, { photo: './photo.jpg' })

  await fPost('/todos', formData)

  expect(fResponse).toHaveReturnedWithStatus('OK')
  expect(fResponseBody).toEqual({ id: 1, text: 'Buy milk', filesCount: 1 })
})

Configurations

fAuthorization(token: string)

It setups all future requests to have the Authorization header with the given token.

// Global for all test cases
fAuthorization('token')

it('should return the todo list', async () => {
  // Per test case
  fAuthorization('token1')

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fContentType(type: string)

It setups all future requests to have the Content-Type header with the given type.

// Global for all test cases
fContentType('application/json')

it('should return the todo list', async () => {
  // Per test case
  fContentType('html/text')

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fHeaders(headers: Object)

It setups all future requests to have the given headers.

// Global for all test cases
fHeaders({ 'X-Header': 'value' })

it('should return the todo list', async () => {
  // Per test case
  fHeaders({ 'X-Header': 'value1' })

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fHost(host: string)

It setups all future requests to be made to the given host.

// Global for all test cases
fHost('192.168.0.1')

it('should return the todo list', async () => {
  // Per test case
  fHost('localhost')

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fInit(init: OBject)

It setups all future requests to have the given init.

// Global for all test cases
fInit({ host: 'localhost', port: 3000, protocol: 'https' })

it('should return the todo list', async () => {
  // Per test case
  fInit({ host: '0.0.0.0', port: 3000, protocol: 'http' })

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

fPort(port: number)

It setups all future requests to be made to the given port.

// Global for all test cases
fPort(3000)

it('should return the todo list', async () => {
  // Per test case
  fPort(4000)

  fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

Matchers

toHaveReturnedWithStatus(status: string | number)

It checks if the response status is the given status.

it('should return the todo list', async () => {
  await fGet('/todos')

  expect(fResponse).toHaveReturnedWithStatus('OK')
})

Typescript

In order for typescript to see the global types you need to reference the types somewhere in your project, normally ./src/globals.d.ts.

/// <reference types="@universal-packages/fetch-jest" />

This library is developed in TypeScript and shipped fully typed.

Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

License

MIT licensed.