Created
August 9, 2017 08:24
-
-
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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