diff --git a/src/can-deploy/can-deploy.spec.ts b/src/can-deploy/can-deploy.spec.ts index ac404b88..46418567 100644 --- a/src/can-deploy/can-deploy.spec.ts +++ b/src/can-deploy/can-deploy.spec.ts @@ -14,137 +14,125 @@ import brokerMock from '../../test/integration/broker-mock'; const { expect } = chai; chai.use(chaiAsPromised); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('CanDeploy Spec', () => { - const PORT = Math.floor(Math.random() * 999) + 9000; - let server: http.Server; - let absolutePath: string; - let relativePath: string; - - before(() => - brokerMock(PORT).then((s) => { - logger.debug(`Pact Broker Mock listening on port: ${PORT}`); - server = s; - }) - ); - - after(() => server.close()); - - beforeEach(() => { - relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; - absolutePath = path.resolve(__dirname, '..', relativePath); - mkdirp.sync(absolutePath); - }); - - afterEach(() => { - if (fs.existsSync(absolutePath)) { - rimraf.sync(absolutePath); - } - }); - - describe('convertForSpawnBinary helper function', () => { - it('produces an array of SpawnArguments', () => { - const value = { pactBroker: 'some broker', pacticipants: [] }; - const result = CanDeploy.convertForSpawnBinary(value); - expect(result).to.be.an('array'); - expect(result.length).to.be.equal(1); - expect(result).to.be.deep.equal([{ pactBroker: 'some broker' }]); +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'CanDeploy Spec', + () => { + const PORT = Math.floor(Math.random() * 999) + 9000; + let server: http.Server; + let absolutePath: string; + let relativePath: string; + + before(() => + brokerMock(PORT).then((s) => { + logger.debug(`Pact Broker Mock listening on port: ${PORT}`); + server = s; + }) + ); + + after(() => server.close()); + + beforeEach(() => { + relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; + absolutePath = path.resolve(__dirname, '..', relativePath); + mkdirp.sync(absolutePath); }); - it('has version and participant in the right order', () => { - const result = CanDeploy.convertForSpawnBinary({ - pacticipants: [{ version: 'v2', name: 'one' }], - pactBroker: 'some broker', - pactBrokerUsername: 'username', - pactBrokerPassword: 'password', + afterEach(() => { + if (fs.existsSync(absolutePath)) { + rimraf.sync(absolutePath); + } + }); + + describe('convertForSpawnBinary helper function', () => { + it('produces an array of SpawnArguments', () => { + const value = { pactBroker: 'some broker', pacticipants: [] }; + const result = CanDeploy.convertForSpawnBinary(value); + expect(result).to.be.an('array'); + expect(result.length).to.be.equal(1); + expect(result).to.be.deep.equal([{ pactBroker: 'some broker' }]); }); - expect(result).to.eql([ - { + it('has version and participant in the right order', () => { + const result = CanDeploy.convertForSpawnBinary({ + pacticipants: [{ version: 'v2', name: 'one' }], pactBroker: 'some broker', pactBrokerUsername: 'username', pactBrokerPassword: 'password', - }, - { name: 'one' }, - { version: 'v2' }, - ]); - }); + }); - it('has latest tag and participant in the right order', () => { - const result = CanDeploy.convertForSpawnBinary({ - pacticipants: [{ name: 'two', latest: 'SOME_TAG' }], - pactBroker: 'some broker', + expect(result).to.eql([ + { + pactBroker: 'some broker', + pactBrokerUsername: 'username', + pactBrokerPassword: 'password', + }, + { name: 'one' }, + { version: 'v2' }, + ]); }); - expect(result).to.eql([ - { + it('has latest tag and participant in the right order', () => { + const result = CanDeploy.convertForSpawnBinary({ + pacticipants: [{ name: 'two', latest: 'SOME_TAG' }], pactBroker: 'some broker', - }, - { name: 'two' }, - { latest: 'SOME_TAG' }, - ]); - }); + }); - it("understands 'true' for latest", () => { - const result = CanDeploy.convertForSpawnBinary({ - pacticipants: [{ name: 'two', latest: true }], - pactBroker: 'some broker', + expect(result).to.eql([ + { + pactBroker: 'some broker', + }, + { name: 'two' }, + { latest: 'SOME_TAG' }, + ]); }); - expect(result).to.eql([ - { + it("understands 'true' for latest", () => { + const result = CanDeploy.convertForSpawnBinary({ + pacticipants: [{ name: 'two', latest: true }], pactBroker: 'some broker', - }, - { name: 'two' }, - { latest: 'PACT_NODE_NO_VALUE' }, - ]); - }); - }); + }); - context('when invalid options are set', () => { - it('should fail with an Error when not given pactBroker', () => { - expect(() => canDeployFactory({} as CanDeployOptions)).to.throw(Error); + expect(result).to.eql([ + { + pactBroker: 'some broker', + }, + { name: 'two' }, + { latest: 'PACT_NODE_NO_VALUE' }, + ]); + }); }); - it('should fail with an error when there are no paticipants', () => { - expect(() => - canDeployFactory({ - pactBroker: 'http://localhost', - pacticipants: [], - }) - ).to.throw(Error); - }); - }); + context('when invalid options are set', () => { + it('should fail with an Error when not given pactBroker', () => { + expect(() => canDeployFactory({} as CanDeployOptions)).to.throw(Error); + }); - context('when valid options are set', () => { - it('should return a CanDeploy object when given the correct arguments', () => { - const c = canDeployFactory({ - pactBroker: 'http://localhost', - pacticipants: [{ name: 'two', version: '2' }], + it('should fail with an error when there are no paticipants', () => { + expect(() => + canDeployFactory({ + pactBroker: 'http://localhost', + pacticipants: [], + }) + ).to.throw(Error); }); - expect(c).to.be.ok; - expect(c.canDeploy).to.be.a('function'); }); - }); - - context('candeploy function', () => { - it('should return success with a table result deployable true', (done) => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'Foo', version: '4' }], - }; - const ding = canDeployFactory(opts); - - ding.canDeploy().then((results) => { - expect(results).not.to.be.null; - done(); + + context('when valid options are set', () => { + it('should return a CanDeploy object when given the correct arguments', () => { + const c = canDeployFactory({ + pactBroker: 'http://localhost', + pacticipants: [{ name: 'two', version: '2' }], + }); + expect(c).to.be.ok; + expect(c.canDeploy).to.be.a('function'); }); }); - context('with latest true', () => { + context('candeploy function', () => { it('should return success with a table result deployable true', (done) => { const opts: CanDeployOptions = { pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'Foo', latest: true }], + pacticipants: [{ name: 'Foo', version: '4' }], }; const ding = canDeployFactory(opts); @@ -154,38 +142,96 @@ chai.use(chaiAsPromised); }); }); - it('should throw an error with a table result deployable false', () => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'FooFail', latest: true }], - }; - const ding = canDeployFactory(opts); + context('with latest true', () => { + it('should return success with a table result deployable true', (done) => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'Foo', latest: true }], + }; + const ding = canDeployFactory(opts); + + ding.canDeploy().then((results) => { + expect(results).not.to.be.null; + done(); + }); + }); - return ding - .canDeploy() - .then(() => expect.fail()) - .catch((message) => expect(message).not.be.null); + it('should throw an error with a table result deployable false', () => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'FooFail', latest: true }], + }; + const ding = canDeployFactory(opts); + + return ding + .canDeploy() + .then(() => expect.fail()) + .catch((message) => expect(message).not.be.null); + }); }); - }); - context('with latest a string', () => { - it('should return success with a table result deployable true', (done) => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'Foo', latest: 'tag' }], - }; - const ding = canDeployFactory(opts); + context('with latest a string', () => { + it('should return success with a table result deployable true', (done) => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'Foo', latest: 'tag' }], + }; + const ding = canDeployFactory(opts); + + ding.canDeploy().then((results) => { + expect(results).not.to.be.null; + done(); + }); + }); - ding.canDeploy().then((results) => { - expect(results).not.to.be.null; - done(); + it('should throw an error with a table result deployable false', () => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'FooFail', latest: 'tag' }], + }; + const ding = canDeployFactory(opts); + + return ding + .canDeploy() + .then(() => expect.fail()) + .catch((message) => expect(message).not.be.null); + }); + }); + + context('with latest a string, and a to', () => { + it('should return success with a table result deployable true', (done) => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'Foo', latest: 'tag' }], + to: 'prod', + }; + const ding = canDeployFactory(opts); + + ding.canDeploy().then((results) => { + expect(results).not.to.be.null; + done(); + }); + }); + + it('should throw an error with a table result deployable false', () => { + const opts: CanDeployOptions = { + pactBroker: `http://localhost:${PORT}`, + pacticipants: [{ name: 'FooFail', latest: 'tag' }], + to: 'prod', + }; + const ding = canDeployFactory(opts); + + return ding + .canDeploy() + .then(() => expect.fail()) + .catch((message) => expect(message).not.be.null); }); }); it('should throw an error with a table result deployable false', () => { const opts: CanDeployOptions = { pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'FooFail', latest: 'tag' }], + pacticipants: [{ name: 'FooFail', version: '4' }], }; const ding = canDeployFactory(opts); @@ -194,14 +240,12 @@ chai.use(chaiAsPromised); .then(() => expect.fail()) .catch((message) => expect(message).not.be.null); }); - }); - context('with latest a string, and a to', () => { - it('should return success with a table result deployable true', (done) => { + it('should return success with a json result deployable true', (done) => { const opts: CanDeployOptions = { pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'Foo', latest: 'tag' }], - to: 'prod', + pacticipants: [{ name: 'Foo', version: '4' }], + output: 'json', }; const ding = canDeployFactory(opts); @@ -211,11 +255,11 @@ chai.use(chaiAsPromised); }); }); - it('should throw an error with a table result deployable false', () => { + it('should throw an error with a json result deployable false', () => { const opts: CanDeployOptions = { pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'FooFail', latest: 'tag' }], - to: 'prod', + pacticipants: [{ name: 'FooFail', version: '4' }], + output: 'json', }; const ding = canDeployFactory(opts); @@ -225,46 +269,5 @@ chai.use(chaiAsPromised); .catch((message) => expect(message).not.be.null); }); }); - - it('should throw an error with a table result deployable false', () => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'FooFail', version: '4' }], - }; - const ding = canDeployFactory(opts); - - return ding - .canDeploy() - .then(() => expect.fail()) - .catch((message) => expect(message).not.be.null); - }); - - it('should return success with a json result deployable true', (done) => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'Foo', version: '4' }], - output: 'json', - }; - const ding = canDeployFactory(opts); - - ding.canDeploy().then((results) => { - expect(results).not.to.be.null; - done(); - }); - }); - - it('should throw an error with a json result deployable false', () => { - const opts: CanDeployOptions = { - pactBroker: `http://localhost:${PORT}`, - pacticipants: [{ name: 'FooFail', version: '4' }], - output: 'json', - }; - const ding = canDeployFactory(opts); - - return ding - .canDeploy() - .then(() => expect.fail()) - .catch((message) => expect(message).not.be.null); - }); - }); -}); + } +); diff --git a/src/message.spec.ts b/src/message.spec.ts index 1541c37b..f42fc833 100644 --- a/src/message.spec.ts +++ b/src/message.spec.ts @@ -9,134 +9,138 @@ import messageFactory from './message'; const { expect } = chai; chai.use(chaiAsPromised); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Message Spec', () => { - const validJSON = `{ "description": "a test mesage", "content": { "name": "Mary" } }`; +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Message Spec', + () => { + const validJSON = `{ "description": "a test mesage", "content": { "name": "Mary" } }`; - let absolutePath: string; - let relativePath: string; - beforeEach(() => { - relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; - absolutePath = path.resolve(__dirname, '..', relativePath); - mkdirp.sync(absolutePath); - }); + let absolutePath: string; + let relativePath: string; + beforeEach(() => { + relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; + absolutePath = path.resolve(__dirname, '..', relativePath); + mkdirp.sync(absolutePath); + }); - afterEach(() => { - if (fs.existsSync(absolutePath)) { - rimraf.sync(absolutePath); - } - }); + afterEach(() => { + if (fs.existsSync(absolutePath)) { + rimraf.sync(absolutePath); + } + }); - context('when invalid options are set', () => { - it('should throw an Error when not given any message content', () => { - expect(() => - messageFactory({ - consumer: 'a-consumer', - dir: absolutePath, - }) - ).to.throw(Error); + context('when invalid options are set', () => { + it('should throw an Error when not given any message content', () => { + expect(() => + messageFactory({ + consumer: 'a-consumer', + dir: absolutePath, + }) + ).to.throw(Error); + }); + + it('should throw an Error when not given a consumer', () => { + expect(() => + messageFactory({ + provider: 'a-provider', + dir: absolutePath, + content: validJSON, + }) + ).to.throw(Error); + }); + + it('should throw an Error when not given a provider', () => { + expect(() => + messageFactory({ + consumer: 'a-provider', + dir: absolutePath, + content: validJSON, + }) + ).to.throw(Error); + }); + + it('should throw an Error when given an invalid JSON document', () => { + expect(() => + messageFactory({ + consumer: 'some-consumer', + provider: 'a-provider', + dir: absolutePath, + content: `{ "unparseable" }`, + }) + ).to.throw(Error); + }); + + it('should throw an Error when not given a pact dir', () => { + expect(() => + messageFactory({ + consumer: 'a-consumer', + content: validJSON, + }) + ).to.throw(Error); + }); }); - it('should throw an Error when not given a consumer', () => { - expect(() => - messageFactory({ + context('when valid options are set', () => { + it('should return a message object when given the correct arguments', () => { + const message = messageFactory({ + consumer: 'some-consumer', provider: 'a-provider', dir: absolutePath, content: validJSON, - }) - ).to.throw(Error); - }); + }); + expect(message).to.be.a('object'); + expect(message).to.respondTo('createMessage'); + }); - it('should throw an Error when not given a provider', () => { - expect(() => - messageFactory({ - consumer: 'a-provider', + it('should return a promise when calling createMessage', () => { + const promise = messageFactory({ + consumer: 'some-consumer', + provider: 'a-provider', dir: absolutePath, content: validJSON, - }) - ).to.throw(Error); - }); + }).createMessage(); + expect(promise).to.ok; + return expect(promise).to.eventually.be.fulfilled; + }); - it('should throw an Error when given an invalid JSON document', () => { - expect(() => - messageFactory({ + it("should create a new directory if the directory specified doesn't exist yet", () => { + const dir = path.resolve(absolutePath, 'create'); + return messageFactory({ consumer: 'some-consumer', provider: 'a-provider', - dir: absolutePath, - content: `{ "unparseable" }`, - }) - ).to.throw(Error); - }); - - it('should throw an Error when not given a pact dir', () => { - expect(() => - messageFactory({ - consumer: 'a-consumer', + dir, content: validJSON, }) - ).to.throw(Error); - }); - }); - - context('when valid options are set', () => { - it('should return a message object when given the correct arguments', () => { - const message = messageFactory({ - consumer: 'some-consumer', - provider: 'a-provider', - dir: absolutePath, - content: validJSON, + .createMessage() + .then(() => expect(fs.existsSync(dir)).to.be.true); }); - expect(message).to.be.a('object'); - expect(message).to.respondTo('createMessage'); - }); - - it('should return a promise when calling createMessage', () => { - const promise = messageFactory({ - consumer: 'some-consumer', - provider: 'a-provider', - dir: absolutePath, - content: validJSON, - }).createMessage(); - expect(promise).to.ok; - return expect(promise).to.eventually.be.fulfilled; - }); - it("should create a new directory if the directory specified doesn't exist yet", () => { - const dir = path.resolve(absolutePath, 'create'); - return messageFactory({ - consumer: 'some-consumer', - provider: 'a-provider', - dir, - content: validJSON, - }) - .createMessage() - .then(() => expect(fs.existsSync(dir)).to.be.true); - }); + it('should return an absolute path when a relative one is given', () => { + const dir = path.join(relativePath, 'create'); + expect( + messageFactory({ + consumer: 'some-consumer', + provider: 'a-provider', + dir, + content: validJSON, + }).options.dir + ).to.equal(path.resolve(__dirname, '..', dir)); + }); - it('should return an absolute path when a relative one is given', () => { - const dir = path.join(relativePath, 'create'); - expect( - messageFactory({ + it('should create a new directory with a relative path', () => { + const dir = path.join(relativePath, 'create'); + return messageFactory({ consumer: 'some-consumer', provider: 'a-provider', dir, content: validJSON, - }).options.dir - ).to.equal(path.resolve(__dirname, '..', dir)); - }); - - it('should create a new directory with a relative path', () => { - const dir = path.join(relativePath, 'create'); - return messageFactory({ - consumer: 'some-consumer', - provider: 'a-provider', - dir, - content: validJSON, - }) - .createMessage() - .then( - () => - expect(fs.existsSync(path.resolve(__dirname, '..', dir))).to.be.true - ); + }) + .createMessage() + .then( + () => + expect(fs.existsSync(path.resolve(__dirname, '..', dir))).to.be + .true + ); + }); }); - }); -}); + } +); diff --git a/src/pact-standalone.spec.ts b/src/pact-standalone.spec.ts index 31d46453..8e326976 100644 --- a/src/pact-standalone.spec.ts +++ b/src/pact-standalone.spec.ts @@ -8,128 +8,133 @@ const { expect } = chai; const basePath = pactEnvironment.cwd; // Needs to stay a function and not an arrow function to access mocha 'this' context -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Pact Standalone', function forMocha() { - // Set timeout to 10 minutes because downloading binaries might take a while. - this.timeout(600000); - - let pact: PactStandalone; - - it('should return an object with cwd, file and fullPath properties that is platform specific', () => { - pact = standalone(); - expect(pact).to.be.an('object'); - expect(pact.cwd).to.be.ok; - expect(pact.brokerPath).to.contain('pact-broker'); - expect(pact.brokerFullPath).to.contain('pact-broker'); - expect(pact.mockServicePath).to.contain('pact-mock-service'); - expect(pact.mockServiceFullPath).to.contain('pact-mock-service'); - expect(pact.stubPath).to.contain('pact-stub-service'); - expect(pact.stubFullPath).to.contain('pact-stub-service'); - expect(pact.verifierPath).to.contain('pact-provider-verifier'); - expect(pact.verifierFullPath).to.contain('pact-provider-verifier'); - expect(pact.pactPath).to.contain('pact'); - expect(pact.pactFullPath).to.contain('pact'); - expect(pact.pactflowPath).to.contain('pactflow'); - expect(pact.pactflowFullPath).to.contain('pactflow'); - }); - - it("should return the base directory of the project with 'cwd' (where the package.json file is)", () => { - expect(fs.existsSync(path.resolve(pact.cwd, 'package.json'))).to.be.true; - }); - - describe('Check if OS specific files are there', () => { - const tests = [ - ['darwin', 'arm64'], - ['darwin', 'x64'], - ['linux', 'arm64'], - ['linux', 'x64'], - ['win32', 'x64'], - ].filter(([platform]) => - process.env['ONLY_DOWNLOAD_PACT_FOR_WINDOWS'] - ? platform === 'win32' - : true - ); - - tests.forEach(([platform, arch]) => { - describe(`${platform} ${arch}`, () => { - beforeEach(() => { - pact = standalone(platform, arch); - }); +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Pact Standalone', + function forMocha() { + // Set timeout to 10 minutes because downloading binaries might take a while. + this.timeout(600000); + + let pact: PactStandalone; + + it('should return an object with cwd, file and fullPath properties that is platform specific', () => { + pact = standalone(); + expect(pact).to.be.an('object'); + expect(pact.cwd).to.be.ok; + expect(pact.brokerPath).to.contain('pact-broker'); + expect(pact.brokerFullPath).to.contain('pact-broker'); + expect(pact.mockServicePath).to.contain('pact-mock-service'); + expect(pact.mockServiceFullPath).to.contain('pact-mock-service'); + expect(pact.stubPath).to.contain('pact-stub-service'); + expect(pact.stubFullPath).to.contain('pact-stub-service'); + expect(pact.verifierPath).to.contain('pact-provider-verifier'); + expect(pact.verifierFullPath).to.contain('pact-provider-verifier'); + expect(pact.pactPath).to.contain('pact'); + expect(pact.pactFullPath).to.contain('pact'); + expect(pact.pactflowPath).to.contain('pactflow'); + expect(pact.pactflowFullPath).to.contain('pactflow'); + }); - it('broker relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.brokerPath))).to.be - .true; - }); + it("should return the base directory of the project with 'cwd' (where the package.json file is)", () => { + expect(fs.existsSync(path.resolve(pact.cwd, 'package.json'))).to.be.true; + }); - it('broker full path', () => { - expect(fs.existsSync(pact.brokerFullPath)).to.be.true; - }); + describe('Check if OS specific files are there', () => { + const tests = [ + ['darwin', 'arm64'], + ['darwin', 'x64'], + ['linux', 'arm64'], + ['linux', 'x64'], + ['win32', 'x64'], + ].filter(([platform]) => + process.env['ONLY_DOWNLOAD_PACT_FOR_WINDOWS'] + ? platform === 'win32' + : true + ); + + tests.forEach(([platform, arch]) => { + describe(`${platform} ${arch}`, () => { + beforeEach(() => { + pact = standalone(platform, arch); + }); - it('mock service relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.mockServicePath))).to - .be.true; - }); + it('broker relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.brokerPath))).to.be + .true; + }); - it('mock service full path', () => { - expect(fs.existsSync(pact.mockServiceFullPath)).to.be.true; - }); + it('broker full path', () => { + expect(fs.existsSync(pact.brokerFullPath)).to.be.true; + }); - it('stub relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.stubPath))).to.be - .true; - }); + it('mock service relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.mockServicePath))) + .to.be.true; + }); - it('stub full path', () => { - expect(fs.existsSync(pact.stubFullPath)).to.be.true; - }); + it('mock service full path', () => { + expect(fs.existsSync(pact.mockServiceFullPath)).to.be.true; + }); - it('provider verifier relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.verifierPath))).to.be - .true; - }); + it('stub relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.stubPath))).to.be + .true; + }); - it('provider verifier full path', () => { - expect(fs.existsSync(pact.verifierFullPath)).to.be.true; - }); + it('stub full path', () => { + expect(fs.existsSync(pact.stubFullPath)).to.be.true; + }); - it('pact relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.pactPath))).to.be - .true; - }); + it('provider verifier relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.verifierPath))).to + .be.true; + }); - it('pact full path', () => { - expect(fs.existsSync(pact.pactFullPath)).to.be.true; - }); + it('provider verifier full path', () => { + expect(fs.existsSync(pact.verifierFullPath)).to.be.true; + }); - it('pactflow relative path', () => { - expect(fs.existsSync(path.resolve(basePath, pact.pactflowPath))).to.be - .true; - }); + it('pact relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.pactPath))).to.be + .true; + }); - it('pactflow full path', () => { - expect(fs.existsSync(pact.pactflowFullPath)).to.be.true; - }); + it('pact full path', () => { + expect(fs.existsSync(pact.pactFullPath)).to.be.true; + }); - if (platform === 'win32') { - it("should add '.bat' to the end of the binary names", () => { - expect(pact.brokerPath).to.contain('pact-broker.bat'); - expect(pact.brokerFullPath).to.contain('pact-broker.bat'); - expect(pact.mockServicePath).to.contain('pact-mock-service.bat'); - expect(pact.mockServiceFullPath).to.contain( - 'pact-mock-service.bat' - ); - expect(pact.stubPath).to.contain('pact-stub-service.bat'); - expect(pact.stubFullPath).to.contain('pact-stub-service.bat'); - expect(pact.verifierPath).to.contain('pact-provider-verifier.bat'); - expect(pact.verifierFullPath).to.contain( - 'pact-provider-verifier.bat' - ); - expect(pact.pactPath).to.contain('pact.bat'); - expect(pact.pactFullPath).to.contain('pact.bat'); - expect(pact.pactflowPath).to.contain('pactflow.bat'); - expect(pact.pactflowFullPath).to.contain('pactflow.bat'); + it('pactflow relative path', () => { + expect(fs.existsSync(path.resolve(basePath, pact.pactflowPath))).to + .be.true; }); - } + + it('pactflow full path', () => { + expect(fs.existsSync(pact.pactflowFullPath)).to.be.true; + }); + + if (platform === 'win32') { + it("should add '.bat' to the end of the binary names", () => { + expect(pact.brokerPath).to.contain('pact-broker.bat'); + expect(pact.brokerFullPath).to.contain('pact-broker.bat'); + expect(pact.mockServicePath).to.contain('pact-mock-service.bat'); + expect(pact.mockServiceFullPath).to.contain( + 'pact-mock-service.bat' + ); + expect(pact.stubPath).to.contain('pact-stub-service.bat'); + expect(pact.stubFullPath).to.contain('pact-stub-service.bat'); + expect(pact.verifierPath).to.contain( + 'pact-provider-verifier.bat' + ); + expect(pact.verifierFullPath).to.contain( + 'pact-provider-verifier.bat' + ); + expect(pact.pactPath).to.contain('pact.bat'); + expect(pact.pactFullPath).to.contain('pact.bat'); + expect(pact.pactflowPath).to.contain('pactflow.bat'); + expect(pact.pactflowFullPath).to.contain('pactflow.bat'); + }); + } + }); }); }); - }); -}); + } +); diff --git a/src/pact.spec.ts b/src/pact.spec.ts index da34eaaf..bdeecdb6 100644 --- a/src/pact.spec.ts +++ b/src/pact.spec.ts @@ -8,264 +8,267 @@ import { ServerOptions } from '.'; const { expect } = chai; chai.use(chaiAsPromised); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Pact Spec', () => { - afterEach(() => pact.removeAllServers()); - - describe('Set Log Level', () => { - let originalLogLevel: any; - // Reset log level after the tests - before(() => { - originalLogLevel = pact.logLevel(); - }); - after(() => pact.logLevel(originalLogLevel)); - - context('when setting a log level', () => { - it("should be able to set log level 'trace'", () => { - pact.logLevel('trace'); - pact.logLevel(); +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Pact Spec', + () => { + afterEach(() => pact.removeAllServers()); + + describe('Set Log Level', () => { + let originalLogLevel: any; + // Reset log level after the tests + before(() => { + originalLogLevel = pact.logLevel(); }); + after(() => pact.logLevel(originalLogLevel)); - it("should be able to set log level 'debug'", () => { - pact.logLevel('debug'); - pact.logLevel(); - }); + context('when setting a log level', () => { + it("should be able to set log level 'trace'", () => { + pact.logLevel('trace'); + pact.logLevel(); + }); - it("should be able to set log level 'info'", () => { - pact.logLevel('info'); - pact.logLevel(); - }); + it("should be able to set log level 'debug'", () => { + pact.logLevel('debug'); + pact.logLevel(); + }); - it("should be able to set log level 'warn'", () => { - pact.logLevel('warn'); - pact.logLevel(); - }); + it("should be able to set log level 'info'", () => { + pact.logLevel('info'); + pact.logLevel(); + }); - it("should be able to set log level 'error'", () => { - pact.logLevel('error'); - pact.logLevel(); + it("should be able to set log level 'warn'", () => { + pact.logLevel('warn'); + pact.logLevel(); + }); + + it("should be able to set log level 'error'", () => { + pact.logLevel('error'); + pact.logLevel(); + }); }); }); - }); - describe('Create serverFactory', () => { - let dirPath: string; - const monkeypatchFile: string = path.resolve( - __dirname, - '../test/monkeypatch.rb' - ); - - beforeEach(() => { - dirPath = path.resolve( + describe('Create serverFactory', () => { + let dirPath: string; + const monkeypatchFile: string = path.resolve( __dirname, - `../.tmp/${Math.floor(Math.random() * 1000)}` + '../test/monkeypatch.rb' ); - }); - afterEach(() => { - try { - if (fs.statSync(dirPath).isDirectory()) { - fs.rmdirSync(dirPath); - } - } catch (e) { - /* any errors here are not a failed test */ - } - }); - - context('when no options are set', () => { - it('should use defaults and return serverFactory', () => { - const server = pact.createServer(); - expect(server).to.be.an('object'); - expect(server.options).to.be.an('object'); - expect(server.options).to.contain.all.keys([ - 'cors', - 'ssl', - 'host', - 'dir', - ]); - expect(server.start).to.be.a('function'); - expect(server.stop).to.be.a('function'); - expect(server.delete).to.be.a('function'); + beforeEach(() => { + dirPath = path.resolve( + __dirname, + `../.tmp/${Math.floor(Math.random() * 1000)}` + ); }); - }); - context('when user specifies valid options', () => { - it('should return serverFactory using specified options', () => { - const options = { - port: 9500, - host: 'localhost', - dir: dirPath, - ssl: true, - cors: true, - log: './log/log.txt', - spec: 1, - consumer: 'consumerName', - provider: 'providerName', - monkeypatch: monkeypatchFile, - }; - const server = pact.createServer(options); - expect(server).to.be.an('object'); - expect(server.options).to.be.an('object'); - expect(server.options.port).to.equal(options.port); - expect(server.options.host).to.equal(options.host); - expect(server.options.dir).to.equal(options.dir); - expect(server.options.ssl).to.equal(options.ssl); - expect(server.options.cors).to.equal(options.cors); - expect(server.options.log).to.equal(path.resolve(options.log)); - expect(server.options.spec).to.equal(options.spec); - expect(server.options.consumer).to.equal(options.consumer); - expect(server.options.provider).to.equal(options.provider); - expect(server.options.monkeypatch).to.equal(options.monkeypatch); + afterEach(() => { + try { + if (fs.statSync(dirPath).isDirectory()) { + fs.rmdirSync(dirPath); + } + } catch (e) { + /* any errors here are not a failed test */ + } }); - }); - context('when user specifies invalid port', () => { - it('should return an error on negative port number', () => { - expect(() => pact.createServer({ port: -42 })).to.throw(Error); + context('when no options are set', () => { + it('should use defaults and return serverFactory', () => { + const server = pact.createServer(); + expect(server).to.be.an('object'); + expect(server.options).to.be.an('object'); + expect(server.options).to.contain.all.keys([ + 'cors', + 'ssl', + 'host', + 'dir', + ]); + expect(server.start).to.be.a('function'); + expect(server.stop).to.be.a('function'); + expect(server.delete).to.be.a('function'); + }); }); - it('should return an error on non-integer', () => { - expect(() => { - pact.createServer({ port: 42.42 }); - }).to.throw(Error); + context('when user specifies valid options', () => { + it('should return serverFactory using specified options', () => { + const options = { + port: 9500, + host: 'localhost', + dir: dirPath, + ssl: true, + cors: true, + log: './log/log.txt', + spec: 1, + consumer: 'consumerName', + provider: 'providerName', + monkeypatch: monkeypatchFile, + }; + const server = pact.createServer(options); + expect(server).to.be.an('object'); + expect(server.options).to.be.an('object'); + expect(server.options.port).to.equal(options.port); + expect(server.options.host).to.equal(options.host); + expect(server.options.dir).to.equal(options.dir); + expect(server.options.ssl).to.equal(options.ssl); + expect(server.options.cors).to.equal(options.cors); + expect(server.options.log).to.equal(path.resolve(options.log)); + expect(server.options.spec).to.equal(options.spec); + expect(server.options.consumer).to.equal(options.consumer); + expect(server.options.provider).to.equal(options.provider); + expect(server.options.monkeypatch).to.equal(options.monkeypatch); + }); }); - it('should return an error on non-number', () => { - expect(() => - pact.createServer({ port: '99' } as unknown as ServerOptions) - ).to.throw(Error); - }); + context('when user specifies invalid port', () => { + it('should return an error on negative port number', () => { + expect(() => pact.createServer({ port: -42 })).to.throw(Error); + }); - it('should return an error on outside port range', () => { - expect(() => { - pact.createServer({ port: 99999 }); - }).to.throw(Error); - }); - }); + it('should return an error on non-integer', () => { + expect(() => { + pact.createServer({ port: 42.42 }); + }).to.throw(Error); + }); - context("when user specifies port that's currently in use", () => { - it('should return a port conflict error', () => { - pact.createServer({ port: 5100 }); - expect(() => pact.createServer({ port: 5100 })).to.throw(Error); - }); - }); + it('should return an error on non-number', () => { + expect(() => + pact.createServer({ port: '99' } as unknown as ServerOptions) + ).to.throw(Error); + }); - context('when user specifies invalid host', () => { - it('should return an error on non-string', () => { - expect(() => - pact.createServer({ host: 12 } as unknown as ServerOptions) - ).to.throw(Error); + it('should return an error on outside port range', () => { + expect(() => { + pact.createServer({ port: 99999 }); + }).to.throw(Error); + }); }); - }); - context('when user specifies invalid pact directory', () => { - it('should create the directory for us', () => { - pact.createServer({ dir: dirPath }); - expect(fs.statSync(dirPath).isDirectory()).to.be.true; + context("when user specifies port that's currently in use", () => { + it('should return a port conflict error', () => { + pact.createServer({ port: 5100 }); + expect(() => pact.createServer({ port: 5100 })).to.throw(Error); + }); }); - }); - context('when user specifies invalid ssl', () => { - it('should return an error on non-boolean', () => { - expect(() => - pact.createServer({ ssl: 1 } as unknown as ServerOptions) - ).to.throw(Error); + context('when user specifies invalid host', () => { + it('should return an error on non-string', () => { + expect(() => + pact.createServer({ host: 12 } as unknown as ServerOptions) + ).to.throw(Error); + }); }); - }); - context('when user specifies invalid cors', () => { - it('should return an error on non-boolean', () => { - expect(() => - pact.createServer({ cors: 1 } as unknown as ServerOptions) - ).to.throw(Error); + context('when user specifies invalid pact directory', () => { + it('should create the directory for us', () => { + pact.createServer({ dir: dirPath }); + expect(fs.statSync(dirPath).isDirectory()).to.be.true; + }); }); - }); - context('when user specifies invalid spec', () => { - it('should return an error on non-number', () => { - expect(() => - pact.createServer({ spec: '1' } as unknown as ServerOptions) - ).to.throw(Error); + context('when user specifies invalid ssl', () => { + it('should return an error on non-boolean', () => { + expect(() => + pact.createServer({ ssl: 1 } as unknown as ServerOptions) + ).to.throw(Error); + }); }); - it('should return an error on negative number', () => { - expect(() => { - pact.createServer({ spec: -12 }); - }).to.throw(Error); + context('when user specifies invalid cors', () => { + it('should return an error on non-boolean', () => { + expect(() => + pact.createServer({ cors: 1 } as unknown as ServerOptions) + ).to.throw(Error); + }); }); - it('should return an error on non-integer', () => { - expect(() => { - pact.createServer({ spec: 3.14 }); - }).to.throw(Error); - }); - }); + context('when user specifies invalid spec', () => { + it('should return an error on non-number', () => { + expect(() => + pact.createServer({ spec: '1' } as unknown as ServerOptions) + ).to.throw(Error); + }); - context('when user specifies invalid consumer name', () => { - it('should return an error on non-string', () => { - expect(() => - pact.createServer({ consumer: 1234 } as unknown as ServerOptions) - ).to.throw(Error); - }); - }); + it('should return an error on negative number', () => { + expect(() => { + pact.createServer({ spec: -12 }); + }).to.throw(Error); + }); - context('when user specifies invalid provider name', () => { - it('should return an error on non-string', () => { - expect(() => - pact.createServer({ provider: 2341 } as unknown as ServerOptions) - ).to.throw(Error); + it('should return an error on non-integer', () => { + expect(() => { + pact.createServer({ spec: 3.14 }); + }).to.throw(Error); + }); }); - }); - context('when user specifies invalid monkeypatch', () => { - it('should return an error on invalid path', () => { - expect(() => { - pact.createServer({ monkeypatch: 'some-ruby-file.rb' }); - }).to.throw(Error); + context('when user specifies invalid consumer name', () => { + it('should return an error on non-string', () => { + expect(() => + pact.createServer({ consumer: 1234 } as unknown as ServerOptions) + ).to.throw(Error); + }); }); - }); - }); - describe('List servers', () => { - context('when called and there are no servers', () => { - it('should return an empty list', () => { - expect(pact.listServers()).to.be.empty; + context('when user specifies invalid provider name', () => { + it('should return an error on non-string', () => { + expect(() => + pact.createServer({ provider: 2341 } as unknown as ServerOptions) + ).to.throw(Error); + }); }); - }); - context('when called and there are servers in list', () => { - it('should return a list of all servers', () => { - pact.createServer({ port: 1234 }); - pact.createServer({ port: 1235 }); - pact.createServer({ port: 1236 }); - expect(pact.listServers()).to.have.length(3); + context('when user specifies invalid monkeypatch', () => { + it('should return an error on invalid path', () => { + expect(() => { + pact.createServer({ monkeypatch: 'some-ruby-file.rb' }); + }).to.throw(Error); + }); }); }); - context('when server is removed', () => { - it('should update the list', () => { - pact.createServer({ port: 1234 }); - pact.createServer({ port: 1235 }); - return pact - .createServer({ port: 1236 }) - .delete() - .then(() => expect(pact.listServers()).to.have.length(2)); + describe('List servers', () => { + context('when called and there are no servers', () => { + it('should return an empty list', () => { + expect(pact.listServers()).to.be.empty; + }); }); - }); - }); - describe('Remove all servers', () => { - context( - 'when removeAll() is called and there are servers to remove', - () => { - it('should remove all servers', () => { + context('when called and there are servers in list', () => { + it('should return a list of all servers', () => { pact.createServer({ port: 1234 }); pact.createServer({ port: 1235 }); pact.createServer({ port: 1236 }); + expect(pact.listServers()).to.have.length(3); + }); + }); + + context('when server is removed', () => { + it('should update the list', () => { + pact.createServer({ port: 1234 }); + pact.createServer({ port: 1235 }); return pact - .removeAllServers() - .then(() => expect(pact.listServers()).to.be.empty); + .createServer({ port: 1236 }) + .delete() + .then(() => expect(pact.listServers()).to.have.length(2)); }); - } - ); - }); -}); + }); + }); + + describe('Remove all servers', () => { + context( + 'when removeAll() is called and there are servers to remove', + () => { + it('should remove all servers', () => { + pact.createServer({ port: 1234 }); + pact.createServer({ port: 1235 }); + pact.createServer({ port: 1236 }); + return pact + .removeAllServers() + .then(() => expect(pact.listServers()).to.be.empty); + }); + } + ); + }); + } +); diff --git a/src/publisher.spec.ts b/src/publisher.spec.ts index dea31e1a..bec793b0 100644 --- a/src/publisher.spec.ts +++ b/src/publisher.spec.ts @@ -13,122 +13,125 @@ import { PublisherOptions } from './types'; const { expect } = chai; chai.use(chaiAsPromised); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Publish Spec', () => { - const PORT = Math.floor(Math.random() * 999) + 9000; - const pactFile = path.resolve( - __dirname, - '../test/integration/me-they-success.json' - ); - let server: http.Server; - let absolutePath: string; - let relativePath: string; +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Publish Spec', + () => { + const PORT = Math.floor(Math.random() * 999) + 9000; + const pactFile = path.resolve( + __dirname, + '../test/integration/me-they-success.json' + ); + let server: http.Server; + let absolutePath: string; + let relativePath: string; - before(() => - brokerMock(PORT).then((s) => { - logger.debug(`Pact Broker Mock listening on port: ${PORT}`); - server = s; - }) - ); + before(() => + brokerMock(PORT).then((s) => { + logger.debug(`Pact Broker Mock listening on port: ${PORT}`); + server = s; + }) + ); - after(() => server.close()); + after(() => server.close()); - beforeEach(() => { - relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; - absolutePath = path.resolve(__dirname, '..', relativePath); - mkdirp.sync(absolutePath); - }); - - afterEach(() => { - if (fs.existsSync(absolutePath)) { - rimraf.sync(absolutePath); - } - }); - - context('when invalid options are set', () => { - it('should fail with an Error when not given pactBroker', () => { - expect(() => { - publisherFactory({ - pactFilesOrDirs: [absolutePath], - consumerVersion: '1.0.0', - } as PublisherOptions); - }).to.throw(Error); + beforeEach(() => { + relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; + absolutePath = path.resolve(__dirname, '..', relativePath); + mkdirp.sync(absolutePath); }); - it('should fail with an Error when not given consumerVersion', () => { - expect(() => { - publisherFactory({ - pactBroker: 'http://localhost', - pactFilesOrDirs: [absolutePath], - } as PublisherOptions); - }).to.throw(Error); + afterEach(() => { + if (fs.existsSync(absolutePath)) { + rimraf.sync(absolutePath); + } }); - it('should fail with an error when not given pactFilesOrDirs', () => { - expect(() => { - publisherFactory({ - pactBroker: 'http://localhost', - consumerVersion: '1.0.0', - } as PublisherOptions); - }).to.throw(Error); - }); + context('when invalid options are set', () => { + it('should fail with an Error when not given pactBroker', () => { + expect(() => { + publisherFactory({ + pactFilesOrDirs: [absolutePath], + consumerVersion: '1.0.0', + } as PublisherOptions); + }).to.throw(Error); + }); - it('should fail with an Error when given Pact paths that do not exist', () => { - expect(() => { - publisherFactory({ - pactBroker: 'http://localhost', - pactFilesOrDirs: ['test.json'], - consumerVersion: '1.0.0', - }); - }).to.throw(Error); - }); - }); + it('should fail with an Error when not given consumerVersion', () => { + expect(() => { + publisherFactory({ + pactBroker: 'http://localhost', + pactFilesOrDirs: [absolutePath], + } as PublisherOptions); + }).to.throw(Error); + }); - context('when valid options are set', () => { - it('should return an absolute path when a relative one is given', () => { - expect( - publisherFactory({ - pactBroker: 'http://localhost', - pactFilesOrDirs: [relativePath], - consumerVersion: '1.0.0', - }).options.pactFilesOrDirs[0] - ).to.be.equal(path.resolve(__dirname, '..', relativePath)); - }); + it('should fail with an error when not given pactFilesOrDirs', () => { + expect(() => { + publisherFactory({ + pactBroker: 'http://localhost', + consumerVersion: '1.0.0', + } as PublisherOptions); + }).to.throw(Error); + }); - it('should return a Publisher object when given the correct arguments', () => { - const p = publisherFactory({ - pactBroker: 'http://localhost', - pactFilesOrDirs: [pactFile], - consumerVersion: '1.0.0', + it('should fail with an Error when given Pact paths that do not exist', () => { + expect(() => { + publisherFactory({ + pactBroker: 'http://localhost', + pactFilesOrDirs: ['test.json'], + consumerVersion: '1.0.0', + }); + }).to.throw(Error); }); - expect(p).to.be.ok; - expect(p.publish).to.be.a('function'); }); - }); - context('when a bearer token is provided', () => { - context('and specifies a username or password', () => { - it('should fail with an error', () => { - expect(() => + context('when valid options are set', () => { + it('should return an absolute path when a relative one is given', () => { + expect( publisherFactory({ pactBroker: 'http://localhost', pactFilesOrDirs: [relativePath], consumerVersion: '1.0.0', - pactBrokerToken: '1234', - pactBrokerUsername: 'username', - pactBrokerPassword: '5678', - }) - ).to.throw(Error); + }).options.pactFilesOrDirs[0] + ).to.be.equal(path.resolve(__dirname, '..', relativePath)); + }); + + it('should return a Publisher object when given the correct arguments', () => { + const p = publisherFactory({ + pactBroker: 'http://localhost', + pactFilesOrDirs: [pactFile], + consumerVersion: '1.0.0', + }); + expect(p).to.be.ok; + expect(p.publish).to.be.a('function'); }); }); - it('should not fail', () => { - const p = publisherFactory({ - pactBroker: 'http://localhost', - pactFilesOrDirs: [pactFile], - consumerVersion: '1.0.0', - pactBrokerToken: '1234', + + context('when a bearer token is provided', () => { + context('and specifies a username or password', () => { + it('should fail with an error', () => { + expect(() => + publisherFactory({ + pactBroker: 'http://localhost', + pactFilesOrDirs: [relativePath], + consumerVersion: '1.0.0', + pactBrokerToken: '1234', + pactBrokerUsername: 'username', + pactBrokerPassword: '5678', + }) + ).to.throw(Error); + }); + }); + it('should not fail', () => { + const p = publisherFactory({ + pactBroker: 'http://localhost', + pactFilesOrDirs: [pactFile], + consumerVersion: '1.0.0', + pactBrokerToken: '1234', + }); + expect(p).to.be.ok; + expect(p.publish).to.be.a('function'); }); - expect(p).to.be.ok; - expect(p.publish).to.be.a('function'); }); - }); -}); + } +); diff --git a/src/server.spec.ts b/src/server.spec.ts index 0b60e029..e6f00453 100644 --- a/src/server.spec.ts +++ b/src/server.spec.ts @@ -11,360 +11,367 @@ chai.use(chaiAsPromised); const { expect } = chai; const rm = util.promisify(rimraf); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Server Spec', () => { - let server: any; - const monkeypatchFile: string = path.resolve( - __dirname, - '../test/monkeypatch.rb' - ); - - let absolutePath: string; - let relativePath: string; - - beforeEach(() => { - relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; - absolutePath = path.resolve(__dirname, '..', relativePath); - mkdirp.sync(absolutePath); - }); - - afterEach(async () => { - if (server) { - await server.delete(); - } - - if (fs.existsSync(absolutePath)) { - await rm(absolutePath); - } - }); - - const relativeSSLCertPath = 'test/ssl/server.crt'; - const absoluteSSLCertPath = path.resolve( - __dirname, - '..', - relativeSSLCertPath - ); - const relativeSSLKeyPath = 'test/ssl/server.key'; - const absoluteSSLKeyPath = path.resolve(__dirname, '..', relativeSSLKeyPath); - - describe('Start server', () => { - context('when no options are set', () => { - it('should start correctly with defaults', () => { - server = serverFactory(); - return expect(server.start()).to.eventually.be.fulfilled; - }); +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Server Spec', + () => { + let server: any; + const monkeypatchFile: string = path.resolve( + __dirname, + '../test/monkeypatch.rb' + ); + + let absolutePath: string; + let relativePath: string; + + beforeEach(() => { + relativePath = `.tmp/${Math.floor(Math.random() * 1000)}`; + absolutePath = path.resolve(__dirname, '..', relativePath); + mkdirp.sync(absolutePath); }); - context('when invalid options are set', () => { - it('should fail if custom ssl certs do not exist', () => { - expect(() => - serverFactory({ - ssl: true, - sslcert: 'does/not/exist', - sslkey: absoluteSSLKeyPath, - }) - ).to.throw(Error); - }); + afterEach(async () => { + if (server) { + await server.delete(); + } - it('should fail if custom ssl keys do not exist', () => { - expect(() => - serverFactory({ - ssl: true, - sslcert: absoluteSSLCertPath, - sslkey: 'does/not/exist', - }) - ).to.throw(Error); - }); - - it("should fail if custom ssl cert is set, but ssl key isn't", () => { - expect(() => - serverFactory({ - ssl: true, - sslcert: absoluteSSLCertPath, - }) - ).to.throw(Error); - }); - - it("should fail if custom ssl key is set, but ssl cert isn't", () => { - expect(() => - serverFactory({ - ssl: true, - sslkey: absoluteSSLKeyPath, - }) - ).to.throw(Error); - }); + if (fs.existsSync(absolutePath)) { + await rm(absolutePath); + } }); - context('when valid options are set', () => { - it('should start correctly when instance is delayed', () => { - server = serverFactory(); - - const waitForServerUp = server.__waitForServiceUp.bind(server); - return expect( - Promise.all([ - waitForServerUp(server.options), - new Promise((resolve) => { - setTimeout(resolve, 5000); - }).then(() => server.start()), - ]) - ).to.eventually.be.fulfilled; - }); - - it('should start correctly with ssl', () => { - server = serverFactory({ ssl: true }); - expect(server.options.ssl).to.equal(true); - return expect(server.start()).to.eventually.be.fulfilled; - }); - - it('should start correctly with custom ssl cert/key', () => { - server = serverFactory({ - ssl: true, - sslcert: absoluteSSLCertPath, - sslkey: absoluteSSLKeyPath, + const relativeSSLCertPath = 'test/ssl/server.crt'; + const absoluteSSLCertPath = path.resolve( + __dirname, + '..', + relativeSSLCertPath + ); + const relativeSSLKeyPath = 'test/ssl/server.key'; + const absoluteSSLKeyPath = path.resolve( + __dirname, + '..', + relativeSSLKeyPath + ); + + describe('Start server', () => { + context('when no options are set', () => { + it('should start correctly with defaults', () => { + server = serverFactory(); + return expect(server.start()).to.eventually.be.fulfilled; }); - expect(server.options.ssl).to.equal(true); - return expect(server.start()).to.eventually.be.fulfilled; }); - it('should start correctly with custom ssl cert/key but without specifying ssl flag', () => { - server = serverFactory({ - sslcert: absoluteSSLCertPath, - sslkey: absoluteSSLKeyPath, + context('when invalid options are set', () => { + it('should fail if custom ssl certs do not exist', () => { + expect(() => + serverFactory({ + ssl: true, + sslcert: 'does/not/exist', + sslkey: absoluteSSLKeyPath, + }) + ).to.throw(Error); }); - expect(server.options.ssl).to.equal(true); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it('should fail if custom ssl keys do not exist', () => { + expect(() => + serverFactory({ + ssl: true, + sslcert: absoluteSSLCertPath, + sslkey: 'does/not/exist', + }) + ).to.throw(Error); + }); - it('should start correctly with cors', () => { - server = serverFactory({ cors: true }); - expect(server.options.cors).to.equal(true); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it("should fail if custom ssl cert is set, but ssl key isn't", () => { + expect(() => + serverFactory({ + ssl: true, + sslcert: absoluteSSLCertPath, + }) + ).to.throw(Error); + }); - it('should start correctly with port', () => { - const port = Math.floor(Math.random() * 999) + 9000; - server = serverFactory({ port }); - expect(server.options.port).to.equal(port); - return expect(server.start()).to.eventually.be.fulfilled; + it("should fail if custom ssl key is set, but ssl cert isn't", () => { + expect(() => + serverFactory({ + ssl: true, + sslkey: absoluteSSLKeyPath, + }) + ).to.throw(Error); + }); }); - it('should start correctly with host', () => { - const host = 'localhost'; - server = serverFactory({ host }); - expect(server.options.host).to.equal(host); - return expect(server.start()).to.eventually.be.fulfilled; - }); + context('when valid options are set', () => { + it('should start correctly when instance is delayed', () => { + server = serverFactory(); + + const waitForServerUp = server.__waitForServiceUp.bind(server); + return expect( + Promise.all([ + waitForServerUp(server.options), + new Promise((resolve) => { + setTimeout(resolve, 5000); + }).then(() => server.start()), + ]) + ).to.eventually.be.fulfilled; + }); - it('should start correctly with spec version 1', () => { - server = serverFactory({ spec: 1 }); - expect(server.options.spec).to.equal(1); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with ssl', () => { + server = serverFactory({ ssl: true }); + expect(server.options.ssl).to.equal(true); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with spec version 2', () => { - server = serverFactory({ spec: 2 }); - expect(server.options.spec).to.equal(2); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with custom ssl cert/key', () => { + server = serverFactory({ + ssl: true, + sslcert: absoluteSSLCertPath, + sslkey: absoluteSSLKeyPath, + }); + expect(server.options.ssl).to.equal(true); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with log', () => { - const logPath = path.resolve(absolutePath, 'log.txt'); - server = serverFactory({ log: logPath }); - expect(server.options.log).to.equal(logPath); + it('should start correctly with custom ssl cert/key but without specifying ssl flag', () => { + server = serverFactory({ + sslcert: absoluteSSLCertPath, + sslkey: absoluteSSLKeyPath, + }); - return expect(server.start()).to.eventually.be.fulfilled; + expect(server.options.ssl).to.equal(true); + return expect(server.start()).to.eventually.be.fulfilled; + }); - // return expect(server.start()).to.eventually.be.fulfilled.then( - // () => void rm(logPath), - // ); - }); + it('should start correctly with cors', () => { + server = serverFactory({ cors: true }); + expect(server.options.cors).to.equal(true); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with consumer name', () => { - const consumerName = 'cName'; - server = serverFactory({ consumer: consumerName }); - expect(server.options.consumer).to.equal(consumerName); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with port', () => { + const port = Math.floor(Math.random() * 999) + 9000; + server = serverFactory({ port }); + expect(server.options.port).to.equal(port); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with provider name', () => { - const providerName = 'pName'; - server = serverFactory({ provider: providerName }); - expect(server.options.provider).to.equal(providerName); - return expect(server.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with host', () => { + const host = 'localhost'; + server = serverFactory({ host }); + expect(server.options.host).to.equal(host); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with monkeypatch', () => { - const s = serverFactory({ monkeypatch: monkeypatchFile }); - expect(s.options.monkeypatch).to.equal(monkeypatchFile); - return expect(s.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with spec version 1', () => { + server = serverFactory({ spec: 1 }); + expect(server.options.spec).to.equal(1); + return expect(server.start()).to.eventually.be.fulfilled; + }); - context('Paths', () => { - it('should start correctly with dir, absolute path', () => { - server = serverFactory({ dir: relativePath }); - expect(server.options.dir).to.equal(absolutePath); + it('should start correctly with spec version 2', () => { + server = serverFactory({ spec: 2 }); + expect(server.options.spec).to.equal(2); return expect(server.start()).to.eventually.be.fulfilled; }); - it('should start correctly with log, relative paths', () => { - const logPath = path.join(relativePath, 'log.txt'); + it('should start correctly with log', () => { + const logPath = path.resolve(absolutePath, 'log.txt'); server = serverFactory({ log: logPath }); - expect(server.options.log).to.equal(path.resolve(logPath)); + expect(server.options.log).to.equal(logPath); + return expect(server.start()).to.eventually.be.fulfilled; + + // return expect(server.start()).to.eventually.be.fulfilled.then( + // () => void rm(logPath), + // ); }); - it('should start correctly with custom ssl cert/key, relative paths', () => { - server = serverFactory({ - sslcert: relativeSSLCertPath, - sslkey: relativeSSLKeyPath, - }); - expect(server.options.sslcert).to.equal(absoluteSSLCertPath); - expect(server.options.sslkey).to.equal(absoluteSSLKeyPath); + it('should start correctly with consumer name', () => { + const consumerName = 'cName'; + server = serverFactory({ consumer: consumerName }); + expect(server.options.consumer).to.equal(consumerName); return expect(server.start()).to.eventually.be.fulfilled; }); - }); - context('File Write Mode', () => { - it("should start correctly with 'overwrite'", () => - expect( - serverFactory({ - pactFileWriteMode: 'overwrite', - }).start() - ).to.eventually.be.fulfilled); + it('should start correctly with provider name', () => { + const providerName = 'pName'; + server = serverFactory({ provider: providerName }); + expect(server.options.provider).to.equal(providerName); + return expect(server.start()).to.eventually.be.fulfilled; + }); - it("should start correctly with 'merge'", () => - expect( - serverFactory({ - pactFileWriteMode: 'merge', - }).start() - ).to.eventually.be.fulfilled); + it('should start correctly with monkeypatch', () => { + const s = serverFactory({ monkeypatch: monkeypatchFile }); + expect(s.options.monkeypatch).to.equal(monkeypatchFile); + return expect(s.start()).to.eventually.be.fulfilled; + }); - it("should start correctly with 'update'", () => - expect( - serverFactory({ - pactFileWriteMode: 'update', - }).start() - ).to.eventually.be.fulfilled); - }); + context('Paths', () => { + it('should start correctly with dir, absolute path', () => { + server = serverFactory({ dir: relativePath }); + expect(server.options.dir).to.equal(absolutePath); + return expect(server.start()).to.eventually.be.fulfilled; + }); - context('Log Level', () => { - it("should start correctly with 'debug'", () => - Promise.all([ - expect( - serverFactory({ - logLevel: 'debug', - }).start() - ).to.eventually.be.fulfilled, + it('should start correctly with log, relative paths', () => { + const logPath = path.join(relativePath, 'log.txt'); + server = serverFactory({ log: logPath }); + expect(server.options.log).to.equal(path.resolve(logPath)); + return expect(server.start()).to.eventually.be.fulfilled; + }); - expect( - serverFactory({ - logLevel: 'DEBUG', - } as unknown as ServerOptions).start() - ).to.eventually.be.fulfilled, - ])); + it('should start correctly with custom ssl cert/key, relative paths', () => { + server = serverFactory({ + sslcert: relativeSSLCertPath, + sslkey: relativeSSLKeyPath, + }); + expect(server.options.sslcert).to.equal(absoluteSSLCertPath); + expect(server.options.sslkey).to.equal(absoluteSSLKeyPath); + return expect(server.start()).to.eventually.be.fulfilled; + }); + }); - it("should start correctly with 'info'", () => - Promise.all([ + context('File Write Mode', () => { + it("should start correctly with 'overwrite'", () => expect( serverFactory({ - logLevel: 'info', + pactFileWriteMode: 'overwrite', }).start() - ).to.eventually.be.fulfilled, + ).to.eventually.be.fulfilled); + it("should start correctly with 'merge'", () => expect( serverFactory({ - logLevel: 'INFO', - } as unknown as ServerOptions).start() - ).to.eventually.be.fulfilled, - ])); - - it("should start correctly with 'warn'", () => - Promise.all([ - expect( - serverFactory({ - logLevel: 'warn', + pactFileWriteMode: 'merge', }).start() - ).to.eventually.be.fulfilled, - - expect( - serverFactory({ - logLevel: 'WARN', - } as unknown as ServerOptions).start() - ).to.eventually.be.fulfilled, - ])); + ).to.eventually.be.fulfilled); - it("should start correctly with 'error'", () => - Promise.all([ + it("should start correctly with 'update'", () => expect( serverFactory({ - logLevel: 'error', + pactFileWriteMode: 'update', }).start() - ).to.eventually.be.fulfilled, + ).to.eventually.be.fulfilled); + }); - expect( - serverFactory({ - logLevel: 'ERROR', - } as unknown as ServerOptions).start() - ).to.eventually.be.fulfilled, - ])); + context('Log Level', () => { + it("should start correctly with 'debug'", () => + Promise.all([ + expect( + serverFactory({ + logLevel: 'debug', + }).start() + ).to.eventually.be.fulfilled, + + expect( + serverFactory({ + logLevel: 'DEBUG', + } as unknown as ServerOptions).start() + ).to.eventually.be.fulfilled, + ])); + + it("should start correctly with 'info'", () => + Promise.all([ + expect( + serverFactory({ + logLevel: 'info', + }).start() + ).to.eventually.be.fulfilled, + + expect( + serverFactory({ + logLevel: 'INFO', + } as unknown as ServerOptions).start() + ).to.eventually.be.fulfilled, + ])); + + it("should start correctly with 'warn'", () => + Promise.all([ + expect( + serverFactory({ + logLevel: 'warn', + }).start() + ).to.eventually.be.fulfilled, + + expect( + serverFactory({ + logLevel: 'WARN', + } as unknown as ServerOptions).start() + ).to.eventually.be.fulfilled, + ])); + + it("should start correctly with 'error'", () => + Promise.all([ + expect( + serverFactory({ + logLevel: 'error', + }).start() + ).to.eventually.be.fulfilled, + + expect( + serverFactory({ + logLevel: 'ERROR', + } as unknown as ServerOptions).start() + ).to.eventually.be.fulfilled, + ])); + }); }); - }); - - it('should dispatch event when starting', (done) => { - server = serverFactory(); - server.once('start', () => done()); - server.start(); - }); - it('should change running state to true', () => { - server = serverFactory(); - return server.start().then(() => expect(server.__running).to.be.true); - }); - }); - - describe('Stop server', () => { - context('when already started', () => { - it('should stop running', () => { + it('should dispatch event when starting', (done) => { server = serverFactory(); - return server.start().then(() => server.stop()); + server.once('start', () => done()); + server.start(); }); - it('should dispatch event when stopping', (done) => { + it('should change running state to true', () => { server = serverFactory(); - server.once('stop', () => done()); - server.start().then(() => server.stop()); + return server.start().then(() => expect(server.__running).to.be.true); }); + }); - it('should change running state to false', () => { - server = serverFactory(); - return server - .start() - .then(() => server.stop()) - .then(() => expect(server.__running).to.be.false); + describe('Stop server', () => { + context('when already started', () => { + it('should stop running', () => { + server = serverFactory(); + return server.start().then(() => server.stop()); + }); + + it('should dispatch event when stopping', (done) => { + server = serverFactory(); + server.once('stop', () => done()); + server.start().then(() => server.stop()); + }); + + it('should change running state to false', () => { + server = serverFactory(); + return server + .start() + .then(() => server.stop()) + .then(() => expect(server.__running).to.be.false); + }); }); }); - }); - describe('Delete server', () => { - context('when already running', () => { - it('should stop & delete server', () => { - server = serverFactory(); - return server.start().then(() => server.delete()); - }); + describe('Delete server', () => { + context('when already running', () => { + it('should stop & delete server', () => { + server = serverFactory(); + return server.start().then(() => server.delete()); + }); - it('should dispatch event when deleting', (done) => { - server = serverFactory(); - server.once('delete', () => done()); - server.start().then(() => server.delete()); - }); + it('should dispatch event when deleting', (done) => { + server = serverFactory(); + server.once('delete', () => done()); + server.start().then(() => server.delete()); + }); - it('should change running state to false', () => { - server = serverFactory(); - return server - .start() - .then(() => server.delete()) - .then(() => expect(server.__running).to.be.false); + it('should change running state to false', () => { + server = serverFactory(); + return server + .start() + .then(() => server.delete()) + .then(() => expect(server.__running).to.be.false); + }); }); }); - }); -}); + } +); diff --git a/src/stub.spec.ts b/src/stub.spec.ts index 38c944b2..2c2a1894 100644 --- a/src/stub.spec.ts +++ b/src/stub.spec.ts @@ -7,224 +7,227 @@ import stubFactory from './stub'; chai.use(chaiAsPromised); const { expect } = chai; -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Stub Spec', () => { - let stub: any; - const validDefaults = { - pactUrls: [ - path.resolve(__dirname, '../test/integration/me-they-success.json'), - ], - }; - - afterEach(() => - stub - ? stub.delete().then(() => { - stub = null; - return stub; - }) - : null - ); - - describe('Start stub', () => { - context('when invalid options are set', () => { - it('should fail if custom ssl certs do not exist', () => { - expect(() => - stubFactory({ - ssl: true, - sslcert: 'does/not/exist', - sslkey: path.resolve(__dirname, '../test/ssl/server.key'), +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Stub Spec', + () => { + let stub: any; + const validDefaults = { + pactUrls: [ + path.resolve(__dirname, '../test/integration/me-they-success.json'), + ], + }; + + afterEach(() => + stub + ? stub.delete().then(() => { + stub = null; + return stub; }) - ).to.throw(Error); - }); + : null + ); + + describe('Start stub', () => { + context('when invalid options are set', () => { + it('should fail if custom ssl certs do not exist', () => { + expect(() => + stubFactory({ + ssl: true, + sslcert: 'does/not/exist', + sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + }) + ).to.throw(Error); + }); - it('should fail if custom ssl keys do not exist', () => { - expect(() => - stubFactory({ - ssl: true, - sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), - sslkey: 'does/not/exist', - }) - ).to.throw(Error); - }); + it('should fail if custom ssl keys do not exist', () => { + expect(() => + stubFactory({ + ssl: true, + sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), + sslkey: 'does/not/exist', + }) + ).to.throw(Error); + }); - it("should fail if custom ssl cert is set, but ssl key isn't", () => { - expect(() => - stubFactory({ - ssl: true, - sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), - }) - ).to.throw(Error); - }); + it("should fail if custom ssl cert is set, but ssl key isn't", () => { + expect(() => + stubFactory({ + ssl: true, + sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), + }) + ).to.throw(Error); + }); - it("should fail if custom ssl key is set, but ssl cert isn't", () => { - expect(() => - stubFactory({ - ssl: true, - sslkey: path.resolve(__dirname, '../test/ssl/server.key'), - }) - ).to.throw(Error); + it("should fail if custom ssl key is set, but ssl cert isn't", () => { + expect(() => + stubFactory({ + ssl: true, + sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + }) + ).to.throw(Error); + }); }); - }); - context('when valid options are set', () => { - let dirPath: string; + context('when valid options are set', () => { + let dirPath: string; - beforeEach(() => { - dirPath = path.resolve( - __dirname, - `../.tmp/${Math.floor(Math.random() * 1000)}` - ); - }); + beforeEach(() => { + dirPath = path.resolve( + __dirname, + `../.tmp/${Math.floor(Math.random() * 1000)}` + ); + }); - afterEach(() => { - try { - if (fs.statSync(dirPath).isDirectory()) { - fs.rmdirSync(dirPath); + afterEach(() => { + try { + if (fs.statSync(dirPath).isDirectory()) { + fs.rmdirSync(dirPath); + } + } catch (e) { + /* any errors here are not a failed test */ } - } catch (e) { - /* any errors here are not a failed test */ - } - }); - - it('should start correctly when instance is delayed', () => { - stub = stubFactory(validDefaults); - - const waitForStubUp = stub.__waitForServiceUp.bind(stub); - return Promise.all([ - waitForStubUp(stub.options), - new Promise((resolve) => { - setTimeout(resolve, 5000); - }).then(() => stub.start()), - ]); - }); + }); - it('should start correctly with valid pact URLs', () => { - stub = stubFactory(validDefaults); - return expect(stub.start()).to.eventually.be.fulfilled; - }); + it('should start correctly when instance is delayed', () => { + stub = stubFactory(validDefaults); - it('should start correctly with valid pact URLs with spaces in it', () => { - stub = stubFactory({ - pactUrls: [ - path.resolve( - __dirname, - '../test/integration/me-they-weird path-success.json' - ), - ], + const waitForStubUp = stub.__waitForServiceUp.bind(stub); + return Promise.all([ + waitForStubUp(stub.options), + new Promise((resolve) => { + setTimeout(resolve, 5000); + }).then(() => stub.start()), + ]); }); - return expect(stub.start()).to.eventually.be.fulfilled; - }); - it('should start correctly with ssl', () => { - stub = stubFactory({ ...validDefaults, ssl: true }); - expect(stub.options.ssl).to.equal(true); - return expect(stub.start()).to.eventually.be.fulfilled; - }); - - it('should start correctly with custom ssl cert/key', () => { - stub = stubFactory({ - ...validDefaults, - ssl: true, - sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), - sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + it('should start correctly with valid pact URLs', () => { + stub = stubFactory(validDefaults); + return expect(stub.start()).to.eventually.be.fulfilled; }); - expect(stub.options.ssl).to.equal(true); - return expect(stub.start()).to.eventually.be.fulfilled; - }); - it('should start correctly with custom ssl cert/key but without specifying ssl flag', () => { - stub = stubFactory({ - ...validDefaults, - sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), - sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + it('should start correctly with valid pact URLs with spaces in it', () => { + stub = stubFactory({ + pactUrls: [ + path.resolve( + __dirname, + '../test/integration/me-they-weird path-success.json' + ), + ], + }); + return expect(stub.start()).to.eventually.be.fulfilled; }); - expect(stub.options.ssl).to.equal(true); - return expect(stub.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with ssl', () => { + stub = stubFactory({ ...validDefaults, ssl: true }); + expect(stub.options.ssl).to.equal(true); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with cors', () => { - stub = stubFactory({ ...validDefaults, cors: true }); - expect(stub.options.cors).to.equal(true); - return expect(stub.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with custom ssl cert/key', () => { + stub = stubFactory({ + ...validDefaults, + ssl: true, + sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), + sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + }); + expect(stub.options.ssl).to.equal(true); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with port', () => { - const port = Math.floor(Math.random() * 999) + 9000; - stub = stubFactory({ ...validDefaults, port }); - expect(stub.options.port).to.equal(port); - return expect(stub.start()).to.eventually.be.fulfilled; - }); + it('should start correctly with custom ssl cert/key but without specifying ssl flag', () => { + stub = stubFactory({ + ...validDefaults, + sslcert: path.resolve(__dirname, '../test/ssl/server.crt'), + sslkey: path.resolve(__dirname, '../test/ssl/server.key'), + }); - it('should start correctly with host', () => { - const host = 'localhost'; - stub = stubFactory({ ...validDefaults, host }); - expect(stub.options.host).to.equal(host); - return expect(stub.start()).to.eventually.be.fulfilled; - }); + expect(stub.options.ssl).to.equal(true); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - it('should start correctly with log', () => { - const logPath = path.resolve(dirPath, 'log.txt'); - stub = stubFactory({ ...validDefaults, log: logPath }); - expect(stub.options.log).to.equal(logPath); - return expect(stub.start()).to.eventually.be.fulfilled; - }); - }); + it('should start correctly with cors', () => { + stub = stubFactory({ ...validDefaults, cors: true }); + expect(stub.options.cors).to.equal(true); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - it('should dispatch event when starting', (done) => { - stub = stubFactory(validDefaults); - stub.once('start', () => done()); - stub.start(); - }); + it('should start correctly with port', () => { + const port = Math.floor(Math.random() * 999) + 9000; + stub = stubFactory({ ...validDefaults, port }); + expect(stub.options.port).to.equal(port); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - it('should change running state to true', () => { - stub = stubFactory(validDefaults); - return stub.start().then(() => expect(stub.__running).to.be.true); - }); - }); + it('should start correctly with host', () => { + const host = 'localhost'; + stub = stubFactory({ ...validDefaults, host }); + expect(stub.options.host).to.equal(host); + return expect(stub.start()).to.eventually.be.fulfilled; + }); - describe('Stop stub', () => { - context('when already started', () => { - it('should stop running', () => { - stub = stubFactory(validDefaults); - return stub.start().then(() => stub.stop()); + it('should start correctly with log', () => { + const logPath = path.resolve(dirPath, 'log.txt'); + stub = stubFactory({ ...validDefaults, log: logPath }); + expect(stub.options.log).to.equal(logPath); + return expect(stub.start()).to.eventually.be.fulfilled; + }); }); - it('should dispatch event when stopping', (done) => { + it('should dispatch event when starting', (done) => { stub = stubFactory(validDefaults); - stub.once('stop', () => done()); - stub.start().then(() => stub.stop()); + stub.once('start', () => done()); + stub.start(); }); - it('should change running state to false', () => { + it('should change running state to true', () => { stub = stubFactory(validDefaults); - return stub - .start() - .then(() => stub.stop()) - .then(() => expect(stub.__running).to.be.false); + return stub.start().then(() => expect(stub.__running).to.be.true); }); }); - }); - describe('Delete stub', () => { - context('when already running', () => { - it('should stop & delete stub', () => { - stub = stubFactory(validDefaults); - return stub.start().then(() => stub.delete()); - }); + describe('Stop stub', () => { + context('when already started', () => { + it('should stop running', () => { + stub = stubFactory(validDefaults); + return stub.start().then(() => stub.stop()); + }); - it('should dispatch event when deleting', (done) => { - stub = stubFactory(validDefaults); - stub.once('delete', () => done()); - stub.start().then(() => stub.delete()); + it('should dispatch event when stopping', (done) => { + stub = stubFactory(validDefaults); + stub.once('stop', () => done()); + stub.start().then(() => stub.stop()); + }); + + it('should change running state to false', () => { + stub = stubFactory(validDefaults); + return stub + .start() + .then(() => stub.stop()) + .then(() => expect(stub.__running).to.be.false); + }); }); + }); - it('should change running state to false', () => { - stub = stubFactory(validDefaults); - return stub - .start() - .then(() => stub.delete()) - .then(() => expect(stub.__running).to.be.false); + describe('Delete stub', () => { + context('when already running', () => { + it('should stop & delete stub', () => { + stub = stubFactory(validDefaults); + return stub.start().then(() => stub.delete()); + }); + + it('should dispatch event when deleting', (done) => { + stub = stubFactory(validDefaults); + stub.once('delete', () => done()); + stub.start().then(() => stub.delete()); + }); + + it('should change running state to false', () => { + stub = stubFactory(validDefaults); + return stub + .start() + .then(() => stub.delete()) + .then(() => expect(stub.__running).to.be.false); + }); }); }); - }); -}); + } +); diff --git a/test/publisher.integration.spec.ts b/test/publisher.integration.spec.ts index 5c5d37ab..be2e3de2 100644 --- a/test/publisher.integration.spec.ts +++ b/test/publisher.integration.spec.ts @@ -9,91 +9,151 @@ import brokerMock from './integration/broker-mock'; const { expect } = chai; chai.use(chaiAsPromised); -(process.env['SKIP_STANDALONE'] === "true" ? describe.skip : describe )('Publish Spec', () => { - let server: http.Server; - const PORT = Math.floor(Math.random() * 999) + 9000; - const pactBrokerBaseUrl = `http://localhost:${PORT}`; - const authenticatedPactBrokerBaseUrl = `${pactBrokerBaseUrl}/auth`; - - before(() => - brokerMock(PORT).then((s) => { - logger.debug(`Pact Broker Mock listening on port: ${PORT}`); - server = s; - }) - ); - - after(() => server.close()); - - context('when publishing a to a broker', () => { - context('without authentication', () => { - context('and the Pact contract is valid', () => { - it('should successfully publish all Pacts', () => { - const publisher = publisherFactory({ - pactBroker: pactBrokerBaseUrl, - pactFilesOrDirs: [ - path.resolve( - __dirname, - 'integration/publish/publish-success.json' - ), - ], - consumerVersion: '1.0.0', - buildUrl: 'http://ci/build/1', +(process.env['SKIP_STANDALONE'] === 'true' ? describe.skip : describe)( + 'Publish Spec', + () => { + let server: http.Server; + const PORT = Math.floor(Math.random() * 999) + 9000; + const pactBrokerBaseUrl = `http://localhost:${PORT}`; + const authenticatedPactBrokerBaseUrl = `${pactBrokerBaseUrl}/auth`; + + before(() => + brokerMock(PORT).then((s) => { + logger.debug(`Pact Broker Mock listening on port: ${PORT}`); + server = s; + }) + ); + + after(() => server.close()); + + context('when publishing a to a broker', () => { + context('without authentication', () => { + context('and the Pact contract is valid', () => { + it('should successfully publish all Pacts', () => { + const publisher = publisherFactory({ + pactBroker: pactBrokerBaseUrl, + pactFilesOrDirs: [ + path.resolve( + __dirname, + 'integration/publish/publish-success.json' + ), + ], + consumerVersion: '1.0.0', + buildUrl: 'http://ci/build/1', + }); + + expect(publisher).to.be.a('object'); + expect(publisher).to.respondTo('publish'); + return expect(publisher.publish()).to.eventually.be.fulfilled; }); - expect(publisher).to.be.a('object'); - expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.fulfilled; + it('should successfully tag all Pacts with `test` and `latest`', () => { + const publisher = publisherFactory({ + pactBroker: pactBrokerBaseUrl, + pactFilesOrDirs: [ + path.resolve( + __dirname, + 'integration/publish/publish-success.json' + ), + ], + consumerVersion: '1.0.0', + tags: ['test', 'latest'], + }); + + expect(publisher).to.be.a('object'); + expect(publisher).to.respondTo('publish'); + return expect(publisher.publish()).to.eventually.be.fulfilled; + }); + }); + context('and the Pact contract is invalid', () => { + it('should report an unsuccessful push', () => { + const publisher = publisherFactory({ + pactBroker: pactBrokerBaseUrl, + pactFilesOrDirs: [ + path.resolve( + __dirname, + 'integration/publish/publish-fail.json' + ), + ], + consumerVersion: '1.0.0', + }); + + expect(publisher).to.be.a('object'); + expect(publisher).to.respondTo('publish'); + return expect(publisher.publish()).to.eventually.be.rejected; + }); }); + }); - it('should successfully tag all Pacts with `test` and `latest`', () => { - const publisher = publisherFactory({ - pactBroker: pactBrokerBaseUrl, - pactFilesOrDirs: [ - path.resolve( - __dirname, - 'integration/publish/publish-success.json' - ), - ], - consumerVersion: '1.0.0', - tags: ['test', 'latest'], + context('with authentication', () => { + context('and valid credentials', () => { + it('should return a sucessful promise', () => { + const publisher = publisherFactory({ + pactBroker: authenticatedPactBrokerBaseUrl, + pactFilesOrDirs: [ + path.resolve( + __dirname, + 'integration/publish/publish-success.json' + ), + ], + consumerVersion: '1.0.0', + pactBrokerUsername: 'foo', + pactBrokerPassword: 'bar', + }); + + expect(publisher).to.be.a('object'); + expect(publisher).to.respondTo('publish'); + return expect(publisher.publish()).to.eventually.be.fulfilled; }); + }); - expect(publisher).to.be.a('object'); - expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.fulfilled; + context('and invalid credentials', () => { + it('should return a rejected promise', () => { + const publisher = publisherFactory({ + pactBroker: authenticatedPactBrokerBaseUrl, + pactFilesOrDirs: [ + path.resolve( + __dirname, + 'integration/publish/publish-success.json' + ), + ], + consumerVersion: '1.0.0', + pactBrokerUsername: 'not', + pactBrokerPassword: 'right', + }); + + expect(publisher).to.be.a('object'); + expect(publisher).to.respondTo('publish'); + return expect(publisher.publish()).to.eventually.be.rejected; + }); }); }); - context('and the Pact contract is invalid', () => { - it('should report an unsuccessful push', () => { + }); + + context('when publishing a directory of Pacts to a Broker', () => { + context('and the directory contains only valid Pact contracts', () => { + it('should asynchronously send all Pacts to the Broker', () => { const publisher = publisherFactory({ pactBroker: pactBrokerBaseUrl, pactFilesOrDirs: [ - path.resolve(__dirname, 'integration/publish/publish-fail.json'), + path.resolve(__dirname, 'integration/publish/pactDirTests'), ], consumerVersion: '1.0.0', }); expect(publisher).to.be.a('object'); expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.rejected; + return expect(publisher.publish()).to.eventually.be.fulfilled; }); - }); - }); - context('with authentication', () => { - context('and valid credentials', () => { - it('should return a sucessful promise', () => { + it('should successfully tag all Pacts sent with `test` and `latest`', () => { const publisher = publisherFactory({ - pactBroker: authenticatedPactBrokerBaseUrl, + pactBroker: pactBrokerBaseUrl, pactFilesOrDirs: [ - path.resolve( - __dirname, - 'integration/publish/publish-success.json' - ), + path.resolve(__dirname, 'integration/publish/pactDirTests'), ], consumerVersion: '1.0.0', - pactBrokerUsername: 'foo', - pactBrokerPassword: 'bar', + tags: ['test', 'latest'], }); expect(publisher).to.be.a('object'); @@ -102,78 +162,24 @@ chai.use(chaiAsPromised); }); }); - context('and invalid credentials', () => { - it('should return a rejected promise', () => { + context('and the directory contains Pact and non-Pact contracts', () => { + it('should asynchronously send only the Pact contracts to the broker', () => { const publisher = publisherFactory({ - pactBroker: authenticatedPactBrokerBaseUrl, + pactBroker: pactBrokerBaseUrl, pactFilesOrDirs: [ path.resolve( __dirname, - 'integration/publish/publish-success.json' + 'integration/publish/pactDirTestsWithOtherStuff' ), ], consumerVersion: '1.0.0', - pactBrokerUsername: 'not', - pactBrokerPassword: 'right', }); expect(publisher).to.be.a('object'); expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.rejected; - }); - }); - }); - }); - - context('when publishing a directory of Pacts to a Broker', () => { - context('and the directory contains only valid Pact contracts', () => { - it('should asynchronously send all Pacts to the Broker', () => { - const publisher = publisherFactory({ - pactBroker: pactBrokerBaseUrl, - pactFilesOrDirs: [ - path.resolve(__dirname, 'integration/publish/pactDirTests'), - ], - consumerVersion: '1.0.0', - }); - - expect(publisher).to.be.a('object'); - expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.fulfilled; - }); - - it('should successfully tag all Pacts sent with `test` and `latest`', () => { - const publisher = publisherFactory({ - pactBroker: pactBrokerBaseUrl, - pactFilesOrDirs: [ - path.resolve(__dirname, 'integration/publish/pactDirTests'), - ], - consumerVersion: '1.0.0', - tags: ['test', 'latest'], - }); - - expect(publisher).to.be.a('object'); - expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.fulfilled; - }); - }); - - context('and the directory contains Pact and non-Pact contracts', () => { - it('should asynchronously send only the Pact contracts to the broker', () => { - const publisher = publisherFactory({ - pactBroker: pactBrokerBaseUrl, - pactFilesOrDirs: [ - path.resolve( - __dirname, - 'integration/publish/pactDirTestsWithOtherStuff' - ), - ], - consumerVersion: '1.0.0', + return expect(publisher.publish()).to.eventually.be.fulfilled; }); - - expect(publisher).to.be.a('object'); - expect(publisher).to.respondTo('publish'); - return expect(publisher.publish()).to.eventually.be.fulfilled; }); }); - }); -}); + } +);