11import context from 'js-slang/context' ;
22
3- import type { Suite , SuiteResult , Test , TestResult } from './types' ;
3+ import {
4+ UnitestBundleInternalError ,
5+ type Suite ,
6+ type SuiteResult ,
7+ type Test ,
8+ type TestResult ,
9+ type TestSuite
10+ } from './types' ;
411
512function getNewSuite ( name ?: string ) : Suite {
613 return {
@@ -14,7 +21,9 @@ function getNewSuite(name?: string): Suite {
1421 * to collect those Suite Results since none of them will have a parent suite
1522 */
1623export const suiteResults : SuiteResult [ ] = [ ] ;
17- let currentSuite : Suite | null = null ;
24+
25+ export let currentSuite : Suite | null = null ;
26+ export let currentTest : string | null = null ;
1827
1928function handleErr ( err : any ) {
2029 if ( err . error && err . error . message ) {
@@ -26,42 +35,54 @@ function handleErr(err: any) {
2635 throw err ;
2736}
2837
29- /**
30- * Defines a single test.
31- * @param name Description for this test.
32- * @param func Function containing assertions.
33- */
34- export function it ( name : string , func : Test ) : void {
38+ function runTest ( name : string , funcName : string , func : Test ) {
3539 if ( currentSuite === null ) {
36- throw new Error ( `'${ it . name } ' must be called from within a test suite!` ) ;
40+ throw new UnitestBundleInternalError ( `${ funcName } must be called from within a test suite!` ) ;
41+ }
42+
43+ if ( currentTest !== null ) {
44+ throw new UnitestBundleInternalError ( `${ funcName } cannot be called from within another test!` ) ;
3745 }
3846
3947 try {
48+ currentTest = name ;
4049 func ( ) ;
4150 currentSuite . results . push ( {
4251 name,
4352 passed : true ,
4453 } ) ;
4554 } catch ( err ) {
55+ if ( err instanceof UnitestBundleInternalError ) {
56+ throw err ;
57+ }
58+
4659 const error = handleErr ( err ) ;
4760 currentSuite . results . push ( {
4861 name,
4962 passed : false ,
5063 error,
5164 } ) ;
65+ } finally {
66+ currentTest = null ;
5267 }
5368}
5469
70+ /**
71+ * Defines a single test.
72+ * @param name Description for this test.
73+ * @param func Function containing assertions.
74+ */
75+ export function it ( name : string , func : Test ) : void {
76+ runTest ( name , it . name , func ) ;
77+ }
78+
5579/**
5680 * Defines a single test.
5781 * @param msg Description for this test.
5882 * @param func Function containing assertions.
5983 */
6084export function test ( msg : string , func : Test ) : void {
61- if ( currentSuite === null ) {
62- throw new Error ( `${ test . name } must be called from within a test suite!` ) ;
63- }
64- it ( msg , func ) ;
85+ runTest ( msg , test . name , func ) ;
6586}
6687
6788function determinePassCount ( results : ( TestResult | SuiteResult ) [ ] ) : number {
@@ -82,7 +103,7 @@ function determinePassCount(results: (TestResult | SuiteResult)[]): number {
82103 * @param msg Description for this test suite.
83104 * @param func Function containing tests.
84105 */
85- export function describe ( msg : string , func : Test ) : void {
106+ export function describe ( msg : string , func : TestSuite ) : void {
86107 const oldSuite = currentSuite ;
87108 const newSuite = getNewSuite ( msg ) ;
88109
0 commit comments