Best JavaScript code snippet using pact-foundation-pact
application.specs.js
Source:application.specs.js
...186 it('should register the before hooks', () => {187 const name = 'connect'188 const gen = function * (next) { yield next }189 let app = new Application(settings)190 app.registerBeforeHook(name, gen)191 expect(app._beforeHooks[name]).to.eql(gen)192 })193 it('should throw without a name', () => {194 const gen = function * (next) { yield next }195 let app = new Application(settings)196 expect(() => {197 app.registerBeforeHook('', gen)198 }).to.throw(/#registerBeforeHook requires a method name/)199 })200 it('should if method does not allow before hook', () => {201 const name = 'something'202 const gen = function * (next) { yield next }203 let app = new Application(settings)204 expect(() => {205 app.registerBeforeHook(name, gen)206 }).to.throw(/#registerBeforeHook hook on method something is unsupported/)207 })208 it('should throw when a non-generator function is passed', () => {209 const name = 'connect'210 let app = new Application(settings)211 expect(() => {212 app.registerBeforeHook(name)213 }).to.throw(/#registerBeforeHook requires a generator/)214 expect(() => {215 app.registerBeforeHook(name, () => {})216 }).to.throw(/#registerBeforeHook requires a generator/)217 })218 })219 describe('#registerAfterHook', () => {220 it('should register the after hooks', () => {221 const name = 'connect'222 const gen = function * (next) { yield next }223 let app = new Application(settings)224 app.registerAfterHook(name, gen)225 expect(app._afterHooks[name]).to.eql(gen)226 })227 it('should throw without a name', () => {228 const gen = function * (next) { yield next }229 let app = new Application(settings)230 expect(() => {231 app.registerAfterHook('', gen)232 }).to.throw(/#registerAfterHook requires a method name/)233 })234 it('should if method does not allow after hook', () => {235 const name = 'something'236 const gen = function * (next) { yield next }237 let app = new Application(settings)238 expect(() => {239 app.registerAfterHook(name, gen)240 }).to.throw(/#registerAfterHook hook on method something is unsupported/)241 })242 it('should throw when a non-generator function is passed', () => {243 const name = 'connect'244 let app = new Application(settings)245 expect(() => {246 app.registerAfterHook(name)247 }).to.throw(/#registerAfterHook requires a generator/)248 expect(() => {249 app.registerAfterHook(name, () => {})250 }).to.throw(/#registerAfterHook requires a generator/)251 })252 })253 describe('#connect', () => {254 let app255 beforeEach(() => {256 app = new Application(settings)257 app._composedStack = sinon.stub()258 })259 it('should connect to a rabbit queue', function * () {260 yield app.connect()261 expect(amqplib.connect.called).to.be.true262 expect(app.connection).to.exist263 expect(app.connection).to.eql(connection)264 })265 it('should create a channel', function * () {266 yield app.connect()267 expect(connection.createChannel.called).to.be.true268 expect(app.channel).to.exist269 expect(app.channel).to.eql(channel)270 })271 it('should setup a consumer using the handler', function * () {272 yield app.connect()273 expect(channel.consume.called).to.be.true274 expect(channel.consume.calledWith(settings.queueName, app._composedStack)).to.be.true275 })276 it('should surface errors', function * () {277 let received = null278 const error = 'Test Error'279 connection.createChannel.rejects(error)280 try {281 yield app.connect()282 } catch (err) {283 received = err284 }285 expect(received).to.be.an('error')286 expect(received.toString()).to.contain(error)287 })288 context('when it has a before hook', function() {289 it('should call the before hook', function * () {290 const stub = sinon.stub()291 const gen = function * () { stub() }292 app.registerBeforeHook('connect', gen)293 yield app.connect()294 expect(stub.called).to.be.true295 expect(stub.calledBefore(amqplib.connect.called)).to.be.true296 })297 })298 context('when it has an after hook', function() {299 it('should call the after hook', function * () {300 const stub = sinon.stub()301 const gen = function * () { stub() }302 app.registerAfterHook('connect', gen)303 yield app.connect()304 expect(stub.called).to.be.true305 expect(stub.calledBefore(amqplib.connect.called)).to.be.true306 })307 })308 })309 describe('#close', () => {310 let app311 beforeEach(function * () {312 app = new Application(settings)313 app._composedStack = sinon.stub()314 yield app.connect()315 })316 it('should close the channel', function * () {317 yield app.close()318 expect(channel.close.called).to.be.true319 })320 it('should close the connection', function * () {321 yield app.close()322 expect(connection.close.called).to.be.true323 })324 context('when it has a before hook', () => {325 it('should call the before hook', function * () {326 const stub = sinon.stub()327 const gen = function * () { stub() }328 app.registerBeforeHook('close', gen)329 yield app.close()330 expect(stub.called).to.be.true331 })332 })333 context('when it has an after hook', () => {334 it('should call the after hook', function * () {335 const stub = sinon.stub()336 const gen = function * () { stub() }337 app.registerAfterHook('close', gen)338 yield app.close()339 expect(stub.called).to.be.true340 })341 })342 })...
application.js
Source:application.js
1'use strict'2const EventEmitter = require('events').EventEmitter3const assert = require('assert')4const co = require('co')5const compose = require('koa-compose')6const amqp = require('amqplib')7const isGenerator = require('./utils/is-generator')8const buildRabbitUrl = require('./utils/build-rabbit-url')9module.exports = Application10function Application(config) {11 if (!(this instanceof Application)) return new Application(config)12 assert(config.hosts, 'array of hosts are required')13 assert(config.username, 'rabbit username is required')14 assert(config.password, 'rabbit password is required')15 assert(config.queueName, 'queueName is required')16 this._config = config || {}17 this._beforeHooks = {}18 this._afterHooks = {}19 this.middleware = []20}21Object.setPrototypeOf(Application.prototype, EventEmitter.prototype)22Application.prototype.use = function(fn) {23 assert(isGenerator(fn), '#use requires a generator')24 this.middleware = this.middleware.concat(fn)25 return this26}27Application.prototype.get = function(key) {28 return key ? this._config[key] : this._config29}30Application.prototype.createContext = function(message) {31 return {32 app: this,33 message,34 onError: function(err) {35 this.err = err36 this.app.emit('error', err, this)37 }38 }39}40Application.prototype.listen = function(handler) {41 assert(isGenerator(handler), '#listen requires a generator')42 const mw = this.middleware.concat(handler)43 const composedMiddleware = co.wrap(compose(mw))44 this._composedStack = (message) => {45 const context = this.createContext(message)46 return composedMiddleware47 .call(context)48 .catch(context.onError.bind(context))49 }50 return this51}52Application.prototype.getStack = function() {53 assert.ok(this._composedStack, 'Stack not composed. You must call #listen first.')54 return this._composedStack55}56Application.prototype.registerBeforeHook = function(name, fn) {57 const supported = ['connect', 'close']58 assert(name, '#registerBeforeHook requires a method name')59 assert(supported.indexOf(name) >= 0, `#registerBeforeHook hook on method ${name} is unsupported`)60 assert(isGenerator(fn), '#registerBeforeHook requires a generator')61 this._beforeHooks[name] = fn62 return this63}64Application.prototype.registerAfterHook = function(name, fn) {65 const supported = ['connect', 'close']66 assert(name, '#registerAfterHook requires a method name')67 assert(supported.indexOf(name) >= 0, `#registerAfterHook hook on method ${name} is unsupported`)68 assert(isGenerator(fn), '#registerAfterHook requires a generator')69 this._afterHooks[name] = fn70 return this71}72Application.prototype.callBeforeHook = function * (name) {73 const hook = this._beforeHooks[name]74 if (typeof hook === 'function') yield hook()75}76Application.prototype.callAfterHook = function * (name) {77 const hook = this._afterHooks[name]78 if (typeof hook === 'function') yield hook()79}80Application.prototype.connect = function() {81 const self = this82 const hosts = this.get('hosts')83 const username = this.get('username')84 const password = this.get('password')85 const rabbitUrl = buildRabbitUrl(hosts, username, password)86 const queueName = this.get('queueName')87 return co(function * () {88 yield self.callBeforeHook('connect')89 self.connection = yield amqp.connect(rabbitUrl)90 self.channel = yield self.connection.createChannel()91 self.connection.on('close', function(err) {92 self.emit('connection:closed', err)93 })94 yield self.channel.consume(queueName, self._composedStack)95 yield self.callAfterHook('connect')96 }).catch((err) => { throw err })97}98Application.prototype.close = function(done) {99 const self = this100 return co(function * () {101 yield self.callBeforeHook('close')102 yield self.channel.close()103 yield self.connection.close()104 yield self.callAfterHook('close')105 }).catch((err) => { throw err })...
component.js
Source:component.js
...18 if ( this.ignoreFields ) {19 this.ignoreFields('stackId');20 }21 if ( this.registerBeforeHook ) {22 this.registerBeforeHook(this.save.bind(this), 'saveStack', 10);23 } else {24 throw new Error('Must pass in registerBeforeHook action')25 }26 const stack = get(this, 'stack');27 const choices = get(this, 'stacks');28 const def = choices.findBy('isDefault');29 set(this, 'newStack', get(this, 'store').createRecord({30 type: 'stack',31 name: ''32 }));33 if ( stack && choices.includes(stack) ) {34 set(this, 'create', false);35 } else if ( def ) {36 this.send('existingChanged', def);...
Using AI Code Generation
1const {Pact} = require('@pact-foundation/pact');2const path = require('path');3Pact.registerBeforeHook(() => {4 console.log('This is before hook');5});6describe('Test2', () => {7 it('should do something', () => {8 console.log('This is test');9 });10});11const {Pact} = require('@pact-foundation/pact');12const path = require('path');13Pact.registerAfterHook(() => {14 console.log('This is after hook');15});16describe('Test1', () => {17 it('should do something', () => {18 console.log('This is test');19 });20});
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2pact.registerBeforeHook(function() {3 console.log("before hook");4});5var pact = require('pact-foundation/pact-node');6pact.registerAfterHook(function() {7 console.log("after hook");8});9var pact = require('pact-foundation/pact-node');10pact.registerAfterEachHook(function() {11 console.log("after each hook");12});13var pact = require('pact-foundation/pact-node');14pact.registerBeforeEachHook(function() {15 console.log("before each hook");16});17var pact = require('pact-foundation/pact-node');18pact.registerAfterAllHook(function() {19 console.log("after all hook");20});21var pact = require('pact-foundation/pact-node');22pact.registerBeforeAllHook(function() {23 console.log("before all hook");24});25var pact = require('pact-foundation/pact-node');
Using AI Code Generation
1var pact = require('pact-foundation/pact-web');2describe('Pact', function() {3 before(function() {4 });5 beforeEach(function() {6 });7 after(function() {8 });9 afterEach(function() {10 });11 it('should pass', function() {12 expect(true).to.be.true;13 });14 it('should fail', function() {15 expect(false).to.be.true;16 });17});18var pact = require('pact-foundation/pact-web');19describe('Pact', function() {20 before(function() {21 });22 beforeEach(function() {23 });24 after(function() {25 });26 afterEach(function() {27 });28 it('should pass', function() {29 expect(true).to.be.true;30 });31 it('should fail', function() {32 expect(false).to.be.true;33 });34});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!