Skip to content

Commit

Permalink
test(Errors): added tests for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stradivario committed Jul 29, 2019
1 parent 8bfd5b5 commit 39f5980
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Test",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
1 change: 0 additions & 1 deletion src/services/migration/migration.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export class MigrationService {
const appliedItems = statusItems.filter(
item => item.appliedAt !== 'PENDING'
);

const lastAppliedItem = appliedItems[appliedItems.length - 1];
if (!lastAppliedItem) {
return;
Expand Down
4 changes: 3 additions & 1 deletion tests/helpers/fake-mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export function TestCollectionMongoWrongCollection(response: unknown, data: Retu
toArray: () => data
}),
updateOne: () => ({ response }),
deleteOne: () => ({ response })
deleteOne: () => {
throw new Error('Cannot delete inside this mongo collection')
}
};
}

Expand Down
62 changes: 52 additions & 10 deletions tests/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ describe('Global Xmigrate Tests', () => {
);
});

it('Should throw error UP when inserting to mongo collection "Could not update changelo"', async () => {
it('Should throw error UP when inserting to mongo collection "Could not update changelog"', async () => {
await migrationService.createWithTemplate(
template as 'typescript',
'pesho1234',
Expand Down Expand Up @@ -465,24 +465,66 @@ describe('Global Xmigrate Tests', () => {
);
});

it('Should throw error DOWN when inserting to mongo collection "Could not update changelo"', async () => {
await migrationService.createWithTemplate(
template as 'typescript',
'pesho1234',
{ raw: true, typescript: true }
);
it('Should throw error DOWN when inserting to mongo collection "Could not update changelog"', async () => {
const spy = spyOn(migrationService, 'connect').and.callFake(() =>
FakeMongoClientErrorWhenInsertingCollection(true)
FakeMongoClientErrorWhenInsertingCollection(true, [
{
fileName: '20190728192825-pesho1234.js',
appliedAt: new Date(),
result: {}
}
])
);

const spyResolver = spyOn(migrationResolver, 'getFileNames').and.callFake(
() => ['20190728192825-pesho1234.js']
);

const spyLoad = spyOn(migrationResolver, 'loadMigration').and.callFake(
() => ({ down: async () => ({}) })
);
try {
await migrationService.down();
} catch (e) {
expect(spy).toHaveBeenCalled();
expect(e.message).toBe(
'Could not update changelog: Cannot insert inside this mongo collection'
'Could not update changelog: Cannot delete inside this mongo collection'
);
}
expect(spy).toHaveBeenCalled();
expect(spyResolver).toHaveBeenCalled();
expect(spyLoad).toHaveBeenCalled();
});

it('Should throw custom error DOWN "', async () => {
const spy = spyOn(migrationService, 'connect').and.callFake(() =>
FakeMongoClientErrorWhenInsertingCollection(true, [
{
fileName: '20190728192825-pesho1234.js',
appliedAt: new Date(),
result: {}
}
])
);

const spyResolver = spyOn(migrationResolver, 'getFileNames').and.callFake(
() => ['20190728192825-pesho1234.js']
);

const spyLoad = spyOn(migrationResolver, 'loadMigration').and.callFake(
() => ({
down: async () => {
throw new Error('test');
}
})
);
try {
await migrationService.down();
} catch (e) {
expect(e.message).toBe('test');
}
expect(spy).toHaveBeenCalled();
expect(spyResolver).toHaveBeenCalled();
expect(spyLoad).toHaveBeenCalled();
const fileNames = await migrationResolver.getFileNames();
await migrationResolver.delete(migrationResolver.getFilePath(fileNames[0]));
await migrationResolver.delete(
Expand Down

0 comments on commit 39f5980

Please sign in to comment.