Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
sinon stubbing a module does not properly work

I have a twitter plugin which uses axios internally to call the twitter oembed api. In the unit tests, I would like to stub / mock these external calls with sinon. However, for some reason, the sinon.stub() call does not replace the implementation of axios.get inside the concrete implementation in twitter.js

twitter.spec.js:

const axios = require('axios')
const tweetIncludePlugin = require('../../../../../plugins/includes/twitter')
const tweetFunction = tweetIncludePlugin()
this.renderFn = tweetFunction.rendering.render

describe('Twitter Plugin:', function () {
  let stub

  beforeEach(async function () {        
    stub = sinon.stub(axios, 'get').resolves({data: {html: 'OEMBED_RETURNED_HTML'}})
  }

  it('with existing tweet embedLink', async function () {
    const includeParams = {embedLink: 'https://twitter.com/dsdsd/status/xy'}
    const {html} = await this.renderFn(includeParams, {})
    sinon.assert.calledOnce(stub)
    expect(html).to.be.a('string').and.equals('OEMBED_RETURNED_HTML')
  })

}

tweet.js:

const axios = require('axios')
module.exports = function () {

  async function renderTweet (params, options) {
    const embedLink = _.get(params, 'embedLink')
    const url = `http://publish.twitter.com/oembed?url=${embedLink};dnt=true;maxheight=300;maxwidth=400;`
    await axios.get(url)
      .then(response => {
        console.log(`Success ${response.status}, data: ${response.data.html}`)
        const html = response.data.html
        return html
      })
  }

  return {
    name: 'twitterPlugin',
    rendering: {
      type: 'function',
      render (params, options) {
        return renderTweet(params, options)
      }
    }
  }
}

After some trial and error, I found out, that if I place

const tweetIncludePlugin = require('../../../../../plugins/includes/twitter')
const tweetFunction = tweetIncludePlugin()
this.renderFn = tweetFunction.rendering.render

after the stub = sinon.stub(axios, 'get').resolves({data: {html: 'OEMBED_RETURNED_HTML'}}) call, it works! But I don't understand why! From my understanding, both require() calls will acquire a reference to the same module instance of axios in which .get() should have been mocked no?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda