Best JavaScript code snippet using pact-foundation-pact
versioning_spec.js
Source:versioning_spec.js
1var should = require('should'),2 sinon = require('sinon'),3 Promise = require('bluebird'),4 // Stuff we are testing5 versioning = require('../../server/data/schema').versioning,6 db = require('../../server/data/db'),7 errors = require('../../server/errors'),8 sandbox = sinon.sandbox.create();9describe('Versioning', function () {10 afterEach(function () {11 sandbox.restore();12 });13 describe('getMigrationVersions', function () {14 it('should output a single item if the from and to versions are the same', function () {15 should.exist(versioning.getMigrationVersions);16 versioning.getMigrationVersions('003', '003').should.eql(['003']);17 versioning.getMigrationVersions('004', '004').should.eql(['004']);18 });19 it('should output an empty array if the toVersion is higher than the fromVersion', function () {20 versioning.getMigrationVersions('003', '002').should.eql([]);21 });22 it('should output all the versions between two versions', function () {23 versioning.getMigrationVersions('003', '004').should.eql(['003', '004']);24 versioning.getMigrationVersions('003', '005').should.eql(['003', '004', '005']);25 versioning.getMigrationVersions('003', '006').should.eql(['003', '004', '005', '006']);26 versioning.getMigrationVersions('010', '011').should.eql(['010', '011']);27 });28 });29 describe('getNewestDatabaseVersion', function () {30 it('should return the correct version', function () {31 var currentVersion = require('../../server/data/schema').defaultSettings.core.databaseVersion.defaultValue;32 // This function has an internal cache, so we call it twice.33 // First, to check that it fetches the correct version from default-settings.json.34 versioning.getNewestDatabaseVersion().should.eql(currentVersion);35 // Second, to check that it returns the same value from the cache.36 versioning.getNewestDatabaseVersion().should.eql(currentVersion);37 });38 });39 describe('getDatabaseVersion', function () {40 var knexStub, knexMock, queryMock;41 beforeEach(function () {42 queryMock = {43 where: sandbox.stub().returnsThis(),44 first: sandbox.stub()45 };46 knexMock = sandbox.stub().returns(queryMock);47 knexMock.schema = {48 hasTable: sandbox.stub()49 };50 // this MUST use sinon, not sandbox, see sinonjs/sinon#78151 knexStub = sinon.stub(db, 'knex', {get: function () { return knexMock; }});52 });53 afterEach(function () {54 knexStub.restore();55 });56 it('should throw error if settings table does not exist', function (done) {57 // Setup58 knexMock.schema.hasTable.returns(new Promise.resolve(false));59 // Execute60 versioning.getDatabaseVersion().then(function () {61 done('Should throw an error if the settings table does not exist');62 }).catch(function (err) {63 should.exist(err);64 (err instanceof errors.DatabaseNotPopulated).should.eql(true);65 knexStub.get.calledOnce.should.be.true();66 knexMock.schema.hasTable.calledOnce.should.be.true();67 knexMock.schema.hasTable.calledWith('settings').should.be.true();68 queryMock.where.called.should.be.false();69 queryMock.first.called.should.be.false();70 done();71 }).catch(done);72 });73 it('should lookup & return version, if settings table exists', function (done) {74 // Setup75 knexMock.schema.hasTable.returns(new Promise.resolve(true));76 queryMock.first.returns(new Promise.resolve({value: '001'}));77 // Execute78 versioning.getDatabaseVersion().then(function (version) {79 should.exist(version);80 version.should.eql('001');81 knexStub.get.calledTwice.should.be.true();82 knexMock.schema.hasTable.calledOnce.should.be.true();83 knexMock.schema.hasTable.calledWith('settings').should.be.true();84 queryMock.where.called.should.be.true();85 queryMock.first.called.should.be.true();86 done();87 }).catch(done);88 });89 it('should throw error if version does not exist', function (done) {90 // Setup91 knexMock.schema.hasTable.returns(new Promise.resolve(true));92 queryMock.first.returns(new Promise.resolve());93 // Execute94 versioning.getDatabaseVersion().then(function () {95 done('Should throw an error if version does not exist');96 }).catch(function (err) {97 should.exist(err);98 (err instanceof errors.DatabaseVersion).should.eql(true);99 knexStub.get.calledTwice.should.be.true();100 knexMock.schema.hasTable.calledOnce.should.be.true();101 knexMock.schema.hasTable.calledWith('settings').should.be.true();102 queryMock.where.called.should.be.true();103 queryMock.first.called.should.be.true();104 done();105 }).catch(done);106 });107 it('should throw error if version is not a number', function (done) {108 // Setup109 knexMock.schema.hasTable.returns(new Promise.resolve(true));110 queryMock.first.returns(new Promise.resolve('Eyjafjallajökull'));111 // Execute112 versioning.getDatabaseVersion().then(function () {113 done('Should throw an error if version is not a number');114 }).catch(function (err) {115 should.exist(err);116 (err instanceof errors.DatabaseVersion).should.eql(true);117 knexStub.get.calledTwice.should.be.true();118 knexMock.schema.hasTable.calledOnce.should.be.true();119 knexMock.schema.hasTable.calledWith('settings').should.be.true();120 queryMock.where.called.should.be.true();121 queryMock.first.called.should.be.true();122 done();123 }).catch(done);124 });125 });126 describe('setDatabaseVersion', function () {127 var knexStub, knexMock, queryMock;128 beforeEach(function () {129 queryMock = {130 where: sandbox.stub().returnsThis(),131 update: sandbox.stub().returns(new Promise.resolve())132 };133 knexMock = sandbox.stub().returns(queryMock);134 // this MUST use sinon, not sandbox, see sinonjs/sinon#781135 knexStub = sinon.stub(db, 'knex', {get: function () { return knexMock; }});136 });137 afterEach(function () {138 knexStub.restore();139 });140 it('should try to update the databaseVersion', function (done) {141 versioning.setDatabaseVersion().then(function () {142 knexStub.get.calledOnce.should.be.true();143 queryMock.where.called.should.be.true();144 queryMock.update.called.should.be.true();145 done();146 }).catch(done);147 });148 });149 describe('getUpdateTasks', function () {150 var logStub = {};151 it('`getUpdateFixturesTasks` returns empty array if no tasks are found', function () {152 versioning.getUpdateFixturesTasks('999', logStub).should.eql([]);153 });154 it('`getUpdateFixturesTasks` returns 8 items for 004', function () {155 versioning.getUpdateFixturesTasks('004', logStub).should.be.an.Array().with.lengthOf(8);156 });157 it('`getUpdateDatabaseTasks` returns empty array if no tasks are found', function () {158 versioning.getUpdateDatabaseTasks('999', logStub).should.eql([]);159 });160 it('`getUpdateDatabaseTasks` returns 5 items for 004', function () {161 versioning.getUpdateDatabaseTasks('004', logStub).should.be.an.Array().with.lengthOf(5);162 });163 });...
test.js
Source:test.js
1const chai = require('chai');2const sinon = require('sinon');3const expect = chai.expect;4const assert = chai.assert;5suite('Filter Stanza Parse', () =>6{7 const parse = require('../source/Meadow-Filter').parse;8 let queryStub = { addFilter: () => { }, setDistinct: () => { } };9 let queryMock;10 setup(() =>11 {12 queryMock = sinon.mock(queryStub);13 });14 //TODO: add more test cases...15 test('Filter by Value - EQ', () =>16 {17 // given18 const filterString = 'FBV~IDWaffle~EQ~123';19 queryMock.expects('addFilter').once().withArgs('IDWaffle', '123', '=', 'AND');20 // when21 parse(filterString, queryStub);22 // then23 queryMock.verify();24 });25 test('Filter by List - INN', () =>26 {27 // given28 const filterString = 'FBL~IDWaffle~INN~1,23,456';29 queryMock.expects('addFilter').once().withArgs('IDWaffle', [ '1', '23', '456' ], 'IN', 'AND');30 // when31 parse(filterString, queryStub);32 // then33 queryMock.verify();34 });35 test('Filter by List - NIN', () =>36 {37 // given38 const filterString = 'FBL~IDWaffle~NIN~1,23,456';39 queryMock.expects('addFilter').once().withArgs('IDWaffle', [ '1', '23', '456' ], 'NOT IN', 'AND');40 // when41 parse(filterString, queryStub);42 // then43 queryMock.verify();44 });45 test('Compound Filter', () =>46 {47 // given48 const filterString = 'FBL~IDWaffle~INN~1,23,456~FOP~0~(~0~FBV~Limit~GT~5~FBVOR~Limit~LT~0~FCP~0~)~0';49 queryMock.expects('addFilter').once().withArgs('IDWaffle', [ '1', '23', '456' ], 'IN', 'AND');50 queryMock.expects('addFilter').once().withArgs('', '', '(');51 queryMock.expects('addFilter').once().withArgs('Limit', '5', '>', 'AND');52 queryMock.expects('addFilter').once().withArgs('Limit', '0', '<', 'OR');53 queryMock.expects('addFilter').once().withArgs('', '', ')');54 // when55 parse(filterString, queryStub);56 // then57 queryMock.verify();58 });59 test('Filter distinct', () =>60 {61 // given62 const filterString = 'FDST~0~0~0';63 queryMock.expects('setDistinct').once().withArgs(true);64 // when65 parse(filterString, queryStub);66 // then67 queryMock.verify();68 });...
Using AI Code Generation
1const pact = require('@pact-foundation/pact-node');2const path = require('path');3const { Verifier } = require('@pact-foundation/pact');4const opts = {5 pactUrls: [path.resolve(process.cwd(), 'pacts', 'test1-test2.json')],6 consumerVersionSelectors: [{7 }]8};9describe('Pact Verification', () => {10 it('should validate the expectations of test1', () => {11 return new Verifier().verifyProvider(opts);12 });13});14module.exports = {15 pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],16 consumerVersionSelectors: [{17 }],18};19"scripts": {20 },21 "devDependencies": {
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!!