Skip to content

Sinon no longer throws when stubbing non-existent properties #1508

@schreifels

Description

@schreifels
  • Sinon version : 2.3.7
  • Environment : OSX, Node 6 (Jest)
  • Other libraries you are using: Mocha

What did you expect to happen?

When calling sinon.stub(obj, 'nonexistentProperty');, I would expect it to throw an error, e.g. "Attempted to wrap undefined property".

What actually happens

It does not throw (nor does it create a stub on the object at that property).

How to reproduce

Interestingly, if you use the deprecated 3rd argument to sinon.stub, it does still work, as shown in this example:

'use strict';

const assert=require('assert');
// const sinon = require('./lib/sinon'); // if checking out the sinon project
const sinon = require('sinon');

describe('sinon', () => {
  context('with the standard syntax', () => {
    it('throws when stubbing a non-existent property', () => {
      const obj = {};

      // FAIL: expected [Function] to throw an error
      assert.throws(() => {
        sinon.stub(obj, 'nonexistentProperty');
      });
    });
  });

  context('with the deprecated syntax', () => {
    it('throws when stubbing a non-existent property', () => {
      const obj = {};

      // PASS
      assert.throws(() => {
        sinon.stub(obj, 'nonexistentProperty', () => {});
      }, 'Attempted to wrap undefined property');
    });
  });
});

(I would have expected both tests to pass.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions