diff --git a/README.md b/README.md index 55a3860..26a4166 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,16 @@ Passing in the config overrides any default values or values set by bot-tester.j ignoreTypingEvent: boolean, ignoreEndOfConversationEvent: boolean, // Setting this to true will cause checkSession to hang and test to fail. - ignoreInternalSaveMessage: boolean; + ignoreInternalSaveMessage: boolean, + // this will allow using different assertion libraries. Currently supported libraries are: + // 1) chai + + // with the following under development: + // 2) ava + + // should this field not be provided, it will default to chai + assertionLibrary: string, } ``` if timeout is defined, then a particular ```runTest()``` call will fail if it does not receive each expected message within the timeout period of time set in the options. diff --git a/package.json b/package.json index 04611a2..9a75e8e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "lint:fix": "tslint 'src/**/*.ts' --fix", "cleanBuild": "npm run clean && npm run build", "watch": "tsc -w", - "test": "node_modules/mocha/bin/_mocha --require node_modules/ts-node/register test/*.spec.ts", + "test": "node_modules/mocha/bin/_mocha --require node_modules/ts-node/register test/**/*.spec.ts", "buildTypeDocs": "yarn cleanBuild && rm -rf docs && typedoc --excludeNotExported --out docs --hideGenerator src/BotTester.ts --module commonjs --gaID UA-105358964-1 --readme none && touch docs/.nojekyll", "ci": "npm run lint && npm run test", "coverage": "npm run coverageTests && nyc report --reporter=text-lcov | node_modules/.bin/coveralls", diff --git a/src/BotTester.ts b/src/BotTester.ts index d297f3b..9869099 100644 --- a/src/BotTester.ts +++ b/src/BotTester.ts @@ -242,13 +242,13 @@ export class BotTester implements IBotTester, IOptionsModifier { if (!expectedResponses) { expectedMessages = []; } else if (!(expectedResponses instanceof Array)) { - expectedMessages = [new ExpectedMessage(expectedResponses)]; + expectedMessages = [new ExpectedMessage(this.config, expectedResponses)]; } else if (expectedResponses instanceof Array) { if (expectedResponses.length > 0) { expectedMessages = (expectedResponses as PossibleExpectedMessageCollections[]) .map((currentExpectedResponseCollection: PossibleExpectedMessageCollections) => - new ExpectedMessage(currentExpectedResponseCollection)); + new ExpectedMessage(this.config, currentExpectedResponseCollection)); } } diff --git a/src/ExpectedMessage.ts b/src/ExpectedMessage.ts index f5c783c..99e65b4 100644 --- a/src/ExpectedMessage.ts +++ b/src/ExpectedMessage.ts @@ -1,5 +1,6 @@ import { IEvent, IMessage } from 'botbuilder'; -import { expect } from './assertionLibraries/IExpectation'; +import { BotTesterExpectation } from './assertionLibraries/BotTesterExpectation'; +import { IConfig } from './config'; export enum ExpectedMessageType { String, @@ -35,19 +36,23 @@ function getExpectedMessageType(expectedResponseCollection: PossibleExpectedMess * response from the bot. */ export class ExpectedMessage { + private readonly internalExpectation: BotTesterExpectation; /** * set of possible responses, chosen at random. If the collection size is 1, then there is only one response that is expected */ private readonly expectedResponseCollection: PossibleExpectedMessageCollections; - constructor(expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections) { + constructor( + config: IConfig, + expectedResponseCollection: PossibleExpectedMessageType | PossibleExpectedMessageCollections + ) { + this.internalExpectation = new BotTesterExpectation(config); + if (!(expectedResponseCollection instanceof Array)) { this.expectedResponseCollection = [expectedResponseCollection as PossibleExpectedMessageType]; } else { this.expectedResponseCollection = expectedResponseCollection as PossibleExpectedMessageCollections; } - - expect(this.expectedResponseCollection, 'expected response collections cannot be empty').notToBeEmpty(); } /** @@ -69,7 +74,7 @@ export class ExpectedMessage { this.deepMessageMatchCheck(outgoingMessage); break; default: - expect(outgoingMessage.type).toEqual('save'); + this.internalExpectation.expect(outgoingMessage.type).toEqual('save'); } } @@ -92,7 +97,7 @@ export class ExpectedMessage { const errorString = `Bot should have responded with '${errorStringExpectedResponseText}', but was '${outgoingText}'`; - expect(expectedResponseStrings, errorString).toInclude(outgoingText); + this.internalExpectation.expect(expectedResponseStrings, errorString).toInclude(outgoingText); } /** @@ -104,8 +109,9 @@ export class ExpectedMessage { private checkMessageTextForRegex(outgoingMessage: IMessage): void { const text = outgoingMessage.text; const regexCollection: RegExp[] = this.expectedResponseCollection as RegExp[]; - expect(regexCollection.some((regex: RegExp) => regex.test(text)), - `'${text}' did not match any regex in ${regexCollection}`).toBeTrue(); + + this.internalExpectation.expect(regexCollection.some((regex: RegExp) => regex.test(text)), + `'${text}' did not match any regex in ${regexCollection}`).toBeTrue(); } /** @@ -128,6 +134,6 @@ export class ExpectedMessage { this.checkMessageTextForExactStringMatch(outgoingMessage, expectedResponseStrings); } - expect(expectedResponseCollectionAsIMessage).toDeeplyInclude(outgoingMessage); + this.internalExpectation.expect(expectedResponseCollectionAsIMessage).toDeeplyInclude(outgoingMessage); } } diff --git a/src/assertionLibraries/AssertionLibrary.ts b/src/assertionLibraries/AssertionLibrary.ts new file mode 100644 index 0000000..cdd54cd --- /dev/null +++ b/src/assertionLibraries/AssertionLibrary.ts @@ -0,0 +1,4 @@ +export enum AssertionLibrary { + AVA = 'ava', + CHAI = 'chai' +} diff --git a/src/assertionLibraries/BotTesterExpectation.ts b/src/assertionLibraries/BotTesterExpectation.ts new file mode 100644 index 0000000..0575f97 --- /dev/null +++ b/src/assertionLibraries/BotTesterExpectation.ts @@ -0,0 +1,24 @@ +import { IConfig } from './../config'; +import { AssertionLibrary } from './AssertionLibrary'; +import { ChaiExpectation } from './ChaiExpectation'; +import { IExpectation } from './IExpectation'; + +export type expectationFunction = (subject: {}, message?: string) => IExpectation; + +export class BotTesterExpectation { + private readonly assertionLibrary: string; + + constructor(config: IConfig) { + this.assertionLibrary = config.assertionLibrary; + } + + public expect(subject: {}, message?: string): IExpectation { + switch (this.assertionLibrary) { + case AssertionLibrary.AVA: + throw new Error('ava is not yet supported'); + case AssertionLibrary.CHAI: + default: + return new ChaiExpectation(subject, message); + } + } +} diff --git a/src/assertionLibraries/IExpectation.ts b/src/assertionLibraries/IExpectation.ts index 0f53090..7fb26ec 100644 --- a/src/assertionLibraries/IExpectation.ts +++ b/src/assertionLibraries/IExpectation.ts @@ -1,5 +1,3 @@ -import { ChaiExpectation } from './ChaiExpectation'; - /** * framework based extractions. New frameworks will implement this interface to be able to be used seamlessly within the framework */ @@ -10,24 +8,3 @@ export interface IExpectation { toBeTrue(): void; toDeeplyInclude(value: {}): void; } - -/** - * Creates a wrapper instance around an assertion class - * - * @param subject object being tested against - * @param message error message to print if expectation fails - */ -export function expect(subject: {}, message?: string): IExpectation { - return createExpectation(subject, message); -} - -function createExpectation(subject: {}, message?: string): IExpectation { - const name = 'chai'; - - switch (name) { - case 'chai': - return new ChaiExpectation(subject, message); - default: - return new ChaiExpectation(subject, message); - } -} diff --git a/src/config.ts b/src/config.ts index bdd22f4..2c0a86d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,6 @@ import { IAddress, IMessage } from 'botbuilder'; import * as fs from 'fs'; +import { AssertionLibrary } from './assertionLibraries/AssertionLibrary'; import { ignoreInternalSaveMessageFilter } from './builtInMessageFilters'; /** @@ -8,6 +9,17 @@ import { ignoreInternalSaveMessageFilter } from './builtInMessageFilters'; export type MessageFilter = (message: IMessage) => boolean; export interface IConfig { + /** + * assertion library to use when running the tests. Valid libraries/strings/values are: + * 1) chai + * + * chai is the default library if none is provided + * libraries being developed: + * 1) Ava + * + * to request support for a new assertion library, check this thread and submit a comment + */ + assertionLibrary?: AssertionLibrary | string; /** * timeout in milliseconds before a BotTester runner will fail a test (when not overriden) */ @@ -48,6 +60,7 @@ export const NO_TIMEOUT = -1; const defaultConfig: IConfig = { timeout: NO_TIMEOUT, + assertionLibrary: AssertionLibrary.CHAI, defaultAddress: { channelId: 'console', user: { id: 'user1', name: 'user1' }, @@ -74,6 +87,8 @@ export function getConfig(): IConfig { configInternal.messageFilters = []; + configInternal.assertionLibrary = configInternal.assertionLibrary || AssertionLibrary.CHAI; + if (configInternal.ignoreInternalSaveMessage) { configInternal.messageFilters.push(ignoreInternalSaveMessageFilter); } diff --git a/test/BotTester.spec.ts b/test/mocha/chai/BotTester.spec.ts similarity index 98% rename from test/BotTester.spec.ts rename to test/mocha/chai/BotTester.spec.ts index 4f8285b..2f90102 100644 --- a/test/BotTester.spec.ts +++ b/test/mocha/chai/BotTester.spec.ts @@ -2,9 +2,9 @@ import { IAddress, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder'; import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; -import { BotTester } from './../src/BotTester'; -import { TestConnector } from './../src/TestConnector'; -import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './adaptiveCardProvider'; +import { BotTester } from './../../../src/BotTester'; +import { TestConnector } from './../../../src/TestConnector'; +import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './../../adaptiveCardProvider'; chai.use(chaiAsPromised); const expect = chai.expect; diff --git a/test/BotTesterFailure.spec.ts b/test/mocha/chai/BotTesterFailure.spec.ts similarity index 98% rename from test/BotTesterFailure.spec.ts rename to test/mocha/chai/BotTesterFailure.spec.ts index f36dfa6..bc52074 100644 --- a/test/BotTesterFailure.spec.ts +++ b/test/mocha/chai/BotTesterFailure.spec.ts @@ -1,9 +1,9 @@ import { IAddress, IDialogResult, IMessage, Message, Prompts, Session, UniversalBot } from 'botbuilder'; import * as chai from 'chai'; import * as chaiAsPromised from 'chai-as-promised'; -import { BotTester } from './../src/BotTester'; -import { TestConnector } from './../src/TestConnector'; -import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './adaptiveCardProvider'; +import { BotTester } from './../../../src/BotTester'; +import { TestConnector } from './../../../src/TestConnector'; +import { getAdaptiveCard, getAdaptiveCardAttachment, getAdaptiveCardMessage } from './../../adaptiveCardProvider'; chai.use(chaiAsPromised);