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

74
Visualizações
Sinon stub out module's function

Imagine I have a file here:

// a.js
const dbConnection = require('./db-connection.js')

module.exports = function (...args) {
   return async function (req, res, next) {
      // somethin' somethin' ...
      const dbClient = dbConnection.db
      const docs = await dbClient.collection('test').find()
 
      if (!docs) {
         return next(Boom.forbidden())
      }
   }
}

, the db-connection.js looks like this:

const MongoClient = require('mongodb').MongoClient
const dbName = 'test'
const url = process.env.MONGO_URL

const client = new MongoClient(url, { useNewUrlParser: true,
  useUnifiedTopology: true,
  bufferMaxEntries: 0 // dont buffer querys when not connected
})

const init = () => {
  return client.connect().then(() => {
    logger.info(`mongdb db:${dbName} connected`)

    const db = client.db(dbName)
  })
}

/**
 * @type {Connection}
 */
module.exports = {
  init,
  client,
  get db () {
    return client.db(dbName)
  }
}

and I have a test to stub out find() method to return at least true (in order to test out if the a.js's return value is true. How can I do that?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The unit test strategy is to stub the DB connection, we don't need to connect the real mongo DB server. So that we can run test cases in an environment isolated from the external environment.

You can use stub.get(getterFn) replaces a new getter for dbConnection.db getter.

Since the MongoDB client initialization happens when you require the db-connection module. You should provide a correct URL for MongoClient from the environment variable before requiring a and db-connection modules. Otherwise, the MongoDB nodejs driver will throw an invalid url error.

E.g.

a.js:

const dbConnection = require('./db-connection.js');

module.exports = function () {
  const dbClient = dbConnection.db;
  const docs = dbClient.collection('test').find();

  if (!docs) {
    return true;
  }
};

db-connection.js:

const MongoClient = require('mongodb').MongoClient;
const dbName = 'test';
const url = process.env.MONGO_URL;

const client = new MongoClient(url, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

const init = () => {
  return client.connect().then(() => {
    console.info(`mongdb db:${dbName} connected`);
    const db = client.db(dbName);
  });
};

module.exports = {
  init,
  client,
  get db() {
    return client.db(dbName);
  },
};

a.test.js:

const sinon = require('sinon');

describe('a', () => {
  afterEach(() => {
    sinon.restore();
  });
  it('should find some docs', () => {
    process.env.MONGO_URL = 'mongodb://localhost:27017';
    const a = require('./a');
    const dbConnection = require('./db-connection.js');

    const dbStub = {
      collection: sinon.stub().returnsThis(),
      find: sinon.stub(),
    };
    sinon.stub(dbConnection, 'db').get(() => dbStub);
    const actual = a();
    sinon.assert.match(actual, true);
    sinon.assert.calledWithExactly(dbStub.collection, 'test');
    sinon.assert.calledOnce(dbStub.find);
  });
});

Test result:

  a
    ✓ should find some docs (776ms)


  1 passing (779ms)

------------------|---------|----------|---------|---------|-------------------
File              | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------------|---------|----------|---------|---------|-------------------
All files         |      75 |       50 |      25 |      75 |                   
 a.js             |     100 |       50 |     100 |     100 | 7                 
 db-connection.js |      60 |      100 |       0 |      60 | 11-13,21          
------------------|---------|----------|---------|---------|-------------------
about 4 years ago · Juan Pablo Isaza Relatório
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