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

Multiple interactions causing Pact file to only contain the last one #11

Closed
darrenhaken opened this issue Sep 20, 2016 · 13 comments
Closed

Comments

@darrenhaken
Copy link

darrenhaken commented Sep 20, 2016

I'm trying to have several Mocha tests with two interactions against the same provider.

After some digging the recommended approach seems to be to have the finalize() function invoked as part of the after() function of mocha.

This causes a timeout, can someone help? I can provide a Gist to show what I'm trying to do

@mefellows
Copy link
Member

Hi @darrenhaken, a Gist would be great! Currently, all of the tests need to be performed against the same mock server otherwise they will override each other (hence moving the finalize() call to within a single after() function).

@tarciosaraiva
Copy link
Contributor

hi @darrenhaken a Gist would be awesome.

I noticed lately that the call to delete the server on pact-node is not working properly causing the timeout to occur but keen to see how you are approaching.

Personally I found that having one Mocha test per provider is a cleaner solution - certainly it depends on how many tests you got but keep things nice and neat.

@mefellows is correct: if you start servers within different mocha test files using the same consumer and provider they will override each other.

Another solution is what is done here. Essentially you create a single mock server that is available across your entire test suite. There are two caveats with that approach:

  • you need to run Mocha with --delay option which will force you to execute run()
  • you will need to stop and start the servers on your test files

At least you don't have to create new ones and don't end up with ports to manage.

Anyway, Gist. Looking forward to it!

@tarciosaraiva
Copy link
Contributor

hey @darrenhaken any progress on your issue?

@darrenhaken
Copy link
Author

darrenhaken commented Sep 28, 2016

@tarciosaraiva I actually ran out of time setting up a test condition for this 👎

I ended up putting all the interactions in a single test as suggested which let me generate the Pact

This does seem to still be an issue though; I also seem to occasionally get timeouts like you describe.

@mefellows
Copy link
Member

Hi @darrenhaken, is the solution above sufficient to close this out? Tarcio's solution (helper) is the current approach recommended if you want to split your tests cases into multiple files.

Timeouts should be managed by mocha/your test framework, this is not something Pact should worry about (unless I am misunderstanding something)?

@darrenhaken
Copy link
Author

@mefellows Tarcio's solution of a single Mocha test seemed to do the trick. It does feel a little messy though.

@darrenhaken
Copy link
Author

@mefellows we can close this off for now considering there's a work around.

@mefellows
Copy link
Member

I've changed this to an enhancement, we'll review with the next major release (we are currently working in the background on a native JS solution).

@dgreene1
Copy link

dgreene1 commented Mar 28, 2017

This solution is problematic because it produces only one JSON file, which causes us to have to put all of our various providers under one provider name.

Hi @darrenhaken, is the solution above sufficient to close this out? Tarcio's solution (helper) is the current approach recommended if you want to split your tests cases into multiple files.

Has anyone found a way to do this while still giving us the ability to create a unique DSL files for each one of our providers to test against?

For instance, should we be trying to use pact-node-dsl ?

@mefellows
Copy link
Member

@dgreene1 that solution (shared helper) is only useful for @darrenhaken's use case where there is a many-to-one relationship between mocha tests to Provider (Mock Server). If you have multiple tests, - let's say one Mocha suite per Provider - then you should start separate Mock Servers for that purpose. Each Mock Server is designed to mock a single Provider, so you should model your tests this way also.

Does that make sense?

@mefellows
Copy link
Member

FYI we've added another feature - the ability to specify how we write to pacts - which is another solution to this problem.

See https://github.com/pact-foundation/pact-js#splitting-tests-across-multiple-files.

Closing.

@ondikumana
Copy link

For jest lovers who cannot use @tarciosaraiva's fix, a workaround that worked for me is making use of jest's globalSetup and globalTeardown. An additional hack was to manually instantiate the MockService for each worker that jest creates.

pact-js maintainers, let me know if there are any improvements you'd make to this.

service.pact.ts

import { resolve } from 'path';
import { MockService, Pact } from '@pact-foundation/pact';

export const SERVICE_PACT_PROVIDER = new Pact( {
    consumer: 'My Consumer'
    , provider: 'My Provider'
    , port: 3000
    , logLevel: 'error'
    , pactfileWriteMode: 'merge'
    , dir: resolve( process.cwd(), 'pact', 'pacts' )
} );

SERVICE_PACT_PROVIDER.mockService = new MockService(
    SERVICE_PACT_PROVIDER.opts.consumer
    , SERVICE_PACT_PROVIDER.opts.provider
    , SERVICE_PACT_PROVIDER.opts.port
    , SERVICE_PACT_PROVIDER.opts.host
    , SERVICE_PACT_PROVIDER.opts.ssl
    , SERVICE_PACT_PROVIDER.opts.pactfileWriteMode
);

globalSetup.ts

import { SERVICE_PACT_PROVIDER } from './service.pact';

const globalSetup = async (): Promise<void> => {
    await SERVICE_PACT_PROVIDER.setup();
};

export default globalSetup;

globalTeardown.ts

import { SERVICE_PACT_PROVIDER } from './service.pact';

const globalTeardown = async (): Promise<void> => {
    await SERVICE_PACT_PROVIDER.finalize();
};

export default globalTeardown;

package.json

{
    "name": "my-awesome-package",
    "version": "1.0.0",
    "scripts": {
        "test": "jest"
    },
    "jest": {
        "globalSetup": "<rootDir>/jest.globalSetup.ts",
        "globalTeardown": "<rootDir>/jest.globalTeardown.ts"
    }
}

@mefellows
Copy link
Member

Thanks.

You might also like to use https://github.com/pact-foundation/jest-pact which I think solves some of these problems too and makes Pact easier to use.

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

5 participants