1- import * as assert from 'assert' ;
1+ import * as chai from 'chai' ;
2+ const chaiAsPromised = require ( 'chai-as-promised' ) ;
3+ chai . use ( chaiAsPromised ) ;
4+ const assert = chai . assert ;
25import * as fs from 'mz/fs' ;
36import * as path from 'path' ;
47import * as pg from 'pg' ;
58import {
69 Migration ,
10+ MigrationLoadError ,
711 MigrationNotFoundError ,
812 MigrationExecutionError ,
913 TaskTypeNotFoundError ,
@@ -129,61 +133,31 @@ describe('migration', () => {
129133 const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'not_found' } ) } ) ;
130134 const head = new Commit ( { sha1 : 'HEADCOMMITSHA1' } ) ;
131135 const trigger = new Commit ( { sha1 : 'TRIGGERCOMMITSHA1' } ) ;
132- try {
133- await task . execute ( __dirname + '/migrations' , adapter , head , trigger ) ;
134- throw new assert . AssertionError ( {
135- message : 'No MigrationNotFoundError was thrown.'
136- } ) ;
137- } catch ( err ) {
138- if ( ! ( err instanceof MigrationNotFoundError ) ) {
139- throw err ;
140- }
141- }
136+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , MigrationNotFoundError ) ;
137+ } ) ;
138+ it ( 'should throw a MigrationLoadError if the migration fails loading' , async ( ) => {
139+ const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'error_load' } ) } ) ;
140+ const head = new Commit ( { sha1 : 'HEADCOMMITSHA1' } ) ;
141+ const trigger = new Commit ( { sha1 : 'TRIGGERCOMMITSHA1' } ) ;
142+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , MigrationLoadError ) ;
142143 } ) ;
143144 it ( 'should throw a TaskTypeNotFoundError if the migration has no up or down function' , async ( ) => {
144145 const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'no_up' } ) } ) ;
145146 const head = new Commit ( { sha1 : 'HEADCOMMITSHA1' } ) ;
146147 const trigger = new Commit ( { sha1 : 'TRIGGERCOMMITSHA1' } ) ;
147- try {
148- await task . execute ( __dirname + '/migrations' , adapter , head , trigger ) ;
149- throw new assert . AssertionError ( {
150- message : 'No TaskTypeNotFoundError was thrown.'
151- } ) ;
152- } catch ( err ) {
153- if ( ! ( err instanceof TaskTypeNotFoundError ) ) {
154- throw err ;
155- }
156- }
148+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , TaskTypeNotFoundError ) ;
157149 } ) ;
158150 it ( 'should throw a MigrationExecutionError when the migration returns a rejected promise' , async ( ) => {
159151 const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'error_async' } ) } ) ;
160152 const head = new Commit ( { sha1 : 'HEADCOMMITSHA1' } ) ;
161153 const trigger = new Commit ( { sha1 : 'TRIGGERCOMMITSHA1' } ) ;
162- try {
163- await task . execute ( __dirname + '/migrations' , adapter , head , trigger ) ;
164- throw new assert . AssertionError ( {
165- message : 'No MigrationExecutionError was thrown.'
166- } ) ;
167- } catch ( err ) {
168- if ( ! ( err instanceof MigrationExecutionError ) ) {
169- throw err ;
170- }
171- }
154+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , MigrationExecutionError ) ;
172155 } ) ;
173156 it ( 'should throw a MigrationExecutionError when the migration throws sync' , async ( ) => {
174157 const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'error_sync' } ) } ) ;
175158 const head = new Commit ( { sha1 : 'HEADCOMMITSHA1' } ) ;
176159 const trigger = new Commit ( { sha1 : 'TRIGGERCOMMITSHA1' } ) ;
177- try {
178- await task . execute ( __dirname + '/migrations' , adapter , head , trigger ) ;
179- throw new assert . AssertionError ( {
180- message : 'No Migration ExecutionError was thrown.'
181- } ) ;
182- } catch ( err ) {
183- if ( ! ( err instanceof MigrationExecutionError ) ) {
184- throw err ;
185- }
186- }
160+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , MigrationExecutionError ) ;
187161 } ) ;
188162 it ( 'should throw a MigrationExecutionError when the migration would crash the process' , async ( ) => {
189163 const task = new Task ( { type : 'up' , migration : new Migration ( { name : 'error_crash' } ) } ) ;
@@ -193,14 +167,7 @@ describe('migration', () => {
193167 try {
194168 listener = process . listeners ( 'uncaughtException' ) . pop ( ) ;
195169 process . removeListener ( 'uncaughtException' , listener ) ;
196- await task . execute ( __dirname + '/migrations' , adapter , head , trigger ) ;
197- throw new assert . AssertionError ( {
198- message : 'No Migration ExecutionError was thrown.'
199- } ) ;
200- } catch ( err ) {
201- if ( ! ( err instanceof MigrationExecutionError ) ) {
202- throw err ;
203- }
170+ await assert . isRejected ( task . execute ( __dirname + '/migrations' , adapter , head , trigger ) , MigrationExecutionError ) ;
204171 } finally {
205172 process . addListener ( 'uncaughtException' , listener ) ;
206173 }
@@ -209,16 +176,7 @@ describe('migration', () => {
209176 describe ( 'toString' , ( ) => {
210177 it ( 'should throw if the task type is unknown' , async ( ) => {
211178 const task = new Task ( < any > { type : 'd' } ) ;
212- try {
213- task . toString ( ) ;
214- throw new assert . AssertionError ( {
215- message : 'Task did not throw error on toString with wrong type'
216- } ) ;
217- } catch ( err ) {
218- if ( ! ( err instanceof UnknownTaskTypeError ) ) {
219- throw err ;
220- }
221- }
179+ assert . throws ( ( ) => task . toString ( ) , UnknownTaskTypeError ) ;
222180 } ) ;
223181 } ) ;
224182 } ) ;
0 commit comments