Skip to content

Instantly share code, notes, and snippets.

@mroderick
Created August 9, 2017 08:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mroderick/24b26b3477b83a8907193b9e4bbf90d3 to your computer and use it in GitHub Desktop.
Save mroderick/24b26b3477b83a8907193b9e4bbf90d3 to your computer and use it in GitHub Desktop.
A sample which shows that you cannot stub the `Array.prototype.filter` method. All works fine for `Array.prototype.map`.
'use strict';
const sinon = require( 'sinon' );
const expect = require( 'chai' ).expect;
describe( 'Array.prototype', () => {
describe( 'map()', () => {
it( 'uses native map', () => {
const callback = sinon.spy();
const stub = sinon.stub( Array.prototype, 'map' ).returns( [ 2, 4 ] );
const arr = [].map( callback );
expect( stub.calledOnce ).to.equal( true );
expect( stub.firstCall.args[ 0 ] ).to.equal( callback );
expect( arr ).to.deep.equal( [ 2, 4 ] );
} );
} );
describe( 'filter()', () => {
it( 'uses native filter', () => {
const callback = sinon.spy();
const stub = sinon.stub( Array.prototype, 'filter' ).returns( [ 1 ] );
const arr = [].filter( callback );
expect( stub.calledOnce ).to.equal( true );
expect( stub.firstCall.args[ 0 ] ).to.equal( callback );
expect( arr[ 0 ] ).to.equal( 1 );
} );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment