Best JavaScript code snippet using pact-foundation-pact
application.specs.js
Source:application.specs.js
...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 })343 context('on a connection `close` event', () => {344 it('should emit a `connection:closed` event', function * () {345 const app = new Application(settings)346 app._composedStack = sinon.stub()347 yield app.connect()348 const err = new Error('Connection Closed Test Error')349 const spy = sinon.spy()350 app.on('connection:closed', spy)351 connection.emit('close', err)...
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 })...
Migration.ts
Source:Migration.ts
1import { runSingleTask } from './tasks/runTask';2import { MigrationEmitter } from './events';3import type {4 Options,5 TaskResult,6 RegisterCreateTask,7 RegisterRemoveTask,8 RegisterRenameTask,9 RegisterTransformTask,10 RegisterAfterHook,11 Task,12 TaskError,13} from './types';14import { isPattern } from './utils';15import { getReporter } from './reporters';16import { VirtualFileSystem } from './VirtualFileSystem';17import { AfterHookFn } from './hooks';18export type RegisterMethods = {19 transform: RegisterTransformTask;20 rename: RegisterRenameTask;21 remove: RegisterRemoveTask;22 create: RegisterCreateTask;23 after: RegisterAfterHook;24};25export class Migration {26 options: Options;27 events: MigrationEmitter;28 fs: VirtualFileSystem;29 results: Array<TaskResult>;30 errors: Array<TaskError>;31 afterHooks: Array<AfterHookFn>;32 title: string | null;33 constructor(options: Options) {34 this.title = null;35 this.options = options;36 this.events = new MigrationEmitter(this);37 this.fs = new VirtualFileSystem({ cwd: options.cwd });38 this.results = [];39 this.errors = [];40 this.afterHooks = [];41 }42 runTask(task: Task) {43 const { taskErrors, taskResults } = runSingleTask(task, this);44 this.results.push(...taskResults);45 this.errors.push(...taskErrors);46 }47 transform: RegisterTransformTask = (title, pattern, transformFn) => {48 this.runTask({ type: 'transform', title, pattern, fn: transformFn });49 };50 rename: RegisterRenameTask = (title, pattern, renameFn) => {51 this.runTask({ type: 'rename', title, pattern, fn: renameFn });52 };53 remove: RegisterRemoveTask = (title, pattern, removeFn) => {54 this.runTask({ type: 'remove', title, pattern, fn: removeFn });55 };56 create: RegisterCreateTask = (title, patternOrCreateFn, createFn) => {57 let task: Task;58 if (isPattern(patternOrCreateFn)) {59 if (!createFn) {60 throw new Error(61 `When using a pattern for the second argument of the createTask function62You must supply a createFunction as the third argument`63 );64 }65 task = {66 type: 'create',67 title,68 pattern: patternOrCreateFn,69 fn: createFn,70 };71 } else {72 task = { type: 'create', title, fn: patternOrCreateFn };73 }74 this.runTask(task);75 };76 after: RegisterAfterHook = (afterFn: AfterHookFn) => {77 this.afterHooks.push(afterFn);78 };79 registerMethods: RegisterMethods = {80 transform: this.transform,81 rename: this.rename,82 remove: this.remove,83 create: this.create,84 after: this.after,85 };86 write() {87 this.fs.writeChangesToDisc();88 this.afterHooks.forEach((fn) => fn());89 }90 static init(options: Options): Migration {91 const migration = new Migration(options);92 const reporter = getReporter(options.reporter, {93 cwd: options.cwd,94 });95 reporter(migration);96 return migration;97 }...
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2pact.registerAfterHook(function (done) {3 console.log("after hook");4 done();5});6var pact = require('pact-foundation/pact-node');7pact.registerAfterHook(function (done) {8 console.log("after hook");9 done();10});
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2pact.registerAfterHook(function (result) {3 if (result.status === 0) {4 console.log('PACT test passed');5 } else {6 console.log('PACT test failed');7 }8});9var pact = require('pact-foundation/pact-node');10pact.registerAfterHook(function (result) {11 if (result.status === 0) {12 console.log('PACT test passed');13 } else {14 console.log('PACT test failed');15 }16});17var pact = require('pact-foundation/pact-node');18pact.registerAfterHook(function (result) {19 if (result.status === 0) {20 console.log('PACT test passed');21 } else {22 console.log('PACT test failed');23 }24});25var pact = require('pact-foundation/pact-node');26pact.registerAfterHook(function (result) {27 if (result.status === 0) {28 console.log('PACT test passed');29 } else {30 console.log('PACT test failed');31 }32});33var pact = require('pact-foundation/pact-node');34pact.registerAfterHook(function (result) {35 if (result.status === 0) {36 console.log('PACT test passed');37 } else {38 console.log('PACT test failed');39 }40});41var pact = require('pact-foundation/pact-node');42pact.registerAfterHook(function (result) {43 if (result.status === 0) {44 console.log('PACT test passed');45 } else {46 console.log('PACT test failed');47 }48});49var pact = require('pact-foundation/pact-node');
Using AI Code Generation
1const pactNode = require('@pact-foundation/pact-node');2const pactflow = require('@pact-foundation/pactflow');3pactNode.registerAfterHook(function (data) {4 console.log('This hook will be called after the pact verification is completed');5 console.log('data is', data);6 console.log('data.results is', data.results);7 console.log('data.results.length is', data.results.length);8 console.log('data.results[0] is', data.results[0]);9});10: an array of strings;11: a string;12: a boolean;13: an array of strings;14: an array of strings;15: a string;16: a boolean;17: an array of strings;
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!!