@s-ui/mockmock

2.6.0 • Public • Published

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')
...

Readme

Keywords

none

Package Sidebar

Install

npm i @s-ui/mockmock

Weekly Downloads

763

Version

2.6.0

License

MIT

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

  • sergi.martinez.adevinta
  • arnau.guell
  • miriam-gil
  • andresin87.adevinta
  • alfredo.narvaez
  • sendami.luque.ext
  • sebastian.badea.adevinta
  • thomas.page.ext
  • victoria.pasichnyk.ext
  • daniel.perez.ext
  • albert.peiro
  • frandelacasa-adevinta
  • mariapaula.forero.ext
  • hpintos_adevinta
  • sziauberyte
  • victor.perez.adevinta
  • oscar-raig-adevinta
  • carlos.gonzalezl
  • carolina.mallo.ext
  • david.nieto
  • ferran.simon
  • sergi.quintela
  • jamile.radloff
  • xavi_ballestar
  • luz_adv
  • ignacio.rodriguez
  • carlosvillu-adevinta
  • diegomr
  • arturo.vicente
  • adria.velardos
  • emiliovz
  • dann41
  • ruben-martin
  • pol.valls
  • cristina.rodriguez.duque
  • sergio.escano
  • marc.benito
  • azahara
  • patricio.sartore
  • giovanny.sayas.ext
  • david.cuadrado.ext
  • alex.castells
  • beatrizip
  • pablo.rey-adevinta
  • sergiocollado
  • cristhianb
  • alisa_bayanova
  • davidmartin2108
  • ferrangbtw
  • estefania_garcia
  • belen.santos
  • alfredo.arronte
  • joanleon-adv
  • luis-garrido
  • aitor.rodriguez
  • jordi.munoz
  • oscar_ramirez
  • ignacio_navarro
  • a.ferrer
  • gfabregoadv
  • izeller
  • salvador.juan
  • ivanmlaborda
  • alejandro.ferrante
  • pa.chruscinski.ext
  • isabelgomez87
  • jenifer.lopez
  • pablogs
  • javier.miguel
  • oscar.gomez
  • marian.lucaci
  • alverd004
  • oriol.puig
  • nacho_torrella
  • xavi.murcia
  • javiauso
  • alfredo.zimperz
  • francisco.ruiz.lloret
  • andresadv
  • schibstedspain