How to use getCurrentVersion method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

initTo.js

Source: initTo.js Github

copy
1const bunyan = require('bunyan');
2const { expect } = require('code');
3const { afterEach, beforeEach, describe, it } = exports.lab = require('lab').script();
4const proxyquire = require('proxyquire').noCallThru();
5const sinon = require('sinon');
6
7const logTemplate = bunyan.createLogger({
8  name: 'test'
9});
10
11describe('arango/initTo', () => {
12  let bunyanMock;
13  let ensureDb;
14  let getCurrentVersion;
15  let init;
16  let initTo;
17  let migration1;
18  let migration2;
19
20  beforeEach(() => {
21    bunyanMock = sinon.mock(logTemplate);
22
23    ensureDb = sinon.stub().resolves();
24    getCurrentVersion = sinon.stub();
25    migration1 = {
26      rollback: sinon.stub().resolves(),
27      setup: sinon.stub().resolves(),
28      verify: sinon.stub().resolves()
29    };
30    migration2 = {
31      rollback: sinon.stub().resolves(),
32      setup: sinon.stub().resolves(),
33      verify: sinon.stub().resolves()
34    };
35    initTo = proxyquire(
36      '../../../../src/arango/init/initTo',
37      {
38        './ensureDb': ensureDb,
39        './getCurrentVersion': getCurrentVersion,
40        '../../logger': { get: () => logTemplate },
41        '../v1': {}
42      });
43  });
44
45  afterEach(() => {
46    bunyanMock.verify();
47    bunyanMock.restore();
48  });
49
50  it('exists', () => {
51    expect(initTo).to.exist();
52  });
53
54  it('is a function', () => {
55    expect(initTo).to.be.a.function();
56  });
57
58  describe('init to 1 from 0', () => {
59    beforeEach(() => {
60      getCurrentVersion.resolves(0);
61      initTo = proxyquire(
62        '../../../../src/arango/init/initTo',
63        {
64          './ensureDb': ensureDb,
65          './getCurrentVersion': getCurrentVersion,
66          '../../logger': { get: () => logTemplate },
67          '../v1': migration1
68        });
69      init = initTo(1);
70    });
71
72    it('exists', () => {
73      expect(init).to.exist();
74    });
75
76    it('is a function', () => {
77      expect(init).to.be.a.function();
78    });
79
80    describe('successful', () => {
81      beforeEach(async () => {
82        result = await init();
83      });
84
85      it('calls ensureDb', () => {
86        expect(ensureDb.called).to.be.true();
87      });
88
89      it('calls migration.setup', () => {
90        expect(migration1.setup.called).to.be.true();
91      });
92
93      it('calls migration.verify', () => {
94        expect(migration1.verify.called).to.be.true();
95      });
96
97      it('does not call rollback', () => {
98        expect(migration1.rollback.called).to.be.false();
99      });
100    });
101
102    describe('setup rejects', () => {
103      let error;
104
105      beforeEach(async () => {
106        migration1.setup.rejects(new Error());
107
108        bunyanMock.expects('error');
109
110        try {
111          await init();
112        } catch (e) {
113          error = e;
114        }
115      });
116
117      it('throws', () => {
118        expect(error).to.exist();
119        expect(error).to.be.an.instanceOf(Error);
120      })
121
122      it('does not call verify', () => {
123        expect(migration1.verify.called).to.be.false();
124      });
125
126      it('does not call rollback', () => {
127        expect(migration1.rollback.called).to.be.false();
128      });
129    });
130
131    describe('verify rejects', () => {
132      let error;
133
134      beforeEach(async () => {
135        migration1.verify.rejects(new Error());
136
137        bunyanMock.expects('error');
138
139        try {
140          await init();
141        } catch (e) {
142          error = e;
143        }
144      });
145
146      it('calls rollback', () => {
147        expect(migration1.rollback.called).to.be.true();
148      });
149    });
150  });
151
152  describe('init to 2 from 0', () => {
153    beforeEach(() => {
154      getCurrentVersion.resolves(0);
155      initTo = proxyquire(
156        '../../../../src/arango/init/initTo',
157        {
158          './ensureDb': ensureDb,
159          './getCurrentVersion': getCurrentVersion,
160          '../../logger': { get: () => logTemplate },
161          '../v1': migration1,
162          '../v2': migration2
163        });
164      init = initTo(2);
165    });
166
167    describe('successful', () => {
168      beforeEach(async () => {
169        result = await init();
170      });
171
172      it('calls ensureDb', () => {
173        expect(ensureDb.called).to.be.true();
174      });
175
176      it('calls migration.setup', () => {
177        expect(migration1.setup.called).to.be.true();
178        expect(migration2.setup.called).to.be.true();
179      });
180
181      it('calls migration.verify', () => {
182        expect(migration1.verify.called).to.be.true();
183        expect(migration2.verify.called).to.be.true();
184      });
185
186      it('does not call rollback', () => {
187        expect(migration1.rollback.called).to.be.false();
188        expect(migration2.rollback.called).to.be.false();
189      });
190    });
191
192    describe('v2 setup fails', () => {
193      let error;
194
195      beforeEach(async () => {
196        migration2.setup.rejects(new Error());
197
198        bunyanMock.expects('error');
199
200        try {
201          result = await init();
202        } catch (e) {
203          error = e;
204        }
205      });
206
207      it('calls each setup a maximum of once', () => {
208        expect(migration1.setup.calledOnce).to.be.true();
209        expect(migration2.setup.calledOnce).to.be.true();
210      });
211
212      it('calls migration.setup', () => {
213        expect(migration1.setup.called).to.be.true();
214        expect(migration2.setup.called).to.be.true();
215      });
216
217      it('calls migration.verify on migration 1 only', () => {
218        expect(migration1.verify.called).to.be.true();
219        expect(migration2.verify.called).to.be.false();
220      });
221
222      it('does call rollback only for migration 1', () => {
223        expect(migration1.rollback.called).to.be.true();
224        expect(migration2.rollback.called).to.be.false();
225      });
226    });
227
228    describe('v2 verify fails and rollback fails', () => {
229      let error;
230
231      beforeEach(async () => {
232        migration2.verify.rejects(new Error());
233        migration2.rollback.rejects(new Error);
234
235        bunyanMock.expects('error');
236
237        try {
238          result = await init();
239        } catch (e) {
240          error = e;
241        }
242      });
243
244      it('calls migration.setup', () => {
245        expect(migration1.setup.called).to.be.true();
246        expect(migration2.setup.called).to.be.true();
247      });
248
249      it('calls migration.verify', () => {
250        expect(migration1.verify.called).to.be.true();
251        expect(migration2.verify.called).to.be.true();
252      });
253
254      it('calls rollback on migration 2', () => {
255        expect(migration1.rollback.called).to.be.false();
256        expect(migration2.rollback.called).to.be.true();
257      });
258    });
259  });
260
261  describe('init to 1 from 1', () => {
262    beforeEach(async () => {
263      getCurrentVersion.resolves(1);
264      initTo = proxyquire(
265        '../../../../src/arango/init/initTo',
266        {
267          './ensureDb': ensureDb,
268          './getCurrentVersion': getCurrentVersion,
269          '../../logger': { get: () => logTemplate },
270          '../v1': migration1
271        });
272      init = initTo(1);
273      result = await init();
274    });
275
276    it('calls ensureDb', () => {
277      expect(ensureDb.called).to.be.true();
278    });
279
280    it('does not call migration.setup', () => {
281      expect(migration1.setup.called).to.be.false();
282    });
283
284    it('does not call migration.verify', () => {
285      expect(migration1.verify.called).to.be.false();
286    });
287
288    it('does not call rollback', () => {
289      expect(migration1.rollback.called).to.be.false();
290    });
291  });
292});
Full Screen

migrate.test.js

Source: migrate.test.js Github

copy
1/**
2 * Tests for the migration module
3 */
4
5const {
6    migrate,
7    requiresMigration,
8} = require('../../src/repo/migrate');
9
10jest.mock('../../src/repo/migrate/version');
11jest.mock('../../src/repo/model', () => ({
12    ClassModel: { create: jest.fn() },
13    Property: { create: jest.fn() },
14}));
15
16const { getCurrentVersion, getLoadVersion } = jest.requireActual('./../../src/repo/migrate/version');
17const _version = require('../../src/repo/migrate/version');
18
19describe('migrate', () => {
20    let db,
21        propertyMock,
22        modelMock,
23        createRecordMock;
24
25    beforeEach(() => {
26        createRecordMock = jest.fn();
27        const queryMock = jest.fn().mockReturnValue({
28            all: jest.fn().mockResolvedValue([{ conditions: [], count: null }]),
29            one: jest.fn(),
30        });
31        db = {
32            class: {
33                get: jest.fn().mockResolvedValue({
34                    create: createRecordMock,
35                }),
36            },
37            command: queryMock,
38            index: {
39                create: jest.fn(),
40            },
41            insert: jest.fn().mockReturnValue({
42                into: jest.fn().mockReturnValue({
43                    set: queryMock,
44                }),
45            }),
46            query: queryMock,
47            update: jest.fn().mockReturnValue({
48                set: queryMock,
49            }),
50        };
51        const model = require('../../src/repo/model');  // eslint-disable-line
52        propertyMock = model.Property.create;
53        modelMock = model.ClassModel.create;
54    });
55
56    afterEach(() => {
57        jest.clearAllMocks();
58    });
59
60    test('getCurrentVersion', async () => {
61        db.query.mockReturnValue({ all: jest.fn().mockResolvedValueOnce([{ version: '1.6.2' }]) });
62        const version = await getCurrentVersion(db);
63        expect(db.query).toHaveBeenCalledWith('SELECT * FROM SchemaHistory ORDER BY createdAt DESC LIMIT 1');
64        expect(version).toEqual('1.6.2');
65    });
66
67    test('getLoadVersion', () => {
68        const version = getLoadVersion();
69        expect(version).toHaveProperty('version');
70        expect(version.version).toEqual(expect.stringMatching(/^\d+\.\d+\.\d+$/));
71    });
72
73    describe('requiresMigration', () => {
74        test('compatible versions do not require migration', () => {
75            expect(requiresMigration('1.9.1', '1.9.2')).toBeFalsy();
76        });
77
78        test('minor version difference requires migration', () => {
79            expect(requiresMigration('1.9.1', '1.10.1')).toBeTruthy();
80        });
81
82        test('major version difference requires migration', () => {
83            expect(requiresMigration('1.9.1', '2.9.2')).toBeTruthy();
84        });
85    });
86
87    describe('migrate', () => {
88        test('1.6 to 1.7.0', async () => {
89            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.6.2');
90            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.7.0' });
91            await migrate(db);
92            expect(db.index.create).toHaveBeenCalledTimes(3);
93            expect(propertyMock).not.toHaveBeenCalled();
94            expect(modelMock).not.toHaveBeenCalled();
95            expect(db.query).not.toHaveBeenCalled();
96            expect(createRecordMock).toHaveBeenCalledTimes(1);
97        });
98
99        test('1.6 to 1.7.1', async () => {
100            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.6.2');
101            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.7.1' });
102            await migrate(db);
103            expect(db.index.create).toHaveBeenCalledTimes(3);
104            expect(propertyMock).not.toHaveBeenCalled();
105            expect(modelMock).not.toHaveBeenCalled();
106            expect(db.query).not.toHaveBeenCalled();
107            expect(createRecordMock).toHaveBeenCalledTimes(2);
108        });
109
110        test('1.7 to 1.8', async () => {
111            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.7.0');
112            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.8.0' });
113            await migrate(db);
114            expect(db.index.create).not.toHaveBeenCalled();
115            expect(propertyMock).toHaveBeenCalledTimes(1);
116            expect(modelMock).not.toHaveBeenCalled();
117            expect(db.query).not.toHaveBeenCalled();
118            expect(createRecordMock).toHaveBeenCalledTimes(1);
119        });
120
121        test('1.8 to 1.9', async () => {
122            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.8.0');
123            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.9.0' });
124            await migrate(db);
125            expect(db.query).toHaveBeenCalledTimes(20);
126            expect(db.class.get).toHaveBeenCalledTimes(3);
127            expect(propertyMock).toHaveBeenCalledTimes(4);
128            expect(modelMock).toHaveBeenCalledTimes(1);
129            expect(createRecordMock).toHaveBeenCalledTimes(1);
130        });
131
132        test('compatible no migration', async () => {
133            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.8.0');
134            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.8.1' });
135            await migrate(db);
136            expect(db.query).not.toHaveBeenCalled();
137            expect(db.class.get).not.toHaveBeenCalled();
138        });
139
140        test('error on no transition', async () => {
141            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.6.0');
142            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.8.1' });
143            await expect(migrate(db)).rejects.toMatchObject({ message: 'Unable to find migration scripts from 1.6.0 to 1.8.1' });
144            expect(db.query).not.toHaveBeenCalled();
145            expect(db.class.get).not.toHaveBeenCalled();
146        });
147
148        test('incompatible check only', async () => {
149            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.8.0');
150            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.9.1' });
151            await expect(migrate(db, { checkOnly: true })).rejects.toMatchObject({ message: 'Versions (1.8.0, 1.9.1) are not compatible and require migration' });
152            expect(db.query).not.toHaveBeenCalled();
153            expect(db.class.get).not.toHaveBeenCalled();
154        });
155
156        test('1.6 to 1.9', async () => {
157            _version.getCurrentVersion = jest.fn().mockResolvedValue('1.6.2');
158            _version.getLoadVersion = jest.fn().mockReturnValue({ version: '1.9.2' });
159            await migrate(db);
160            expect(createRecordMock).toHaveBeenCalledTimes(4); // logged 4 times
161            expect(db.query).toHaveBeenCalledTimes(20); // 1.8 to 1.9
162            expect(db.index.create).toHaveBeenCalledTimes(3); // 1.6 to 1.7
163            expect(propertyMock).toHaveBeenCalledTimes(5); // mixed
164        });
165
166        test('2.0 to latest', async () => {
167            _version.getCurrentVersion = jest.fn().mockResolvedValue('2.0.0');
168            _version.getLoadVersion = getLoadVersion; // use original load version
169            await migrate(db);
170            // no errors
171            expect(createRecordMock).toHaveBeenCalled();
172            expect(db.query).toHaveBeenCalled();
173            expect(db.index.create).toHaveBeenCalled();
174            expect(propertyMock).toHaveBeenCalled();
175        });
176    });
177});
178
Full Screen

CustomerAggregate.test.js

Source: CustomerAggregate.test.js Github

copy
1import CustomerAggregate from './CustomerAggregate'
2import { CUSTOMER_CREATED } from '../constants/events'
3import { CustomerCreated } from '../events/CustomerEvents'
4
5const customer = CustomerAggregate()
6
7const CUSTOMER_1_ID = '1234-5678-9012-3456'
8const CUSTOMER_1_NAME = 'Test Customer'
9const CUSTOMER_1_EMAIL = '[email protected]'
10const CUSTOMER_1_PASSWORD = 'test1234'
11
12const CUSTOMER_1_NAME_UPDATED = 'Test Customer Updated'
13
14it('should return state with version 0', () => {
15  //  we want no event history to test this case
16  const storedEvents = new Set()
17  let state = customer.loadFromHistory(storedEvents)
18  let version = customer.getCurrentVersion(state)
19  expect(version).toBe(0)
20})
21
22it('should register a customer', () => {
23  //  we want no event history to test this case
24  const storedEvents = new Set()
25  //  prepare aggregate with no event history (new aggregate)
26  let state = customer.loadFromHistory(storedEvents)
27  let version = customer.getCurrentVersion(state)
28  let uncommittedChanges = customer.getUncommittedChanges(state)
29  expect(version).toBe(0)
30  expect(uncommittedChanges.size).toBe(0)
31  //  register a customer
32  state = customer.register(state, CUSTOMER_1_ID, CUSTOMER_1_NAME, CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD)
33  version = customer.getCurrentVersion(state)
34  expect(state.created).toBe(0)
35  expect(state.active).toBe(0)
36  expect(state.customerId).toBe(CUSTOMER_1_ID)
37  expect(state.name).toBe(CUSTOMER_1_NAME)
38  expect(state.email).toBe(CUSTOMER_1_EMAIL)
39  expect(state.password).toBe(CUSTOMER_1_PASSWORD)
40  //  new changes are yet to be written to Event Store
41  //  therefore aggreagate version must not change
42  //  and applied change must be added to uncommittedChanges set
43  expect(version).toBe(0)
44  expect(uncommittedChanges.size).toBe(1)
45})
46
47it('should create a new customer', () => {
48  //  we want no event history to test this case
49  const storedEvents = new Set()
50  //  prepare aggregate with no event history (new aggregate)
51  let state = customer.loadFromHistory(storedEvents)
52  let version = customer.getCurrentVersion(state)
53  let uncommittedChanges = customer.getUncommittedChanges(state)
54  expect(version).toBe(0)
55  expect(uncommittedChanges.size).toBe(0)
56  //  create new customer
57  state = customer.create(state, CUSTOMER_1_ID, CUSTOMER_1_NAME, CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD)
58  version = customer.getCurrentVersion(state)
59  expect(state.created).toBe(1)
60  expect(state.active).toBe(1)
61  expect(state.customerId).toBe(CUSTOMER_1_ID)
62  expect(state.name).toBe(CUSTOMER_1_NAME)
63  expect(state.email).toBe(CUSTOMER_1_EMAIL)
64  expect(state.password).toBe(CUSTOMER_1_PASSWORD)
65  //  new changes are yet to be written to Event Store
66  //  therefore aggreagate version must not change
67  //  and applied change must be added to uncommittedChanges set
68  expect(version).toBe(0)
69  expect(uncommittedChanges.size).toBe(1)
70})
71
72it('should throw an error on create for an existing customer aggregate', () => {
73  //  create event history (that would be loaded from event store in real application)
74  const storedEvents = new Set()
75  storedEvents.add(CustomerCreated(CUSTOMER_1_ID, CUSTOMER_1_NAME, CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD))
76  //  prepare aggregate with no event history (new aggregate)
77  let state = customer.loadFromHistory(storedEvents)
78  let version = customer.getCurrentVersion(state)
79  let uncommittedChanges = customer.getUncommittedChanges(state)
80  expect(version).toBe(1)
81  expect(uncommittedChanges.size).toBe(0)
82  //  create new customer method on existing customer should throw an error
83  expect(() => {
84    customer.create(state, CUSTOMER_1_ID, CUSTOMER_1_NAME, CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD)
85  }).toThrow('can not create same customer more than once')
86})
87
88it('should update an existing customer aggregate', () => {
89  //  create event history (that would be loaded from event store in real application)
90  const storedEvents = new Set()
91  storedEvents.add(CustomerCreated(CUSTOMER_1_ID, CUSTOMER_1_NAME, CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD))
92  //  prepare aggregate with no event history (new aggregate)
93  let state = customer.loadFromHistory(storedEvents)
94  let version = customer.getCurrentVersion(state)
95  let uncommittedChanges = customer.getUncommittedChanges(state)
96  expect(version).toBe(1)
97  expect(uncommittedChanges.size).toBe(0)
98  customer.update(state, CUSTOMER_1_NAME_UPDATED)
99  expect(state.customerId).toBe(CUSTOMER_1_ID)
100  expect(state.name).toBe(CUSTOMER_1_NAME_UPDATED)
101})
102
103it('should throw an error on updating a non-existing customer aggregate', () => {
104  //  create event history (that would be loaded from event store in real application)
105  const storedEvents = new Set()
106  //  prepare aggregate with no event history (new aggregate)
107  let state = customer.loadFromHistory(storedEvents)
108  let version = customer.getCurrentVersion(state)
109  let uncommittedChanges = customer.getUncommittedChanges(state)
110  expect(version).toBe(0)
111  expect(uncommittedChanges.size).toBe(0)
112  //  create new customer method on existing customer should throw an error
113  expect(() => {
114    customer.update(state, 'Test Customer Updated')
115  }).toThrow("can not update customer that doesn't exist")
116})
117
118it('should deactivate an existing customer aggregate', () => {
119  //  create event history (that would be loaded from event store in real application)
120  const storedEvents = new Set()
121  storedEvents.add(CustomerCreated(CUSTOMER_1_ID, 'Test Customer', CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD))
122  //  prepare aggregate with no event history (new aggregate)
123  let state = customer.loadFromHistory(storedEvents)
124  let version = customer.getCurrentVersion(state)
125  let uncommittedChanges = customer.getUncommittedChanges(state)
126  expect(version).toBe(1)
127  expect(uncommittedChanges.size).toBe(0)
128  customer.deactivate(state)
129  expect(state.customerId).toBe(CUSTOMER_1_ID)
130  expect(state.name).toBe('Test Customer')
131  expect(state.deactivated).toBeTruthy()
132})
133
134it('should reactivate an existing customer aggregate', () => {
135  //  create event history (that would be loaded from event store in real application)
136  const storedEvents = new Set()
137  storedEvents.add(CustomerCreated(CUSTOMER_1_ID, 'Test Customer', CUSTOMER_1_EMAIL, CUSTOMER_1_PASSWORD))
138  //  prepare aggregate with no event history (new aggregate)
139  let state = customer.loadFromHistory(storedEvents)
140  let version = customer.getCurrentVersion(state)
141  let uncommittedChanges = customer.getUncommittedChanges(state)
142  expect(version).toBe(1)
143  expect(uncommittedChanges.size).toBe(0)
144  customer.deactivate(state)
145  expect(state.customerId).toBe(CUSTOMER_1_ID)
146  expect(state.name).toBe('Test Customer')
147  expect(state.deactivated).toBeTruthy()
148  customer.reactivate(state)
149  expect(state.customerId).toBe(CUSTOMER_1_ID)
150  expect(state.name).toBe('Test Customer')
151  expect(state.deactivated).toBeUndefined()
152})
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)