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

@s-ui/mockmock

v2.6.0

Published

Mocking utilities for testing.

Downloads

3,966

Readme

sui-mockmock

Mocking utilities for testing.

⚠️ It is highly recommended to use sui-mock instead of this package. It could be deprecated in the future.

Motivation

Centralize common solutions for common mocking concerns in JavaScript.

Installation

npm install @s-ui/mockmock --save-dev

Available mockers

http

Mocks http requests in node and XMLHttpRequest requests in the browser.

Usage

import {HttpMocker} from '@s-ui/mockmock'
import axios from 'axios'

// Mock any requests
const mocker = new HttpMocker()
mocker
  .httpMock('http://fake.api.com')
  .get('/my-service')
  .reply({property: 'value'}, 200)

// Make your requests
axios
  .get('http://fake.api.com/my-service')
  .then(response => console.log(response)) // { property: 'value' }

Also available for import

import HttpMocker from '@s-ui/mockmock/lib/http'

Details

Since this library internally constructs the url of the original and the mocked request and then compares them, it's needed to create the mock request with the same parameter order than the original, if not, it will throw a 404.

incorrect The parameters of both petitions (original and mocked) don't have the same parameter order (err 404 thrown)

mocker
  .httpMock(...)
  .get('/urlToAttack')
  .query({
    search: 'value of the search',
    working: false,
  })
  ...
  ...

domain
  .get('attacks_the_same_url_use_case')
  .execute({
    working: false,
    search: 'value of the search',
  })
  ...
  ...

correct The mocked and the original HTTP requests have to same parameter order

mocker
  .httpMock(...)
  .get('/urlToAttack')
  .query({
    search: 'value of the search',
    working: true,
  })
  ...
  ...

domain
  .get('attacks_the_same_url_use_case')
  .execute({
    search: 'value of the search',
    working: true,
  })
  ...
  ...

Clean up state

It is important to clean tests before and after so you have a initial state non dependent.

Why? Sharing mutable state between components it is a bad idea.

  • One tests depends on the others, so you can not run one test in isolation.
  • If you refactor one test, you might break all the others tests, because all tests are part of a chain of actions.
  • If tests are isolated you can: paralelize test and remove or add tests without breaking other ones

Example:


describe('abc', () => {
  beforeEach(() => {
    mocker.create()
  })
  afterEach(() => {
    mocker.restore()
  })
  ...
  it('xyz', () =>{
    ...
  })
})

Mocking Requests

The .reply(response, statusCode, headers) method causes the 'fake' server to respond to any request not matched by another response with the provided data.

mocker
  .httpMock(...)
  .get('/urlToAttack')
  .query({
    search: 'value of the search',
    working: true,
  })
  .reply('{status: "ok"}', 200, {'Access-Control-Allow-Credentials': true})
  ...
  ...

const response = domain
  .get('attacks_the_same_url_use_case')
  .execute({
    search: 'value of the search',
    working: true,
  })

expect(response.data.status).to.equal('ok')

Retriving Requests

If you want to test what your use_case, function, etc is doing, probably you will need to check that based on the arguments provided to the function are transformed and as an output or side effect one or various requests are done will a certain url, body, or headers.

The .requestNTH(index) method returns the nth request mocked by the fake server so you can assert things like:

  • The content of the body of the request is the one expected.
  • The headers of the request are the expected.
  • The url of the request is the one expected.
  • The order of the requests is the correct one.
mocker
  .httpMock(...)
  .get('/urlToAttack')
  .query({
    search: 'value of the search',
    working: true,
  })
  .reply('{status: "ok"}', 200, {'Access-Control-Allow-Credentials': true})
  ...
  ...

const response = domain
  .get('attacks_the_same_url_use_case')
  .execute({
    search: 'value of the search',
    working: true,
  })

const request = mock.requestNTH(0)
expect(request.body).to.be.equal('year=2030&doors=3&color=blue')
expect(request.url).to.be.equal('https://.../urlToAttack')
...