@@ -14,6 +14,7 @@ import { Cli } from '../cli';
1414import { Config } from '../config' ;
1515import { MockRegistry } from '../../lib' ;
1616import { isTable } from '../../utils' ;
17+ import { TimeoutException } from '../../lib/exceptions' ;
1718
1819export class TestRunner {
1920 private static isAllPassed : boolean = true ;
@@ -25,10 +26,14 @@ export class TestRunner {
2526 await this . runTestSuite ( testName , target ) ;
2627 }
2728 } finally {
28- if ( ! this . isAllPassed && ! Cli . getOptions ( 'watch' ) ) {
29- exit ( 1 ) ;
30- }
3129 TestRegistry . clear ( ) ;
30+ process . nextTick ( ( ) => {
31+ if ( ! this . isAllPassed && ! Cli . getOptions ( 'watch' ) ) {
32+ exit ( 1 ) ;
33+ } else {
34+ exit ( 0 ) ;
35+ }
36+ } ) ;
3237 }
3338 }
3439
@@ -51,14 +56,15 @@ export class TestRunner {
5156
5257 await this . runLifecycleMethods ( testSuite , beforeAll , 'beforeAll' ) ;
5358
54- for ( const { methodName, caseDescription } of testCases ) {
59+ for ( const { methodName, caseDescription, timeout } of testCases ) {
5560 await this . runLifecycleMethods ( testSuite , beforeEach , 'beforeEach' ) ;
5661 await this . runTestCase (
5762 testSuite ,
5863 methodName ,
5964 caseDescription ,
6065 dataSetsArray ,
6166 dataTableArray ,
67+ timeout ,
6268 ) ;
6369 await this . runLifecycleMethods ( testSuite , afterEach , 'afterEach' ) ;
6470 this . clearMocks ( 'afterEach' ) ;
@@ -100,6 +106,7 @@ export class TestRunner {
100106 caseDescription : string ,
101107 dataSetsArray : IDataSet [ ] ,
102108 dataTableArray : IDataTableArray [ ] ,
109+ timeout : number ,
103110 ) {
104111 try {
105112 const { dataTable, dataSets } = this . getCaseData (
@@ -113,9 +120,15 @@ export class TestRunner {
113120 testSuiteInstance ,
114121 methodName ,
115122 dataTable ,
123+ timeout ,
116124 ) ;
117125 } else {
118- await this . runTestCaseWithData ( testSuiteInstance , methodName , dataSets ) ;
126+ await this . runTestCaseWithData (
127+ testSuiteInstance ,
128+ methodName ,
129+ dataSets ,
130+ timeout ,
131+ ) ;
119132 }
120133
121134 this . logTestResult ( caseDescription || methodName , 'PASSED' , 'grey' ) ;
@@ -129,28 +142,49 @@ export class TestRunner {
129142 testSuiteInstance : any ,
130143 methodName : string ,
131144 dataArray : IDataSet [ ] [ ] | IDataTable [ ] ,
145+ timeout : number ,
132146 ) {
133147 if ( dataArray . length <= 0 ) {
134- return await this . getTestResult ( testSuiteInstance , methodName , [ ] ) ;
148+ return await this . getTestResult (
149+ testSuiteInstance ,
150+ methodName ,
151+ [ ] ,
152+ timeout ,
153+ ) ;
135154 }
136155
137156 for ( const data of dataArray ) {
138- await this . getTestResult ( testSuiteInstance , methodName , data ) ;
157+ await this . getTestResult ( testSuiteInstance , methodName , data , timeout ) ;
139158 }
140159 }
141160
142161 private static async getTestResult (
143162 testSuiteInstance : any ,
144163 methodName : string ,
145164 data : IDataSet [ ] | IDataTable ,
165+ timeout : number = 5000 ,
146166 ) {
147- const result = isTable ( data )
148- ? testSuiteInstance [ methodName ] ( ...data . inputs , data . expected )
149- : testSuiteInstance [ methodName ] ( ...data ) ;
167+ const testPromise = new Promise < void > ( ( resolve , reject ) => {
168+ const result = isTable ( data )
169+ ? testSuiteInstance [ methodName ] ( ...data . inputs , data . expected )
170+ : testSuiteInstance [ methodName ] ( ...data ) ;
150171
151- if ( result instanceof Promise ) {
152- await result ;
153- }
172+ if ( result instanceof Promise ) {
173+ result . then ( resolve ) . catch ( reject ) ;
174+ } else {
175+ resolve ( ) ;
176+ }
177+ } ) ;
178+
179+ const timeoutPromise = new Promise ( ( _ , reject ) => {
180+ setTimeout (
181+ reject ,
182+ timeout ,
183+ new TimeoutException ( `Test timed out in ${ timeout } ms` ) ,
184+ ) ;
185+ } ) ;
186+
187+ await Promise . race ( [ testPromise , timeoutPromise ] ) ;
154188 }
155189
156190 private static getCaseData (
0 commit comments