import { TestCaseInstance } from '../testCaseInstance' ; import { TestInstance } from '../tests/test' ; import { TestSuiteInstance } from '../tests/testSuite' ; import { TestStatus } from '../testStatus' ; import { getTestSuiteInstance } from './utils' ; param name export function FTest ( name ? ) { return generateDecoratorFunction ( name , TestStatus . Focused ) ; } param name export function XTest ( name ? ) { return generateDecoratorFunction ( name , TestStatus . Ignored ) ; } interface TestDecorator { ( name ? ) : any ; focus : ( name ? : string ) => any ; ignore : ( name ? : string ) => any ; } param name function testDecorator ( name ? ) { return generateDecoratorFunction ( name , TestStatus . Normal ) ; } param name testDecorator . focus = FTest ; param name testDecorator . ignore = XTest ; export const Test = testDecorator ; deprecated TestSuite param name export function test ( name ? ) { return testDecorator ( name ) ; } deprecated TestSuite param name export function ftest ( name ? ) { return FTest ( name ) ; } deprecated TestSuite param name export function xtest ( name ? ) { return XTest ( name ) ; } function generateDecoratorFunction ( name , status ) { return ( target , key , descriptor ) => { const timeout = getTimeout ( target , key ) ; const testCases = getTestCases ( target , key ) ; const testSuiteInstance = getTestSuiteInstance ( target ) ; name = name ? name : key ; if ( testSuiteInstance . has ( name ) ) { throw new Error ( `A test named "${ name }" is already registered. Copy pasta much?` ) ; } testSuiteInstance . set ( name , generateTest ( name , testCases , status , descriptor . value , timeout ) ) ; } ; } function generateTest ( name , testCases , status , testMethod , timeout ) { return testCases ? generateTestsFromTestcases ( name , testMethod , testCases , status , timeout ) : new TestInstance ( name , decorateTest ( testMethod ) , status , timeout ) ; } function generateTestsFromTestcases ( name , testMethod , testCases , status , timeout ) { const tests = new TestSuiteInstance ( ) ; tests . name = name ; for ( const testCase of testCases ) { const decoratedTestMethod = decorateTest ( testMethod , testCase . args ) ; tests . set ( testCase . name , new TestInstance ( testCase . name , decoratedTestMethod , status , timeout ) ) ; } return tests ; } function decorateTest ( testMethod , args = [ ] ) { return async ( context ) => { await testMethod . bind ( context ) ( ... args ) ; } ; } function getTimeout ( target , key ) { return target . __timeouts ? target . __timeouts [ key ] : undefined ; } function getTestCases ( target , key ) { return target . __testCases ? target . __testCases [ key ] : undefined ; }