diff --git a/mod.ts b/mod.ts index f6a0c77..28a4dbc 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,5 @@ import driver from './src/driver.ts' -import types from './src/types.ts' +import type Types from './src/types.ts' -export { driver, types } +export { driver } +export type { Types } diff --git a/src/constants.ts b/src/constants.ts index 4c87c05..108d77b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -12,7 +12,8 @@ const driverBrowsers: Record = { const seleniumExceptions: Record = { WebDriverError: 'General WebDriver error.', NoSuchElementError: 'The requested element could not be found in the DOM.', - TimeoutError: 'The operation did not complete within the specified timeout.', + TimeoutError: + 'The operation did not complete within the specified timeout.', SessionNotCreatedError: 'A new session could not be created.', ScriptTimeoutError: 'A script execution timeout occurred.', StaleElementReferenceError: diff --git a/src/driver.ts b/src/driver.ts index e64bd5c..f7cce97 100644 --- a/src/driver.ts +++ b/src/driver.ts @@ -1,13 +1,13 @@ import { assert, Builder, By, isEmpty, join, Kia } from '../deps.ts' import type { - TCaseFn, - TConfigJSON, - TData, - TDriverParams, - TDriverServiceCaseParamsBuilder, - TDrowserDriverResponse, - TDrowserServiceCase, - TDrowserThenableWebDriver, + CaseFn, + ConfigJSON, + Data, + DriverParams, + DriverServiceCaseParamsBuilder, + DrowserDriverResponse, + DrowserServiceCase, + DrowserThenableWebDriver, } from './types.ts' import { isValidHttpUrl, result as resultData } from './utils.ts' import { @@ -20,13 +20,13 @@ import { exportGeneratedLog, exportJSONReport } from './export.ts' const driver = async ({ browser, -}: TDriverParams): Promise => { - const data: TData = { url: '', results: [] } +}: DriverParams): Promise => { + const data: Data = { url: '', results: [] } const configPath = join(Deno.cwd(), 'drowser.json') try { await Deno.stat(configPath) - const { url }: TConfigJSON = JSON.parse( + const { url }: ConfigJSON = JSON.parse( await Deno.readTextFile(configPath), ) @@ -39,7 +39,9 @@ const driver = async ({ data.url = url } catch (error) { if (error instanceof Deno.errors.NotFound) { - throw new Error('An error occurred, please create drowser.json file.') + throw new Error( + 'An error occurred, please create drowser.json file.', + ) } if (!(error instanceof Deno.errors.NotFound)) { @@ -50,15 +52,17 @@ const driver = async ({ } if (isEmpty(browser) || !driverBrowserList.includes(browser)) { - throw new Error('An error occurred, please provide a valid browser driver') + throw new Error( + 'An error occurred, please provide a valid browser driver', + ) } - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { if (isEmpty(data.url) || !isValidHttpUrl({ url: data.url })) reject() const builder = new Builder() .forBrowser(driverBrowsers[browser]) - .build() as TDrowserThenableWebDriver + .build() as DrowserThenableWebDriver const service = { cases: [] } @@ -75,16 +79,16 @@ const driver = async ({ .finally(() => { const methodPromises: Promise[] = [] - service.cases.forEach((c: TDrowserServiceCase) => { + service.cases.forEach((c: DrowserServiceCase) => { if (typeof c === 'object') { const omitedBuilder = - builder as unknown as TDriverServiceCaseParamsBuilder + builder as unknown as DriverServiceCaseParamsBuilder const megaBuilder = { builder: omitedBuilder, assert, by: By, } - const method = c.fn as TCaseFn + const method = c.fn as CaseFn const methodPromise = method(megaBuilder) const start = performance.now() diff --git a/src/export.ts b/src/export.ts index daa60dd..14a1932 100644 --- a/src/export.ts +++ b/src/export.ts @@ -17,15 +17,15 @@ import { } from './utils.ts' import { DataPoint, + DataResult, + DriverBrowser, MonthCount, MonthValue, - TDataResult, - TDriverBrowser, - TJSON, + ReportSchema, } from './types.ts' const exportGeneratedLog = ( - { results }: { results: Array }, + { results }: { results: Array }, ): void => { const dirPath = join(Deno.cwd(), 'drowser/logs') const hasDir = existsSync(dirPath) @@ -40,7 +40,8 @@ const exportGeneratedLog = ( const writeResult = () => results.forEach((r) => { - const logRow = `[${r.timestamp}] - Test with ${r.name} is ${r.status}` + const logRow = + `[${r.timestamp}] - Test with ${r.name} is ${r.status}` Deno.writeTextFile(logFilePath, `${logRow}\n`, { append: true }) }) @@ -55,8 +56,8 @@ const exportGeneratedLog = ( const exportJSONReport = ( { results, browser }: { - results: Array - browser: TDriverBrowser + results: Array + browser: DriverBrowser }, ): void => { const filePath = join(Deno.cwd(), 'drowser-reports.json') @@ -73,7 +74,7 @@ const exportJSONReport = ( } if (Array.isArray(results) && results.length > 0) { - const jsonData = readJsonSync(filePath) as TJSON + const jsonData = readJsonSync(filePath) as ReportSchema const month = getCurrentMonth({ type: 'short' }) @@ -83,11 +84,12 @@ const exportJSONReport = ( } } - const flatedTotalTests = jsonData.drowser.metadata.current_month === month - ? jsonData.drowser.cases.flatMap((item) => item.cases).filter((c) => - c.month_of_test === month - ) - : [] + const flatedTotalTests = + jsonData.drowser.metadata.current_month === month + ? jsonData.drowser.cases.flatMap((item) => item.cases).filter(( + c, + ) => c.month_of_test === month) + : [] const totalTests = [ ...flatedTotalTests, ...results, @@ -120,7 +122,9 @@ const exportJSONReport = ( ({ status }: { status: string }) => status, ) - const CombinedTestCoverage = getCoverage({ results: CombinedTotalTests }) + const CombinedTestCoverage = getCoverage({ + results: CombinedTotalTests, + }) const CombinedAvgTestDuration = getAverageDuration({ results: CombinedTotalTests, @@ -149,15 +153,15 @@ const exportJSONReport = ( .data as DataPoint[] ?? [], }, ], - passing_tests: - jsonData?.drowser?.metrics?.graphs?.passing_tests as MonthCount[] ?? - [], - failed_tests: - jsonData?.drowser?.metrics?.graphs?.failed_tests as MonthCount[] ?? - [], - test_coverage: - jsonData?.drowser?.metrics?.graphs?.test_coverage as MonthCount[] ?? - [], + passing_tests: jsonData?.drowser?.metrics?.graphs + ?.passing_tests as MonthCount[] ?? + [], + failed_tests: jsonData?.drowser?.metrics?.graphs + ?.failed_tests as MonthCount[] ?? + [], + test_coverage: jsonData?.drowser?.metrics?.graphs + ?.test_coverage as MonthCount[] ?? + [], avg_test_duration: jsonData?.drowser?.metrics?.graphs ?.avg_test_duration as MonthCount[] ?? [], flaky_tests: jsonData?.drowser?.metrics?.graphs diff --git a/src/types.ts b/src/types.ts index 773236f..cd7b8d3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,84 +1,84 @@ import { assert } from '../deps.ts' import type { By, ThenableWebDriver } from '../deps.ts' -export type TDriverParams = { - browser: TDriverBrowser +export type DriverParams = { + browser: DriverBrowser } -export type TDriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge' +export type DriverBrowser = 'chrome' | 'firefox' | 'safari' | 'edge' -export type TConfigJSON = { +export type ConfigJSON = { url: string exportPdf: boolean } -export type TDataResult = { +export type DataResult = { id?: string name: string status: string timestamp?: Date duration: number month_of_test?: string - browser: TDriverBrowser + browser: DriverBrowser } -export type TData = { +export type Data = { url: string - results: Array + results: Array } -export type TDrowserThenableWebDriver = ThenableWebDriver +export type DrowserThenableWebDriver = ThenableWebDriver -export type TDrowserBuilder = Omit< +export type DrowserBuilder = Omit< ThenableWebDriver, 'get' > -export type TDriverServiceCaseParamsBuilder = Omit< +export type DriverServiceCaseParamsBuilder = Omit< ThenableWebDriver, 'get' | 'quit' | 'then' | 'catch' | 'close' | 'finally' > -export type TDriverServiceCaseParamsAssert = typeof assert +export type DriverServiceCaseParamsAssert = typeof assert -export type TDriverServiceCaseParamsBy = typeof By +export type DriverServiceCaseParamsBy = typeof By -export type TDriverBrowserCaseParams = { - builder: TDriverServiceCaseParamsBuilder - assert: TDriverServiceCaseParamsAssert - by: TDriverServiceCaseParamsBy +export type DriverBrowserCaseParams = { + builder: DriverServiceCaseParamsBuilder + assert: DriverServiceCaseParamsAssert + by: DriverServiceCaseParamsBy } -export type TDrowserServiceCase = { +export type DrowserServiceCase = { name: string fn: ( - params: TDriverBrowserCaseParams, + params: DriverBrowserCaseParams, ) => void } -export type TDrowserService = { - cases: Array +export type DrowserService = { + cases: Array } -export type TCaseFn = ( - params: TDriverBrowserCaseParams, +export type CaseFn = ( + params: DriverBrowserCaseParams, ) => Promise -export type TDrowserDriverResponse = { - service: TDrowserService +export type DrowserDriverResponse = { + service: DrowserService } -export type TAssertFunction = ( +export type AssertFunction = ( actual: unknown, expected: unknown, msg?: string, ) => void -export type TAssertError = { +export type AssertError = { name: string } -export type TIsValidHttpUrlParams = { +export type IsValidHttpUrlParams = { url: string } @@ -102,7 +102,7 @@ export type MonthValue = { value: number } -export type TJSON = { +export type ReportSchema = { drowser: { metadata: { current_month: string @@ -131,35 +131,36 @@ export type TJSON = { coverage: number flaky: number month_of_test: string - browser: TDriverBrowser - cases: Array + browser: DriverBrowser + cases: Array }, ] } } -const types = { - TDriverParams: {} as TDriverParams, - TDriverBrowser: {} as TDriverBrowser, - TConfigJSON: {} as TConfigJSON, - TData: {} as TData, - TDrowserThenableWebDriver: {} as TDrowserThenableWebDriver, - TDrowserBuilder: {} as TDrowserBuilder, - TDriverServiceCaseParamsBuilder: {} as TDriverServiceCaseParamsBuilder, - TDriverServiceCaseParamsAssert: {} as TDriverServiceCaseParamsAssert, - TDriverServiceCaseParamsBy: {} as TDriverServiceCaseParamsBy, - TDriverBrowserCaseParams: {} as TDriverBrowserCaseParams, - TDrowserServiceCase: {} as TDrowserServiceCase, - TDrowserService: {} as TDrowserService, - TCaseFn: {} as TCaseFn, - TDrowserDriverResponse: {} as TDrowserDriverResponse, - TAssertFunction: {} as TAssertFunction, - TAssertError: {} as TAssertError, - TIsValidHttpUrlParams: {} as TIsValidHttpUrlParams, +const Types = { + TDriverParams: {} as DriverParams, + DriverBrowser: {} as DriverBrowser, + ConfigJSON: {} as ConfigJSON, + Data: {} as Data, + DrowserThenableWebDriver: {} as DrowserThenableWebDriver, + DrowserBuilder: {} as DrowserBuilder, + DriverServiceCaseParamsBuilder: {} as DriverServiceCaseParamsBuilder, + DriverServiceCaseParamsAssert: {} as DriverServiceCaseParamsAssert, + DriverServiceCaseParamsBy: {} as DriverServiceCaseParamsBy, + DriverBrowserCaseParams: {} as DriverBrowserCaseParams, + DrowserServiceCase: {} as DrowserServiceCase, + DrowserService: {} as DrowserService, + CaseFn: {} as CaseFn, + DrowserDriverResponse: {} as DrowserDriverResponse, + AssertFunction: {} as AssertFunction, + AssertError: {} as AssertError, + IsValidHttpUrlParams: {} as IsValidHttpUrlParams, DataPoint: {} as DataPoint, DataSet: {} as DataSet, MonthCount: {} as MonthCount, MonthValue: {} as MonthValue, + ReportSchema: {} as ReportSchema, } -export default types +export default Types diff --git a/src/utils.ts b/src/utils.ts index 255b3b4..3bee3a3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,8 @@ import { nanoid } from '../deps.ts' -import { TDataResult, TIsValidHttpUrlParams } from './types.ts' +import { DataResult, IsValidHttpUrlParams } from './types.ts' import { caseStatus } from './constants.ts' -const isValidHttpUrl = ({ url }: TIsValidHttpUrlParams): boolean => { +const isValidHttpUrl = ({ url }: IsValidHttpUrlParams): boolean => { try { const newUrl = new URL(url) return newUrl.protocol === 'http:' || newUrl.protocol === 'https:' @@ -49,13 +49,13 @@ const humanizeDuration = (durationMs: number): string => { return humanized.trim() } -const getAverageDuration = ({ results }: { results: Array }) => { +const getAverageDuration = ({ results }: { results: Array }) => { const totalDuration = results.reduce((sum, r) => sum + r.duration, 0) const averageDuration = totalDuration / results.length return averageDuration } -const getCoverage = ({ results }: { results: Array }) => { +const getCoverage = ({ results }: { results: Array }) => { const totalTests = results.length const passedTests = results.filter((r) => r.status === caseStatus.passed).length @@ -63,7 +63,7 @@ const getCoverage = ({ results }: { results: Array }) => { return coveragePercentage } -const getFlaky = ({ results }: { results: Array }) => { +const getFlaky = ({ results }: { results: Array }) => { const flakyTestCount = (() => { const resultMap = new Map() @@ -120,7 +120,7 @@ const result = ( status, duration, browser, - }: TDataResult, + }: DataResult, ) => { return { id: nanoid(),