@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

781

Version

2.6.0

License

MIT

Unpacked Size

12.3 kB

Total Files

7

Last publish

Collaborators

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