From 29041feabbce29c7e0f751cd40fe0ca100c4caff Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Thu, 6 Dec 2018 15:42:25 -0500 Subject: [PATCH 1/8] Made name parameters optional, inferred name from method/class name --- src/lib/decorators/test.decorator.ts | 10 ++++++++-- src/lib/decorators/testSuite.decorator.ts | 15 ++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/lib/decorators/test.decorator.ts b/src/lib/decorators/test.decorator.ts index 539d89f..9180297 100644 --- a/src/lib/decorators/test.decorator.ts +++ b/src/lib/decorators/test.decorator.ts @@ -10,10 +10,12 @@ import { TestSuite } from '../tests/testSuite'; * @param testCases Allows to run the test multiple times with different arguments. Arguments will be passed to the test class. * @param timeout The test will automaticlaly fail if it has been running for longer than the specified timeout. */ -export function test(name: string, testCases?: TestCase[], timeout: number = 2000) { - return (target, key, descriptor) => { +export function test(name?: string, testCases?: TestCase[], timeout: number = 2000) { + return (target, key: string, descriptor) => { initializeTarget(target); const testSuiteInstance: TestSuite = target.__testSuiteInstance; + + name = name ? name : key; if (testSuiteInstance.has(name)) { throw new Error(`A test named "${name}" is already registered. Copy pasta much?`); } @@ -34,6 +36,8 @@ export function ftest(name: string, testCases?: TestCase[], timeout: number = 20 return (target, key, descriptor) => { initializeTarget(target); const testSuiteInstance: TestSuite = target.__testSuiteInstance; + + name = name ? name : key; if (testSuiteInstance.has(name)) { throw new Error(`A test named "${name}" is already registered. Copy pasta much?`); } @@ -54,6 +58,8 @@ export function xtest(name: string, testCases?: TestCase[], timeout: number = 20 return (target, key, descriptor) => { initializeTarget(target); const testSuiteInstance: TestSuite = target.__testSuiteInstance; + + name = name ? name : key; if (testSuiteInstance.has(name)) { throw new Error(`A test named "${name}" is already registered. Copy pasta much?`); } diff --git a/src/lib/decorators/testSuite.decorator.ts b/src/lib/decorators/testSuite.decorator.ts index 3da2cc2..f23916e 100644 --- a/src/lib/decorators/testSuite.decorator.ts +++ b/src/lib/decorators/testSuite.decorator.ts @@ -6,8 +6,8 @@ import { TestSuite } from '../tests/testSuite'; * * @param name Name of the test suite, displayed in the test report. */ -export function testSuite(name: string): any { - return createTestSuiteDecoratorFactory(name, TestStatus.Normal); +export function testSuite(name?: string): any { + return createTestSuiteDecoratorFactory(TestStatus.Normal, name); } /** @@ -15,8 +15,8 @@ export function testSuite(name: string): * * @param name Name of the test suite, displayed in the test report. */ -export function ftestSuite(name: string): any { - return createTestSuiteDecoratorFactory(name, TestStatus.Focused); +export function ftestSuite(name?: string): any { + return createTestSuiteDecoratorFactory(TestStatus.Focused, name); } /** @@ -24,12 +24,13 @@ export function ftestSuite(name: string): * * @param name Name of the test suite, displayed in the test report. */ -export function xtestSuite(name: string): any { - return createTestSuiteDecoratorFactory(name, TestStatus.Ignored); +export function xtestSuite(name?: string): any { + return createTestSuiteDecoratorFactory(TestStatus.Ignored, name); } -function createTestSuiteDecoratorFactory(name: string, status: TestStatus) { +function createTestSuiteDecoratorFactory(status: TestStatus, name?: string) { return (constructor: T) => { + name = name ? name : constructor.name; (constructor as any).__testSuiteInstance = createTestSuite(constructor, name, status); return constructor; }; From cb067738207b496cc6c92050f5c7e883403727aa Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Thu, 6 Dec 2018 15:47:32 -0500 Subject: [PATCH 2/8] Updated README.md to showcase new feature --- README.md | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5ee6ceb..6752140 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,10 @@ $ testyts init Writing tests with Testy is simple. Don't forget to export your test suites though. Otherwise, they won't be discovered by the test runner. ```ts -@testSuite('Sum Test Suite') +@testSuite() export class MyTestSuite { - @test('One plus one, should equal two') + @test() onePlusOne() { // Act const result = 1 + 1; @@ -47,7 +47,7 @@ export class MyTestSuite { Testy provides setup and teardown hooks. ```ts -@testSuite('Sum Test Suite') +@testSuite() export class MyTestSuite { @beforeAll() @@ -81,7 +81,7 @@ class MyBaseTestSuite{ // Setup/teardown extravaganza } -@testSuite('My Test Suite') +@testSuite() class MyTestSuite extends MyBaseTestSuite { // My tests } @@ -90,7 +90,7 @@ class MyTestSuite extends MyBaseTestSuite { ### Test cases ```ts -@testSuite('Sum Test Suite') +@testSuite() export class MyTestSuite { @test('Addition', [ new TestCase('Two plus two is four', 2, 2, 4), @@ -120,14 +120,14 @@ expect.toBeSorted.inAscendingOrder([0, 1, 1, 2, 3, 5, 8]); You can ignore tests by adding an `x` before a test suite or a specific test decorator. Ignored tests will still show up in the test report, but they will be marked as ignored. ```ts -@xtestSuite('Sum Test Suite') // This test suite will be ignored +@xtestSuite() // This test suite will be ignored export class MyTestSuite { // Your tests } -@testSuite('Sum Test Suite') +@testSuite() export class MyTestSuite { - @xtest('One plus one, should equal two') // This test will be ignored + @xtest() // This test will be ignored onePlusOne() { // Some test } @@ -137,20 +137,39 @@ export class MyTestSuite { You can also focus tests by adding an `f` before a test suite or a specific test decorator. If one test or test suites are focused, only those will be runned. The others will be reported as ignored. ```ts -@ftestSuite('Sum Test Suite') // This test suite will be focused. +@ftestSuite() // This test suite will be focused. export class MyTestSuite { ... } -@testSuite('Sum Test Suite') +@testSuite() export class MyTestSuite { - @ftest('One plus one, should equal two') // This test will be focused + @ftest() // This test will be focused onePlusOne() { // Your test } } ``` +## Custom tests and test suites names + +The tests and test suites names are inferred from the method or class name by default. You can specify a custom name. + +```ts +@testSuite('My glorious test suite') +export class MyTestSuite { + + @test('Adding one plus one, should equal two') + onePlusOne() { + // Act + const result = 1 + 1; + + // Assert + expect.toBeEqual(result, 2); + } +} +``` + ## Run the tests To run the tests, use the following command From 378bccb02d4adb714eaccc5bcf1770da20bcba67 Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Thu, 6 Dec 2018 15:48:43 -0500 Subject: [PATCH 3/8] Upped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 027be86..fcf88b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "testyts", - "version": "0.4.0", + "version": "0.4.1", "icon": "img/test-icon-128x128.png", "description": "Modern test framework for TypeScript.", "main": "build/testyCore.js", From d70ca0a56212d27ab15508c3a83283de928d2bcc Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Thu, 6 Dec 2018 15:52:20 -0500 Subject: [PATCH 4/8] Extracted function --- src/lib/decorators/test.decorator.ts | 38 ++++++++-------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/lib/decorators/test.decorator.ts b/src/lib/decorators/test.decorator.ts index 9180297..fa7b21e 100644 --- a/src/lib/decorators/test.decorator.ts +++ b/src/lib/decorators/test.decorator.ts @@ -11,17 +11,7 @@ import { TestSuite } from '../tests/testSuite'; * @param timeout The test will automaticlaly fail if it has been running for longer than the specified timeout. */ export function test(name?: string, testCases?: TestCase[], timeout: number = 2000) { - return (target, key: string, descriptor) => { - initializeTarget(target); - const testSuiteInstance: TestSuite = target.__testSuiteInstance; - - 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, TestStatus.Normal, descriptor.value, timeout)); - }; + return generateDecoratorFunction(name, TestStatus.Normal, testCases, timeout); } /** @@ -33,17 +23,7 @@ export function test(name?: string, testCases?: TestCase[], timeout: number = 20 * @param timeout The test will automaticlaly fail if it has been running for longer than the specified timeout. */ export function ftest(name: string, testCases?: TestCase[], timeout: number = 2000) { - return (target, key, descriptor) => { - initializeTarget(target); - const testSuiteInstance: TestSuite = target.__testSuiteInstance; - - 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, TestStatus.Focused, descriptor.value, timeout)); - }; + return generateDecoratorFunction(name, TestStatus.Focused, testCases, timeout); } /** @@ -55,6 +35,14 @@ export function ftest(name: string, testCases?: TestCase[], timeout: number = 20 * @param timeout The test will automaticlaly fail if it has been running for longer than the specified timeout. */ export function xtest(name: string, testCases?: TestCase[], timeout: number = 2000) { + return generateDecoratorFunction(name, TestStatus.Ignored, testCases, timeout); +} + +function initializeTarget(target: any) { + if (!target.__testSuiteInstance) { target.__testSuiteInstance = new TestSuite(); } +} + +function generateDecoratorFunction(name: string, status: TestStatus, testCases: TestCase[], timeout: number) { return (target, key, descriptor) => { initializeTarget(target); const testSuiteInstance: TestSuite = target.__testSuiteInstance; @@ -64,14 +52,10 @@ export function xtest(name: string, testCases?: TestCase[], timeout: number = 20 throw new Error(`A test named "${name}" is already registered. Copy pasta much?`); } - testSuiteInstance.set(name, generateTest(name, testCases, TestStatus.Ignored, descriptor.value, timeout)); + testSuiteInstance.set(name, generateTest(name, testCases, status, descriptor.value, timeout)); }; } -function initializeTarget(target: any) { - if (!target.__testSuiteInstance) { target.__testSuiteInstance = new TestSuite(); } -} - function generateTest(name: string, testCases: TestCase[], status: TestStatus, testMethod: Function, timeout: number): Test | TestSuite { return testCases ? generateTestsFromTestcases(name, testMethod, testCases, status, timeout) From 41d0c9a78d2f35ec76ba3106f62c5aba70dde08c Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Thu, 13 Dec 2018 00:07:18 -0500 Subject: [PATCH 5/8] Refactored test to use the test loader instead of createTestSuite. Now everything is broken. --- src/lib/decorators/testSuite.decorator.ts | 5 +- .../testSuiteWithBaseTestSuite.ts | 2 + .../baseTestSuite/testWithBase.spec.ts | 14 +-- .../beforeAfterDecorators.spec.ts | 25 +++-- .../normalBeforeAfterTestSuite.ts | 2 + .../throwsDuringAfterAllTestSuite.ts | 2 + .../throwsDuringAfterEachTestSuite.ts | 2 + .../throwsDuringBeforeAllTestSuite.ts | 2 + .../throwsDuringBeforeEachTestSuite.ts | 3 +- .../multipleTestsTestDecoratorTestSuite.ts | 2 + .../testCasesTestDecoratorTestSuite.ts | 3 +- .../testDecorator/testDecorator.spec.ts | 98 +++++++++++-------- .../testDecorator/testDecoratorTestSuite.ts | 2 + .../testDecorator/testWithoutNames.ts | 19 ++++ .../timeoutTestDecoratorTestSuite.ts | 2 + 15 files changed, 119 insertions(+), 64 deletions(-) create mode 100644 src/spec/decorators/testDecorator/testWithoutNames.ts diff --git a/src/lib/decorators/testSuite.decorator.ts b/src/lib/decorators/testSuite.decorator.ts index f23916e..91373f4 100644 --- a/src/lib/decorators/testSuite.decorator.ts +++ b/src/lib/decorators/testSuite.decorator.ts @@ -36,10 +36,7 @@ function createTestSuiteDecoratorFactory( }; } -/** - * [WARNING] This class should be used for internal testing. - */ -export function createTestSuite(constructor: new () => T, name: string, status: TestStatus): TestSuite { +function createTestSuite(constructor: new () => T, name: string, status: TestStatus): TestSuite { const context = new constructor(); const testSuiteInstance: TestSuite = (context as any).__testSuiteInstance; testSuiteInstance.name = name; diff --git a/src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts b/src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts index 62d243a..e05545c 100644 --- a/src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts +++ b/src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts @@ -1,5 +1,7 @@ import { test, beforeAll, beforeEach, afterEach, afterAll } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Base Test Suite') export class BaseTestSuite { public beforeAllExecuted = []; public beforeEachExecuted = []; diff --git a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts index 992444c..18a48bb 100644 --- a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts +++ b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts @@ -1,11 +1,11 @@ import { test } from '../../../lib/decorators/test.decorator'; -import { createTestSuite, testSuite } from '../../../lib/decorators/testSuite.decorator'; -import { TestStatus } from '../../../lib/testStatus'; -import { TestSuiteWithBase, BaseTestSuite } from './testSuiteWithBaseTestSuite'; -import { expect } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +import { Logger } from '../../../lib/logger/logger'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; +import { TestsLoader } from '../../../lib/utils/testsLoader'; +import { expect } from '../../../testyCore'; import { NullLogger } from '../../utils/nullLogger'; -import { Logger } from '../../../lib/logger/logger'; +import { BaseTestSuite, TestSuiteWithBase } from './testSuiteWithBaseTestSuite'; @testSuite('Test Suite With Base Test Suite Tests') export class BeforeAfterDecoratorsTestSuite { @@ -14,7 +14,9 @@ export class BeforeAfterDecoratorsTestSuite { @test('the base and the actual test suite before and after methods are called.') private async trivialCase() { // Arrange - const testSuite = createTestSuite(TestSuiteWithBase, 'Dummy Test Suite', TestStatus.Normal); + const testsLoader = new TestsLoader(); + + const testSuite = await testsLoader.loadTests('./', ['testSuiteWithBaseTestSuite.ts'], undefined); const testRunnerVisitor = new TestsRunnerVisitor(this.logger); // Act diff --git a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts index 88ea940..1ca472a 100644 --- a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts +++ b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts @@ -1,27 +1,34 @@ import { test } from '../../../lib/decorators/test.decorator'; -import { createTestSuite, testSuite } from '../../../lib/decorators/testSuite.decorator'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; import { Logger } from '../../../lib/logger/logger'; import { TestResult } from '../../../lib/reporting/report/testResult'; import { TestCase } from '../../../lib/testCase'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; import { TestStatus } from '../../../lib/testStatus'; -import { expect } from '../../../testyCore'; +import { expect, beforeEach } from '../../../testyCore'; import { NullLogger } from '../../utils/nullLogger'; import { NormalBeforeAfterTestSuite } from './normalBeforeAfterTestSuite'; import { ThrowsDuringAfterAllTestSuite } from './throwsDuringAfterAllTestSuite'; import { ThrowsDuringAfterEachTestSuite } from './throwsDuringAfterEachTestSuite'; import { ThrowsDuringBeforeAllTestSuite } from './throwsDuringBeforeAllTestSuite'; import { ThrowsDuringBeforeEachTestSuite } from './throwsDuringBeforeEachTestSuite'; +import { TestsLoader } from '../../../lib/utils/testsLoader'; @testSuite('Before and After Decorators Test Suite') export class BeforeAfterDecoratorsTestSuite { + private testsLoader: TestsLoader; private logger: Logger = new NullLogger(); private visitor = new TestsRunnerVisitor(this.logger); + @beforeEach() + beforeEach() { + this.testsLoader = new TestsLoader(); + } + @test('beforeAll, beforeEach, afterEach and afterAll are called the right amount of time.') private async trivialCase() { // Arrange - const testSuite = createTestSuite(NormalBeforeAfterTestSuite, 'Dummy Test Suite', TestStatus.Normal); + const testSuite = await this.testsLoader.loadTests('./', ['beforeAfterDecorators.spec.ts'], undefined); // Act const report = await testSuite.accept(this.visitor); @@ -35,14 +42,14 @@ export class BeforeAfterDecoratorsTestSuite { } @test('Before and after methods failures', [ - new TestCase('beforeAll throws, should return a failed test report', ThrowsDuringBeforeAllTestSuite, 6), - new TestCase('beforeEach throws, should return a failed test report', ThrowsDuringBeforeEachTestSuite, 6), - new TestCase('afterEach throws, should return a failed test report', ThrowsDuringAfterEachTestSuite, 6), - new TestCase('afterAll throws, should return a failed test report', ThrowsDuringAfterAllTestSuite, 6), + new TestCase('beforeAll throws, should return a failed test report', 'throwsDuringBeforeAllTestSuite.ts', 6), + new TestCase('beforeEach throws, should return a failed test report', 'throwsDuringBeforeEachTestSuite.ts', 6), + new TestCase('afterEach throws, should return a failed test report', 'throwsDuringAfterEachTestSuite.ts', 6), + new TestCase('afterAll throws, should return a failed test report', 'throwsDuringAfterAllTestSuite.ts', 6), ]) - private async beforeOfAfterMethodFails(testSuiteType: any, numberOfTests: number) { + private async beforeOfAfterMethodFails(testFile: any, numberOfTests: number) { // Arrange - const testSuite = createTestSuite(testSuiteType, 'Dummy Test Suite', TestStatus.Normal); + const testSuite = await this.testsLoader.loadTests('./', [testFile], undefined); // Act const report = await testSuite.accept(this.visitor); diff --git a/src/spec/decorators/beforeAfterDecorators/normalBeforeAfterTestSuite.ts b/src/spec/decorators/beforeAfterDecorators/normalBeforeAfterTestSuite.ts index 729bc7e..76d24ff 100644 --- a/src/spec/decorators/beforeAfterDecorators/normalBeforeAfterTestSuite.ts +++ b/src/spec/decorators/beforeAfterDecorators/normalBeforeAfterTestSuite.ts @@ -1,6 +1,8 @@ import { test, beforeAll, beforeEach, afterEach, afterAll, TestCase } from '../../../testyCore'; import { xtest } from '../../../lib/decorators/test.decorator'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Normal Before After Test Suite') export class NormalBeforeAfterTestSuite { public numberOfBeforeAllExecutions = 0; public numberOfBeforeEachExecutions = 0; diff --git a/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterAllTestSuite.ts b/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterAllTestSuite.ts index e7816d7..44f152c 100644 --- a/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterAllTestSuite.ts +++ b/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterAllTestSuite.ts @@ -1,5 +1,7 @@ import { test, xtest, afterAll, TestCase } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Throws During After All Test Suite ') export class ThrowsDuringAfterAllTestSuite { @afterAll() private afterAll() { diff --git a/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterEachTestSuite.ts b/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterEachTestSuite.ts index 4e428c1..bb3a270 100644 --- a/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterEachTestSuite.ts +++ b/src/spec/decorators/beforeAfterDecorators/throwsDuringAfterEachTestSuite.ts @@ -1,5 +1,7 @@ import { test, xtest, afterEach, TestCase } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Throws During After Each Test Suite') export class ThrowsDuringAfterEachTestSuite { @afterEach() private afterEach() { diff --git a/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeAllTestSuite.ts b/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeAllTestSuite.ts index 67f8ee8..bfa2f04 100644 --- a/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeAllTestSuite.ts +++ b/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeAllTestSuite.ts @@ -1,5 +1,7 @@ import { test, xtest, beforeAll, TestCase } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Throws During Before All Test Suite') export class ThrowsDuringBeforeAllTestSuite { @beforeAll() private beforeAll() { diff --git a/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeEachTestSuite.ts b/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeEachTestSuite.ts index 8c07655..9bd9beb 100644 --- a/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeEachTestSuite.ts +++ b/src/spec/decorators/beforeAfterDecorators/throwsDuringBeforeEachTestSuite.ts @@ -1,5 +1,6 @@ -import { test, xtest, beforeEach, TestCase } from '../../../testyCore'; +import { test, xtest, beforeEach, TestCase, testSuite } from '../../../testyCore'; +@testSuite('Throws During Before Each Test Suite') export class ThrowsDuringBeforeEachTestSuite { @beforeEach() private beforeEach() { diff --git a/src/spec/decorators/testDecorator/multipleTestsTestDecoratorTestSuite.ts b/src/spec/decorators/testDecorator/multipleTestsTestDecoratorTestSuite.ts index 35093de..18d9819 100644 --- a/src/spec/decorators/testDecorator/multipleTestsTestDecoratorTestSuite.ts +++ b/src/spec/decorators/testDecorator/multipleTestsTestDecoratorTestSuite.ts @@ -1,5 +1,7 @@ import { test } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Multiple Test Test Decorator Test Suite') export class MultipleTestTestDecoratorTestSuite { public numberOfRunsTest1: number = 0; diff --git a/src/spec/decorators/testDecorator/testCasesTestDecoratorTestSuite.ts b/src/spec/decorators/testDecorator/testCasesTestDecoratorTestSuite.ts index ed2d08b..3e16dd0 100644 --- a/src/spec/decorators/testDecorator/testCasesTestDecoratorTestSuite.ts +++ b/src/spec/decorators/testDecorator/testCasesTestDecoratorTestSuite.ts @@ -1,5 +1,6 @@ -import { test, TestCase } from '../../../testyCore'; +import { test, TestCase, testSuite } from '../../../testyCore'; +@testSuite('TestCases Test Decorator Test Suite') export class TestCasesTestDecoratorTestSuite { public numberOfRunsTest1: number = 0; diff --git a/src/spec/decorators/testDecorator/testDecorator.spec.ts b/src/spec/decorators/testDecorator/testDecorator.spec.ts index 8f196bc..ffdaa12 100644 --- a/src/spec/decorators/testDecorator/testDecorator.spec.ts +++ b/src/spec/decorators/testDecorator/testDecorator.spec.ts @@ -1,69 +1,81 @@ -import { test, expect, testSuite } from '../../../testyCore'; +import { test, expect, testSuite, ftest } from '../../../testyCore'; import { TestResult } from '../../../lib/reporting/report/testResult'; -import { SingleTestTestDecoratorTestSuite } from './testDecoratorTestSuite'; -import { MultipleTestTestDecoratorTestSuite } from './multipleTestsTestDecoratorTestSuite'; -import { TestCasesTestDecoratorTestSuite } from './testCasesTestDecoratorTestSuite'; -import { TimeoutTestDecoratorTestSuite } from './timeoutTestDecoratorTestSuite'; -import { createTestSuite } from '../../../lib/decorators/testSuite.decorator'; -import { TestStatus } from '../../../lib/testStatus'; import { Logger } from '../../../lib/logger/logger'; import { NullLogger } from '../../utils/nullLogger'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; +import { TestsLoader } from '../../../lib/utils/testsLoader'; +import { beforeEach } from '../../../lib/decorators/beforeEach.decorator'; @testSuite('Test Decorator Test Suite') export class TestDecoratorTestSuite { + private testsLoader: TestsLoader; private logger: Logger = new NullLogger(); private visitor = new TestsRunnerVisitor(this.logger); - @test('single test, test should be ran once') - private async singleTest() { - // Arrange - const testSuite = createTestSuite(SingleTestTestDecoratorTestSuite, 'Dummy Test Suite', TestStatus.Normal); + @beforeEach() + beforeEach() { + this.testsLoader = new TestsLoader(); + } - // Act - const report = await testSuite.accept(this.visitor); + // @test('single test, test should be ran once') + // private async singleTest() { + // // Arrange + // const testSuite = await this.testsLoader.loadTests('', ['testDecoratorTestSuite.ts'], undefined); - // Assert - expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - } + // // Act + // const report = await testSuite.accept(this.visitor); - @test('multiple test, tests should be ran once') - private async multipleTest() { - // Arrange - const testSuite = createTestSuite(MultipleTestTestDecoratorTestSuite, 'Dummy Test Suite', TestStatus.Normal); + // // Assert + // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); + // } - // Act - const report = await testSuite.accept(this.visitor); + // @test('multiple test, tests should be ran once') + // private async multipleTest() { + // // Arrange + // const testSuite = await this.testsLoader.loadTests('./', ['multipleTestsTestDecoratorTestSuite.ts'], undefined); - // Assert - expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); - expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); - } + // // Act + // const report = await testSuite.accept(this.visitor); - @test('test cases, tests should be ran once') - private async testCasesTest() { - // Arrange - const testSuite = createTestSuite(TestCasesTestDecoratorTestSuite, 'Dummy Test Suite', TestStatus.Normal); + // // Assert + // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); + // expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); + // expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); + // } - // Act - const report = await testSuite.accept(this.visitor); + // @test('test cases, tests should be ran once') + // private async testCasesTest() { + // // Arrange + // const testSuite = await this.testsLoader.loadTests('./', ['testCasesTestDecoratorTestSuite.ts'], undefined); - // Assert - expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); - expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); - } + // // Act + // const report = await testSuite.accept(this.visitor); + + // // Assert + // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); + // expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); + // expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); + // } + + // @test('test takes too long, should return failed test report') + // private async timeoutTest() { + // // Arrange + // const testSuite = await this.testsLoader.loadTests('./', ['timeoutTestDecoratorTestSuite.ts'], undefined); + + // // Act + // const report = await testSuite.accept(this.visitor); + + // // Assert + // expect.toBeEqual(report.result, TestResult.Failure); + // } - @test('test takes too long, should return failed test report') - private async timeoutTest() { + @test('no names, should infer from method names') + private async noNameTest() { // Arrange - const testSuite = createTestSuite(TimeoutTestDecoratorTestSuite, 'Dummy Test Suite', TestStatus.Normal); + const testSuite = await this.testsLoader.loadTests(__dirname, ['testWithoutNames.ts'], undefined); // Act - const report = await testSuite.accept(this.visitor); // Assert - expect.toBeEqual(report.result, TestResult.Failure); } } \ No newline at end of file diff --git a/src/spec/decorators/testDecorator/testDecoratorTestSuite.ts b/src/spec/decorators/testDecorator/testDecoratorTestSuite.ts index 7d00938..901e494 100644 --- a/src/spec/decorators/testDecorator/testDecoratorTestSuite.ts +++ b/src/spec/decorators/testDecorator/testDecoratorTestSuite.ts @@ -1,5 +1,7 @@ import { test } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Single Test Test Decorator Test Suite') export class SingleTestTestDecoratorTestSuite { public numberOfRunsTest1: number = 0; diff --git a/src/spec/decorators/testDecorator/testWithoutNames.ts b/src/spec/decorators/testDecorator/testWithoutNames.ts new file mode 100644 index 0000000..5093a33 --- /dev/null +++ b/src/spec/decorators/testDecorator/testWithoutNames.ts @@ -0,0 +1,19 @@ +import { test } from '../../../testyCore'; +import { TestCase } from '../../../lib/testCase'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; + +@testSuite() +export class TestWithNoNamesTestSuite { + @test() + private myTest1() { } + + @test() + private myTest2() { } + + @test(undefined, [ + new TestCase('testCase1'), + new TestCase('testCase2'), + new TestCase('testCase3') + ]) + private myTest3() { } +} \ No newline at end of file diff --git a/src/spec/decorators/testDecorator/timeoutTestDecoratorTestSuite.ts b/src/spec/decorators/testDecorator/timeoutTestDecoratorTestSuite.ts index c254de5..af0af12 100644 --- a/src/spec/decorators/testDecorator/timeoutTestDecoratorTestSuite.ts +++ b/src/spec/decorators/testDecorator/timeoutTestDecoratorTestSuite.ts @@ -1,5 +1,7 @@ import { test } from '../../../testyCore'; +import { testSuite } from '../../../lib/decorators/testSuite.decorator'; +@testSuite('Timeout Test Decorator Test Suite') export class TimeoutTestDecoratorTestSuite { @test('My test with test cases', undefined, 10) private async tests(n: number) { From c6501e648e72e5a51c896e71fda49b7dfbcf46a2 Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Fri, 14 Dec 2018 20:24:52 -0500 Subject: [PATCH 6/8] Fixed broken tests --- src/lib/utils/testsLoader.ts | 6 +- ...hBaseTestSuite.ts => testSuiteWithBase.ts} | 0 .../baseTestSuite/testWithBase.spec.ts | 7 +- .../beforeAfterDecorators.spec.ts | 31 ++---- .../testDecorator/testDecorator.spec.ts | 102 +++++++++--------- 5 files changed, 66 insertions(+), 80 deletions(-) rename src/spec/decorators/baseTestSuite/{testSuiteWithBaseTestSuite.ts => testSuiteWithBase.ts} (100%) diff --git a/src/lib/utils/testsLoader.ts b/src/lib/utils/testsLoader.ts index 667fe96..b78d7cb 100644 --- a/src/lib/utils/testsLoader.ts +++ b/src/lib/utils/testsLoader.ts @@ -5,11 +5,15 @@ import * as tsnode from 'ts-node'; import { TestSuite } from '../tests/testSuite'; export class TestsLoader { + private static isTsnodeRegistered = false; constructor(private logger?: Logger) { } public async loadTests(root: string, patterns: string[], tsconfig: {}): Promise { // We register the tsnode compiler to transpile the test files - tsnode.register(tsconfig); + if (!TestsLoader.isTsnodeRegistered) { + tsnode.register(tsconfig); + TestsLoader.isTsnodeRegistered = true; + } const files: Set = new Set(); for (const pattern of patterns) { diff --git a/src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts b/src/spec/decorators/baseTestSuite/testSuiteWithBase.ts similarity index 100% rename from src/spec/decorators/baseTestSuite/testSuiteWithBaseTestSuite.ts rename to src/spec/decorators/baseTestSuite/testSuiteWithBase.ts diff --git a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts index 18a48bb..e260941 100644 --- a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts +++ b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts @@ -2,10 +2,9 @@ import { test } from '../../../lib/decorators/test.decorator'; import { testSuite } from '../../../lib/decorators/testSuite.decorator'; import { Logger } from '../../../lib/logger/logger'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; -import { TestsLoader } from '../../../lib/utils/testsLoader'; import { expect } from '../../../testyCore'; import { NullLogger } from '../../utils/nullLogger'; -import { BaseTestSuite, TestSuiteWithBase } from './testSuiteWithBaseTestSuite'; +import { BaseTestSuite, TestSuiteWithBase } from './testSuiteWithBase'; @testSuite('Test Suite With Base Test Suite Tests') export class BeforeAfterDecoratorsTestSuite { @@ -14,9 +13,7 @@ export class BeforeAfterDecoratorsTestSuite { @test('the base and the actual test suite before and after methods are called.') private async trivialCase() { // Arrange - const testsLoader = new TestsLoader(); - - const testSuite = await testsLoader.loadTests('./', ['testSuiteWithBaseTestSuite.ts'], undefined); + const testSuite = (TestSuiteWithBase as any).__testSuiteInstance; const testRunnerVisitor = new TestsRunnerVisitor(this.logger); // Act diff --git a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts index 1ca472a..b57dc36 100644 --- a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts +++ b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts @@ -1,34 +1,25 @@ import { test } from '../../../lib/decorators/test.decorator'; import { testSuite } from '../../../lib/decorators/testSuite.decorator'; import { Logger } from '../../../lib/logger/logger'; -import { TestResult } from '../../../lib/reporting/report/testResult'; -import { TestCase } from '../../../lib/testCase'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; -import { TestStatus } from '../../../lib/testStatus'; -import { expect, beforeEach } from '../../../testyCore'; +import { expect, TestCase, TestResult } from '../../../testyCore'; import { NullLogger } from '../../utils/nullLogger'; import { NormalBeforeAfterTestSuite } from './normalBeforeAfterTestSuite'; -import { ThrowsDuringAfterAllTestSuite } from './throwsDuringAfterAllTestSuite'; -import { ThrowsDuringAfterEachTestSuite } from './throwsDuringAfterEachTestSuite'; import { ThrowsDuringBeforeAllTestSuite } from './throwsDuringBeforeAllTestSuite'; import { ThrowsDuringBeforeEachTestSuite } from './throwsDuringBeforeEachTestSuite'; -import { TestsLoader } from '../../../lib/utils/testsLoader'; +import { ThrowsDuringAfterEachTestSuite } from './throwsDuringAfterEachTestSuite'; +import { ThrowsDuringAfterAllTestSuite } from './throwsDuringAfterAllTestSuite'; + @testSuite('Before and After Decorators Test Suite') export class BeforeAfterDecoratorsTestSuite { - private testsLoader: TestsLoader; private logger: Logger = new NullLogger(); private visitor = new TestsRunnerVisitor(this.logger); - @beforeEach() - beforeEach() { - this.testsLoader = new TestsLoader(); - } - @test('beforeAll, beforeEach, afterEach and afterAll are called the right amount of time.') private async trivialCase() { // Arrange - const testSuite = await this.testsLoader.loadTests('./', ['beforeAfterDecorators.spec.ts'], undefined); + const testSuite = (NormalBeforeAfterTestSuite as any).__testSuiteInstance; // Act const report = await testSuite.accept(this.visitor); @@ -42,14 +33,14 @@ export class BeforeAfterDecoratorsTestSuite { } @test('Before and after methods failures', [ - new TestCase('beforeAll throws, should return a failed test report', 'throwsDuringBeforeAllTestSuite.ts', 6), - new TestCase('beforeEach throws, should return a failed test report', 'throwsDuringBeforeEachTestSuite.ts', 6), - new TestCase('afterEach throws, should return a failed test report', 'throwsDuringAfterEachTestSuite.ts', 6), - new TestCase('afterAll throws, should return a failed test report', 'throwsDuringAfterAllTestSuite.ts', 6), + new TestCase('beforeAll throws, should return a failed test report', ThrowsDuringBeforeAllTestSuite, 6), + new TestCase('beforeEach throws, should return a failed test report', ThrowsDuringBeforeEachTestSuite, 6), + new TestCase('afterEach throws, should return a failed test report', ThrowsDuringAfterEachTestSuite, 6), + new TestCase('afterAll throws, should return a failed test report', ThrowsDuringAfterAllTestSuite, 6), ]) - private async beforeOfAfterMethodFails(testFile: any, numberOfTests: number) { + private async beforeOfAfterMethodFails(testSuiteType: any, numberOfTests: number) { // Arrange - const testSuite = await this.testsLoader.loadTests('./', [testFile], undefined); + const testSuite = testSuiteType.__testSuiteInstance; // Act const report = await testSuite.accept(this.visitor); diff --git a/src/spec/decorators/testDecorator/testDecorator.spec.ts b/src/spec/decorators/testDecorator/testDecorator.spec.ts index ffdaa12..ec1ff08 100644 --- a/src/spec/decorators/testDecorator/testDecorator.spec.ts +++ b/src/spec/decorators/testDecorator/testDecorator.spec.ts @@ -1,81 +1,75 @@ -import { test, expect, testSuite, ftest } from '../../../testyCore'; -import { TestResult } from '../../../lib/reporting/report/testResult'; +import { test, expect, testSuite, ftest, TestResult } from '../../../testyCore'; import { Logger } from '../../../lib/logger/logger'; import { NullLogger } from '../../utils/nullLogger'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; -import { TestsLoader } from '../../../lib/utils/testsLoader'; -import { beforeEach } from '../../../lib/decorators/beforeEach.decorator'; +import { MultipleTestTestDecoratorTestSuite } from './multipleTestsTestDecoratorTestSuite'; +import { TestCasesTestDecoratorTestSuite } from './testCasesTestDecoratorTestSuite'; +import { TimeoutTestDecoratorTestSuite } from './timeoutTestDecoratorTestSuite'; @testSuite('Test Decorator Test Suite') export class TestDecoratorTestSuite { - private testsLoader: TestsLoader; private logger: Logger = new NullLogger(); private visitor = new TestsRunnerVisitor(this.logger); - @beforeEach() - beforeEach() { - this.testsLoader = new TestsLoader(); + @test('single test, test should be ran once') + private async singleTest() { + // Arrange + const testSuite = (TestDecoratorTestSuite as any).__testSuiteInstance; + + // Act + const report = await testSuite.accept(this.visitor); + + // Assert + expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); } - // @test('single test, test should be ran once') - // private async singleTest() { - // // Arrange - // const testSuite = await this.testsLoader.loadTests('', ['testDecoratorTestSuite.ts'], undefined); + @test('multiple test, tests should be ran once') + private async multipleTest() { + // Arrange + const testSuite = (MultipleTestTestDecoratorTestSuite as any).__testSuiteInstance; - // // Act - // const report = await testSuite.accept(this.visitor); + // Act + const report = await testSuite.accept(this.visitor); - // // Assert - // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - // } + // Assert + expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); + expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); + expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); + } - // @test('multiple test, tests should be ran once') - // private async multipleTest() { - // // Arrange - // const testSuite = await this.testsLoader.loadTests('./', ['multipleTestsTestDecoratorTestSuite.ts'], undefined); + @test('test cases, tests should be ran once') + private async testCasesTest() { + // Arrange + const testSuite = (TestCasesTestDecoratorTestSuite as any).__testSuiteInstance; - // // Act - // const report = await testSuite.accept(this.visitor); + // Act + const report = await testSuite.accept(this.visitor); - // // Assert - // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - // expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); - // expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); - // } + // Assert + expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); + expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); + expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); + } - // @test('test cases, tests should be ran once') - // private async testCasesTest() { - // // Arrange - // const testSuite = await this.testsLoader.loadTests('./', ['testCasesTestDecoratorTestSuite.ts'], undefined); + @test('test takes too long, should return failed test report') + private async timeoutTest() { + // Arrange + const testSuite = (TimeoutTestDecoratorTestSuite as any).__testSuiteInstance; - // // Act - // const report = await testSuite.accept(this.visitor); + // Act + const report = await testSuite.accept(this.visitor); - // // Assert - // expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); - // expect.toBeEqual(testSuite.context.numberOfRunsTest2, 1); - // expect.toBeEqual(testSuite.context.numberOfRunsTest3, 1); - // } + // Assert + expect.toBeEqual(report.result, TestResult.Failure); + } - // @test('test takes too long, should return failed test report') - // private async timeoutTest() { + // @test('no names, should infer from method names') + // private async noNameTest() { // // Arrange - // const testSuite = await this.testsLoader.loadTests('./', ['timeoutTestDecoratorTestSuite.ts'], undefined); + // const testSuite = await this.testsLoader.loadTests(__dirname, ['testWithoutNames.ts'], undefined); // // Act - // const report = await testSuite.accept(this.visitor); // // Assert - // expect.toBeEqual(report.result, TestResult.Failure); // } - - @test('no names, should infer from method names') - private async noNameTest() { - // Arrange - const testSuite = await this.testsLoader.loadTests(__dirname, ['testWithoutNames.ts'], undefined); - - // Act - - // Assert - } } \ No newline at end of file From eee38b9edf5b52adeb51e77dcb9a653ce4708e0f Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Fri, 14 Dec 2018 21:09:18 -0500 Subject: [PATCH 7/8] Implemented base test class to reduce duplication. Did not use it because of a bug. --- .../baseTestSuite/testWithBase.spec.ts | 12 ++--- .../beforeAfterDecorators.spec.ts | 29 +++++++---- ...ts => singleTestTestDecoratorTestSuite.ts} | 0 .../testDecorator/testDecorator.spec.ts | 48 ++++++++++++------- src/spec/decorators/testSuiteTestsBase.ts | 21 ++++++++ 5 files changed, 78 insertions(+), 32 deletions(-) rename src/spec/decorators/testDecorator/{testDecoratorTestSuite.ts => singleTestTestDecoratorTestSuite.ts} (100%) create mode 100644 src/spec/decorators/testSuiteTestsBase.ts diff --git a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts index e260941..98b42d8 100644 --- a/src/spec/decorators/baseTestSuite/testWithBase.spec.ts +++ b/src/spec/decorators/baseTestSuite/testWithBase.spec.ts @@ -1,23 +1,19 @@ import { test } from '../../../lib/decorators/test.decorator'; import { testSuite } from '../../../lib/decorators/testSuite.decorator'; -import { Logger } from '../../../lib/logger/logger'; -import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; import { expect } from '../../../testyCore'; -import { NullLogger } from '../../utils/nullLogger'; import { BaseTestSuite, TestSuiteWithBase } from './testSuiteWithBase'; +import { TestSuiteTestsBase } from '../testSuiteTestsBase'; @testSuite('Test Suite With Base Test Suite Tests') -export class BeforeAfterDecoratorsTestSuite { - private logger: Logger = new NullLogger(); +export class BeforeAfterDecoratorsTestSuite extends TestSuiteTestsBase { @test('the base and the actual test suite before and after methods are called.') private async trivialCase() { // Arrange - const testSuite = (TestSuiteWithBase as any).__testSuiteInstance; - const testRunnerVisitor = new TestsRunnerVisitor(this.logger); + const testSuite = this.getTestSuiteInstance(TestSuiteWithBase); // Act - const report = await testSuite.accept(testRunnerVisitor); + await testSuite.accept(this.visitor); // Assert expect.toBeEqual(testSuite.context.beforeAllExecuted[0], BaseTestSuite); diff --git a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts index b57dc36..a886480 100644 --- a/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts +++ b/src/spec/decorators/beforeAfterDecorators/beforeAfterDecorators.spec.ts @@ -1,25 +1,34 @@ import { test } from '../../../lib/decorators/test.decorator'; import { testSuite } from '../../../lib/decorators/testSuite.decorator'; -import { Logger } from '../../../lib/logger/logger'; -import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; -import { expect, TestCase, TestResult } from '../../../testyCore'; -import { NullLogger } from '../../utils/nullLogger'; +import { expect, TestCase, TestResult, beforeEach } from '../../../testyCore'; import { NormalBeforeAfterTestSuite } from './normalBeforeAfterTestSuite'; import { ThrowsDuringBeforeAllTestSuite } from './throwsDuringBeforeAllTestSuite'; import { ThrowsDuringBeforeEachTestSuite } from './throwsDuringBeforeEachTestSuite'; import { ThrowsDuringAfterEachTestSuite } from './throwsDuringAfterEachTestSuite'; import { ThrowsDuringAfterAllTestSuite } from './throwsDuringAfterAllTestSuite'; +import { NullLogger } from '../../utils/nullLogger'; +import { Logger } from '../../../lib/logger/logger'; +import { TestsVisitor } from '../../../lib/tests/visitors/testVisitor'; +import { Report } from '../../../lib/reporting/report/report'; +import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; +import { TestSuite } from '../../../lib/tests/testSuite'; @testSuite('Before and After Decorators Test Suite') export class BeforeAfterDecoratorsTestSuite { + // TODO: This test suite should extend TestSuiteTestsBase when #8 is fixed. private logger: Logger = new NullLogger(); - private visitor = new TestsRunnerVisitor(this.logger); + protected visitor: TestsVisitor; + + @beforeEach() + private beforeEach() { + this.visitor = new TestsRunnerVisitor(this.logger); + } @test('beforeAll, beforeEach, afterEach and afterAll are called the right amount of time.') private async trivialCase() { // Arrange - const testSuite = (NormalBeforeAfterTestSuite as any).__testSuiteInstance; + const testSuite = this.getTestSuiteInstance(NormalBeforeAfterTestSuite); // Act const report = await testSuite.accept(this.visitor); @@ -38,9 +47,9 @@ export class BeforeAfterDecoratorsTestSuite { new TestCase('afterEach throws, should return a failed test report', ThrowsDuringAfterEachTestSuite, 6), new TestCase('afterAll throws, should return a failed test report', ThrowsDuringAfterAllTestSuite, 6), ]) - private async beforeOfAfterMethodFails(testSuiteType: any, numberOfTests: number) { + private async beforeOfAfterMethodFails(testSuiteClass: any, numberOfTests: number) { // Arrange - const testSuite = testSuiteType.__testSuiteInstance; + const testSuite = this.getTestSuiteInstance(testSuiteClass); // Act const report = await testSuite.accept(this.visitor); @@ -50,5 +59,9 @@ export class BeforeAfterDecoratorsTestSuite { expect.toBeEqual(report.result, TestResult.Failure); expect.toBeEqual(report.numberOfTests, numberOfTests, 'Expected all tests to be part of the report.'); } + + protected getTestSuiteInstance(testClass: any): TestSuite { + return testClass.__testSuiteInstance; + } } diff --git a/src/spec/decorators/testDecorator/testDecoratorTestSuite.ts b/src/spec/decorators/testDecorator/singleTestTestDecoratorTestSuite.ts similarity index 100% rename from src/spec/decorators/testDecorator/testDecoratorTestSuite.ts rename to src/spec/decorators/testDecorator/singleTestTestDecoratorTestSuite.ts diff --git a/src/spec/decorators/testDecorator/testDecorator.spec.ts b/src/spec/decorators/testDecorator/testDecorator.spec.ts index ec1ff08..b916cd6 100644 --- a/src/spec/decorators/testDecorator/testDecorator.spec.ts +++ b/src/spec/decorators/testDecorator/testDecorator.spec.ts @@ -1,23 +1,34 @@ -import { test, expect, testSuite, ftest, TestResult } from '../../../testyCore'; -import { Logger } from '../../../lib/logger/logger'; -import { NullLogger } from '../../utils/nullLogger'; -import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; +import { test, expect, testSuite, TestResult, beforeEach } from '../../../testyCore'; import { MultipleTestTestDecoratorTestSuite } from './multipleTestsTestDecoratorTestSuite'; import { TestCasesTestDecoratorTestSuite } from './testCasesTestDecoratorTestSuite'; import { TimeoutTestDecoratorTestSuite } from './timeoutTestDecoratorTestSuite'; +import { TestWithNoNamesTestSuite } from './testWithoutNames'; +import { NullLogger } from '../../utils/nullLogger'; +import { Logger } from '../../../lib/logger/logger'; +import { TestsVisitor } from '../../../lib/tests/visitors/testVisitor'; +import { Report } from '../../../lib/reporting/report/report'; +import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; +import { TestSuite } from '../../../lib/tests/testSuite'; +import { SingleTestTestDecoratorTestSuite } from './singleTestTestDecoratorTestSuite'; @testSuite('Test Decorator Test Suite') export class TestDecoratorTestSuite { + // TODO: This test suite should extend TestSuiteTestsBase when #8 is fixed. private logger: Logger = new NullLogger(); - private visitor = new TestsRunnerVisitor(this.logger); + protected visitor: TestsVisitor; + + @beforeEach() + private beforeEach() { + this.visitor = new TestsRunnerVisitor(this.logger); + } @test('single test, test should be ran once') private async singleTest() { // Arrange - const testSuite = (TestDecoratorTestSuite as any).__testSuiteInstance; + const testSuite = (SingleTestTestDecoratorTestSuite as any).__testSuiteInstance; // Act - const report = await testSuite.accept(this.visitor); + await testSuite.accept(this.visitor); // Assert expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); @@ -29,7 +40,7 @@ export class TestDecoratorTestSuite { const testSuite = (MultipleTestTestDecoratorTestSuite as any).__testSuiteInstance; // Act - const report = await testSuite.accept(this.visitor); + await testSuite.accept(this.visitor); // Assert expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); @@ -43,7 +54,7 @@ export class TestDecoratorTestSuite { const testSuite = (TestCasesTestDecoratorTestSuite as any).__testSuiteInstance; // Act - const report = await testSuite.accept(this.visitor); + await testSuite.accept(this.visitor); // Assert expect.toBeEqual(testSuite.context.numberOfRunsTest1, 1); @@ -63,13 +74,18 @@ export class TestDecoratorTestSuite { expect.toBeEqual(report.result, TestResult.Failure); } - // @test('no names, should infer from method names') - // private async noNameTest() { - // // Arrange - // const testSuite = await this.testsLoader.loadTests(__dirname, ['testWithoutNames.ts'], undefined); + @test('no names, should infer from method names') + private async noNameTest() { + // Arrange + const testSuite = (TestWithNoNamesTestSuite as any).__testSuiteInstance; + + // Act + const report = testSuite.accept(this.visitor); - // // Act + // Assert + } - // // Assert - // } + protected getTestSuiteInstance(testClass: any): TestSuite { + return testClass.__testSuiteInstance; + } } \ No newline at end of file diff --git a/src/spec/decorators/testSuiteTestsBase.ts b/src/spec/decorators/testSuiteTestsBase.ts new file mode 100644 index 0000000..73445e1 --- /dev/null +++ b/src/spec/decorators/testSuiteTestsBase.ts @@ -0,0 +1,21 @@ +import { TestsRunnerVisitor } from '../../lib/tests/visitors/runnerVisitor'; +import { Logger } from '../../lib/logger/logger'; +import { NullLogger } from '../utils/nullLogger'; +import { beforeEach } from '../../lib/decorators/beforeEach.decorator'; +import { TestsVisitor } from '../../lib/tests/visitors/testVisitor'; +import { Report } from '../../lib/reporting/report/report'; +import { TestSuite } from '../../lib/tests/testSuite'; + +export class TestSuiteTestsBase { + private logger: Logger = new NullLogger(); + protected visitor: TestsVisitor; + + @beforeEach() + private beforeEach() { + this.visitor = new TestsRunnerVisitor(this.logger); + } + + protected getTestSuiteInstance(testClass: any): TestSuite { + return testClass.__testSuiteInstance; + } +} \ No newline at end of file From 7a4fa8161fe01ab77182cd5d640938bec03c1fc8 Mon Sep 17 00:00:00 2001 From: Antoine Boisier-Michaud Date: Fri, 14 Dec 2018 21:14:12 -0500 Subject: [PATCH 8/8] Added implicit names test --- .../decorators/testDecorator/testDecorator.spec.ts | 14 ++++++++++---- .../decorators/testDecorator/testWithoutNames.ts | 6 +++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/spec/decorators/testDecorator/testDecorator.spec.ts b/src/spec/decorators/testDecorator/testDecorator.spec.ts index b916cd6..0ead13a 100644 --- a/src/spec/decorators/testDecorator/testDecorator.spec.ts +++ b/src/spec/decorators/testDecorator/testDecorator.spec.ts @@ -10,6 +10,7 @@ import { Report } from '../../../lib/reporting/report/report'; import { TestsRunnerVisitor } from '../../../lib/tests/visitors/runnerVisitor'; import { TestSuite } from '../../../lib/tests/testSuite'; import { SingleTestTestDecoratorTestSuite } from './singleTestTestDecoratorTestSuite'; +import { FailedTestsReportVisitor } from '../../../lib/tests/visitors/failedTestsReportVisitor'; @testSuite('Test Decorator Test Suite') export class TestDecoratorTestSuite { @@ -77,12 +78,17 @@ export class TestDecoratorTestSuite { @test('no names, should infer from method names') private async noNameTest() { // Arrange - const testSuite = (TestWithNoNamesTestSuite as any).__testSuiteInstance; - - // Act - const report = testSuite.accept(this.visitor); + const testSuite = this.getTestSuiteInstance(TestWithNoNamesTestSuite); // Assert + expect.toBeIn('myTest1', testSuite.testIds); + expect.toBeIn('myTest2', testSuite.testIds); + expect.toBeIn('myTest3', testSuite.testIds); + + const myTest3 = testSuite.get('myTest3') as TestSuite; + expect.toBeIn('myTestCase1', myTest3.testIds); + expect.toBeIn('myTestCase2', myTest3.testIds); + expect.toBeIn('myTestCase3', myTest3.testIds); } protected getTestSuiteInstance(testClass: any): TestSuite { diff --git a/src/spec/decorators/testDecorator/testWithoutNames.ts b/src/spec/decorators/testDecorator/testWithoutNames.ts index 5093a33..62b3937 100644 --- a/src/spec/decorators/testDecorator/testWithoutNames.ts +++ b/src/spec/decorators/testDecorator/testWithoutNames.ts @@ -11,9 +11,9 @@ export class TestWithNoNamesTestSuite { private myTest2() { } @test(undefined, [ - new TestCase('testCase1'), - new TestCase('testCase2'), - new TestCase('testCase3') + new TestCase('myTestCase1'), + new TestCase('myTestCase2'), + new TestCase('myTestCase3') ]) private myTest3() { } } \ No newline at end of file