Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sinon fake parameters aren't passed after 5.5.1 #336

Open
edthorne opened this issue Oct 27, 2023 · 1 comment
Open

Sinon fake parameters aren't passed after 5.5.1 #336

edthorne opened this issue Oct 27, 2023 · 1 comment

Comments

@edthorne
Copy link

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.

@edthorne
Copy link
Author

I have a second related issue where I'm using a fake to prevent the SDK from executing a call during testing.

    // service mock to prevent notification callout
    AWS.mock('SNS', 'publish', sinon.fake())

The fake provides no return value/undefined so typeof result.then logs an error. This tracks back to the changes in #253. The issue does not occur on 5.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant