Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Stub out node-vault import

How can the 'node-vault' module be stubbed out using Sinon.JS?

Here is the sample code to be tested:

const NodeVault = require('node-vault');

class BearerAuth {

  getVaultConfig (token) {
    return {
      apiVersion: 'v1',
      endpoint: app.get('vault'),
      token: token
    };
  }

  verify (token) {
    const vault = NodeVault(this.getVaultConfig(token));
    const resp = vault.tokenLookupSelf().then((result) => {
      console.log(`Vault token: ${token}, successfully authenticated.`);
      return Promise.resolve({
        'name': result.data.meta.username
      });
    }).catch((err) => {
      console.error(err.message);
      return Promise.reject(new this.ApplicationError.Authentication('Bearer token is incorrect.'));
    });
    return resp;
  }
}

module.exports = BearerAuth;
}

The test code which attempts to stub out the module using sinon.stub:

const assert = require('assert');
const sinon = require('sinon');
const nodeVault = require('node-vault');
const BearerAuth = require('bearer');


describe('Bearer AuthConfig', () => {
beforeEach(async () => {
    class TestBearerAuth extends BearerAuth {
      // override some other methods     
    }
    testAuth = new TestBearerAuth();
    const vaultConfig = {
        endpoint: 'http://localhost:8200',
        token: '123'
    };
    testAuth.getVaultConfig = sinon.stub.returns(vaultConfig);
  });

  it('returns user when valid token', async () => {
    const user = await testAuth.verify(null, 'mytoken');
    assert.deepEqual(user, {name: 'sre'});
  })
}

This fails when run with this error:

  1) Bearer Auth
       returns user when valid token:
     TypeError: Attempted to wrap undefined property undefined as function
      at wrapMethod (node_modules/sinon/lib/sinon/util/core/wrap-method.js:70:21)
      at TestBearerAuth.stub [as getVaultConfig] (node_modules/sinon/lib/sinon/stub.js:65:44)
      at TestBearerAuthConfig.verify (src/modules/auth/bearer.js:34:34)
      at Context.<anonymous> (test/unit/modules/auth/test-bearer.js:57:39)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

The node-vault library has the following export in index.d.ts:

declare function NodeVault(options?: NodeVault.VaultOptions): NodeVault.client;
export = NodeVault;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The unit test code from the node-vault library provided the solution:

Test code:

const assert = require('assert');
const sinon = require('sinon');
const nodeVault = require('node-vault');
const BearerAuth = require('bearer');


describe('Bearer AuthConfig', () => {
beforeEach(async () => {
    request = sinon.stub();
    response = sinon.stub();
    response.statusCode = 200;
    response.body = {
      data: {
        meta: {
          username: 'jack'
        }
      }
    };

    request.returns({
      then (fn) {
        return fn(response);
      },
      catch (fn) {
        return fn();
      }
    });
    class TestBearerAuth extends BearerAuth {
       getVaultConfig (token) {
        return {
          endpoint: 'http://localhost:8200',
          token: '123',
          namespace: 'test',
          'request-promise': {
            defaults: () => request // dependency injection of stub
          }
        };
      }     
    }
    testAuth = new TestBearerAuth();
  });

  it('returns user when valid token', async () => {
    const user = await testAuth.verify(null, 'mytoken');
    assert.deepEqual(user, {name: 'sre'});
  })
}

Adding this here in case someone else runs into a similar issue.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!