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

Using mocks in multiple test files fails #64

Closed
1 task done
danmana opened this issue Nov 16, 2021 · 4 comments
Closed
1 task done

Using mocks in multiple test files fails #64

danmana opened this issue Nov 16, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@danmana
Copy link

danmana commented Nov 16, 2021

Checklist

  • I have read Caveats documentation and didn't find a solution for this problem there.

Bug description

If we have mocks in multiple files, some of the test suites are failing.
If we run each file individually the tests succeed.
If we run mocha with --parallel it also works

See repro here: https://github.com/danmana/mocha-aws-sdk-client-mock

npm run mocha -- tests/one*.js works

npm run mocha -- tests/two*.js works

npm run mocha -- tests/*.js ⚠️ does not work, suite one fails

npm run mocha -- tests/*.js --parallel works
Also if we put all tests in the same file, it works.

The tests are pretty straight forward

const { ListBucketsCommand, S3Client } = require('@aws-sdk/client-s3');
const { mockClient } = require('aws-sdk-client-mock');
const assert = require('assert');
const mock = mockClient(S3Client);

describe('one', () => {
  let client;

  beforeEach(() => {
    mock.reset();
    client = new S3Client();
  });

  it('should work', async () => {
    mock.on(ListBucketsCommand).resolves({});

    const result = await client.send(new ListBucketsCommand());

    assert.deepEqual(result, {});
  });

});

This is probably due to the way mocha runs the tests, but I can't figure out how to fix it.

Environment

  • Node version: v14.18.0
  • Typescript version: 6.14.15
  • AWS SDK v3 Client mock version:
  • AWS JS SDK libs and versions:
  • "@aws-sdk/client-s3": "^3.41.0",
  • "aws-sdk-client-mock": "^0.5.6",
@danmana danmana added the bug Something isn't working label Nov 16, 2021
@m-radzikowski
Copy link
Owner

Thank you for the detailed description and repo with reproduction!

The problem seems to be that the S3Client mock from the first test file is overridden by the mock from the second test file. Apparently Mocha does not create separate environments for each test file.

The solution is to move mockClient() method to beforeEach() block. This way the mock will be created and configured for each of the tests.

const { ListBucketsCommand, S3Client } = require('@aws-sdk/client-s3');
const { mockClient } = require('aws-sdk-client-mock');
const assert = require('assert');

describe('one', () => {
  let mock;
  let client;

  beforeEach(() => {
    mock = mockClient(S3Client);
    client = new S3Client();
  });

  it('should work', async () => {
    mock.on(ListBucketsCommand).resolves({});

    const result = await client.send(new ListBucketsCommand());

    assert.deepEqual(result, {});
  });

});

I will add this to the caveats section of the readme.

This is not an issue with Jest, which probably separates execution environments for each test file.

@lynxSven
Copy link

lynxSven commented Mar 20, 2024

Do I have to use client = new S3Client() or does mockClient(S3Client) work on its own?
Have similar issues with mocha and chai

Have have a test where I define the on is defined in the first it and i can't get rid of it.
On every following test the mock is the same and i can't override it

describe('describe first test', () => {
    let mock: AwsStub<ServiceInputTypes, ServiceOutputTypes, SSMClientResolvedConfig>
    beforeEach(() => {
        mock = mockClient(SSMClient)
    })
    it('test 1 has on', () => {
        mock.on(GetParameterCommand).resolvesOnce({
            Parameter: {
                Name: 'parameterStore',
                Value: 'value'
            }
        })
        myTestObject.testMethod()
    })

    it('test 2 still has the on and i can not override', async () => {})
})

describe('next description still the defined mock', () => {
    beforeEach(() => {
        mockClient(SSMClient)
    })
    it('still', async () => {})
})

Also tried to use reset() and restore()

Is this a common problem or am i using it incorrectly

@m-radzikowski
Copy link
Owner

@lynxSven Mock is not reset automagically between the tests. The restore or reset can be called in afterEach() to clear it. If it's not working for you, please create a new issue with a reproduction.

@wjaspers
Copy link

The ava test framework has similar behavior where reset has no effect and mocked client paginators do not respond at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants