From 428a70d174f28e0c6ae807d9494928d4891412f5 Mon Sep 17 00:00:00 2001 From: "Sergey M." Date: Fri, 18 Oct 2024 18:02:19 +1000 Subject: [PATCH] Fix the tests --- src/sources/app_version.test.ts | 4 +++- src/sources/document_element_keys.test.ts | 9 --------- src/sources/error_trace.test.ts | 4 ++-- src/sources/rtt.test.ts | 4 ++-- src/sources/user_agent.test.ts | 9 +++------ src/sources/webdriver.test.ts | 8 +------- tests/utils.ts | 20 ++++++++++++++++++++ 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/sources/app_version.test.ts b/src/sources/app_version.test.ts index 3da6c01e..e612e6b4 100644 --- a/src/sources/app_version.test.ts +++ b/src/sources/app_version.test.ts @@ -1,4 +1,5 @@ import { + getBrowserEngineVersion, getBrowserVersion, getOsMajorVersion, isChromium, @@ -13,15 +14,16 @@ describe('Sources', () => { describe('appVersion', () => { it('returns an expected value', () => { const value = getAppVersion() - const version = getBrowserVersion() ?? { major: 0, minor: 0 } if (isWebKit()) { + const version = getBrowserVersion() ?? { major: 0, minor: 0 } expect(navigator.userAgent).toContain(value) expect(value).toContain(`Version/${version.major}.${version.minor}`) return } if (isChromium()) { + const version = getBrowserEngineVersion() ?? { major: 0, minor: 0 } expect(navigator.userAgent).toContain(value) expect(value).toContain(`Chrome/${version.major}.${version.minor}`) return diff --git a/src/sources/document_element_keys.test.ts b/src/sources/document_element_keys.test.ts index 9977482c..131137ce 100644 --- a/src/sources/document_element_keys.test.ts +++ b/src/sources/document_element_keys.test.ts @@ -1,17 +1,8 @@ -import { getBrowserMajorVersion, isChromium } from '../../tests/utils' -import { BotdError } from '../types' import getDocumentElementKeys from './document_element_keys' describe('Sources', () => { describe('documentElementKeys', () => { it('returns expected values or throws', () => { - if (isChromium() && (getBrowserMajorVersion() ?? 0) < 61) { - expect(() => getDocumentElementKeys()).toThrow( - new BotdError(-2, 'document.documentElement.getAttributeNames is not a function'), - ) - return - } - const result = getDocumentElementKeys() // There are no document keys when running the tests using Karma expect(result).toEqual([]) diff --git a/src/sources/error_trace.test.ts b/src/sources/error_trace.test.ts index 4121ed21..f9cb6ab5 100644 --- a/src/sources/error_trace.test.ts +++ b/src/sources/error_trace.test.ts @@ -1,4 +1,4 @@ -import { getBrowserMajorVersion, isChromium } from '../../tests/utils' +import { getBrowserEngineMajorVersion, isChromium } from '../../tests/utils' import getErrorTrace from './error_trace' describe('Sources', () => { @@ -8,7 +8,7 @@ describe('Sources', () => { if (isChromium()) { expect(result).toContain( - (getBrowserMajorVersion() ?? 0) < 93 + (getBrowserEngineMajorVersion() ?? 0) < 93 ? "TypeError: Cannot read property '0' of null" : "TypeError: Cannot read properties of null (reading '0')", ) diff --git a/src/sources/rtt.test.ts b/src/sources/rtt.test.ts index ec38ae0f..735918cf 100644 --- a/src/sources/rtt.test.ts +++ b/src/sources/rtt.test.ts @@ -1,11 +1,11 @@ -import { getBrowserMajorVersion, isChromium, isGecko, isWebKit } from '../../tests/utils' +import { isGecko, isWebKit } from '../../tests/utils' import { BotdError } from '../types' import getRTT from './rtt' describe('Sources', () => { describe('rtt', () => { it('returns an expected value or throws', () => { - if (isGecko() || isWebKit() || (isChromium() && (getBrowserMajorVersion() ?? 0) < 61)) { + if (isGecko() || isWebKit()) { expect(() => getRTT()).toThrow(new BotdError(-1, 'navigator.connection is undefined')) return } diff --git a/src/sources/user_agent.test.ts b/src/sources/user_agent.test.ts index 58cd58d2..274c75e6 100644 --- a/src/sources/user_agent.test.ts +++ b/src/sources/user_agent.test.ts @@ -1,15 +1,12 @@ -import { getBrowserVersion } from '../../tests/utils' +import { getBrowserEngineVersion } from '../../tests/utils' import getUserAgent from './user_agent' describe('Sources', () => { describe('userAgent', () => { it('returns an expected value', () => { const value = getUserAgent() - const version = getBrowserVersion() ?? { major: 0, minor: 0 } - - // We do not want to call ua-parser-js in these tests, as those likely - // use navigator.userAgent internally. - expect(value).toMatch(new RegExp(`(Version|Chrome|Firefox)/${version.major}.${version.minor}`)) + const version = getBrowserEngineVersion() ?? { major: 0, minor: 0 } + expect(value).toMatch(new RegExp(`(Safari|Chrome|Firefox)/${version.major}.${version.minor}`)) }) }) }) diff --git a/src/sources/webdriver.test.ts b/src/sources/webdriver.test.ts index f0c9f800..1f16750c 100644 --- a/src/sources/webdriver.test.ts +++ b/src/sources/webdriver.test.ts @@ -1,15 +1,9 @@ -import { getBrowserMajorVersion, isChromium, isHeadlessChrome } from '../../tests/utils' +import { isHeadlessChrome } from '../../tests/utils' import getWebDriver from './webdriver' -import { BotdError } from '../types' describe('Sources', () => { describe('webdriver', () => { it('returns an expected value or throws', () => { - if (isChromium() && (getBrowserMajorVersion() ?? 0) < 63) { - expect(() => getWebDriver()).toThrow(new BotdError(-1, 'navigator.webdriver is undefined')) - return - } - const result = getWebDriver() if (isHeadlessChrome()) { diff --git a/tests/utils.ts b/tests/utils.ts index 46be83f9..24e35b9c 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -50,6 +50,14 @@ export function getBrowserMajorVersion(): number | undefined { return parseInt(version.split('.')[0]) } +export function getBrowserEngineMajorVersion(): number | undefined { + const version = new UAParser().getEngine().version + if (version === undefined) { + return undefined + } + return parseInt(version.split('.')[0]) +} + export function getBrowserVersion(): { major: number; minor: number } | undefined { const version = new UAParser().getBrowser().version if (version === undefined) { @@ -61,3 +69,15 @@ export function getBrowserVersion(): { major: number; minor: number } | undefine minor: parseInt(version.split('.')[1]), } } + +export function getBrowserEngineVersion(): { major: number; minor: number } | undefined { + const version = new UAParser().getEngine().version + if (version === undefined) { + return undefined + } + + return { + major: parseInt(version.split('.')[0]), + minor: parseInt(version.split('.')[1]), + } +}