Skip to content

Sinon fake parameters aren't passed after 5.5.1 #336

Open
@edthorne

Description

@edthorne

This is related to the fix applied for #256. I spent some time upgrading through releases to find the behavior was working through version 5.5.0 and stopped working with 5.5.1. This may or may not be related to #273.

In my case I was using a fake to implement behavior as follows:

const awsSdkMock = require('aws-sdk-mock')
const AWS = require('aws-sdk')
const { describe, it, beforeEach, afterEach } = require('mocha')
const { expect } = require('chai')
const sinon = require('sinon')

describe('Test case', () => {
    beforeEach(() => {
        awsSdkMock.setSDKInstance(AWS);
    });

    afterEach(() => {
        awsSdkMock.restore();
        sinon.restore();
    })

    it('should be able to access parameters', async () => {
        // use a fake to mock behavior (works < 5.5.1)
        const createDBSnapshotMock = sinon.fake(params => {
            // return the passed in identifier in the response
            return Promise.resolve({ DBSnapshot: { DBSnapshotIdentifier: params.DBSnapshotIdentifier } })
        })
        awsSdkMock.mock('RDS', 'createDBSnapshot', createDBSnapshotMock)

        const rds = new AWS.RDS()

        const result = await rds.createDBSnapshot({
            DBInstanceIdentifier: 'dbInstanceId',
            DBSnapshotIdentifier: 'snapshotId'
        }).promise()

        expect(createDBSnapshotMock.callCount).to.equal(1)
        expect(createDBSnapshotMock.firstCall.args[0].DBSnapshotIdentifier)
            .to.equal('snapshotId')
        expect(result.DBSnapshot.DBSnapshotIdentifier).to.equal('snapshotId');
    })
});

The changes in #257 result in the constructed callback spy being passed as the parameters to the fake rather than the parameters to the mocked function.

If I change the fake to accept an extra, unused parameter like this: const createDBSnapshotMock = sinon.fake((params, cb) => { then it results in the parameters being passed as expected since replace.length === 2.

I'm not sure what the proper behavior is in this case and if the second argument for the fake parameters is required.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions