rewrite firefox-profile service to esm
This commit is contained in:
parent
9d95f42f82
commit
2434b3eb3b
8 changed files with 77 additions and 61 deletions
20
__mocks__/firefox-profile.ts
Normal file
20
__mocks__/firefox-profile.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { vi } from 'vitest'
|
||||
|
||||
const FirefoxProfile = vi.fn(() => ({
|
||||
setPreference : vi.fn(),
|
||||
setProxy : vi.fn(),
|
||||
updatePreferences : vi.fn(),
|
||||
addExtensions : vi.fn((extensions, cb) => {
|
||||
cb()
|
||||
}),
|
||||
encoded : vi.fn((cb) => {
|
||||
cb(null, 'foobar')
|
||||
}),
|
||||
}))
|
||||
|
||||
// @ts-expect-error
|
||||
FirefoxProfile['copy'] = vi.fn((_, cb) => {
|
||||
cb(null, new FirefoxProfile())
|
||||
})
|
||||
|
||||
export default FirefoxProfile
|
||||
|
|
@ -5,7 +5,6 @@
|
|||
"author": "Christian Bromann <mail@bromann.dev>",
|
||||
"homepage": "https://github.com/webdriverio/webdriverio/tree/main/packages/wdio-firefox-profile-service",
|
||||
"license": "MIT",
|
||||
"main": "./build/index",
|
||||
"engines": {
|
||||
"node": "^16.13 || >=18"
|
||||
},
|
||||
|
|
@ -28,6 +27,20 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/webdriverio/webdriverio/issues"
|
||||
},
|
||||
"main": "./build/index.cjs.js",
|
||||
"type": "module",
|
||||
"module": "./build/index.js",
|
||||
"exports": {
|
||||
".": [
|
||||
{
|
||||
"import": "./build/index.js",
|
||||
"require": "./build/index.cjs.js"
|
||||
},
|
||||
"./build/index.cjs.js"
|
||||
]
|
||||
},
|
||||
"types": "./build/index.d.ts",
|
||||
"typeScriptVersion": "3.8.3",
|
||||
"dependencies": {
|
||||
"@wdio/types": "7.20.7",
|
||||
"firefox-profile": "^4.0.0"
|
||||
|
|
@ -37,6 +50,5 @@
|
|||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"types": "./build/index.d.ts"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
/* istanbul ignore file */
|
||||
|
||||
import FirefoxProfileLauncher from './launcher'
|
||||
import FirefoxProfileLauncher from './launcher.js'
|
||||
import type { FirefoxProfileOptions } from './types'
|
||||
|
||||
export const launcher = FirefoxProfileLauncher
|
||||
export * from './types'
|
||||
export * from './types.js'
|
||||
|
||||
declare global {
|
||||
namespace WebdriverIO {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import Profile from 'firefox-profile'
|
||||
import { promisify } from 'util'
|
||||
import { promisify } from 'node:util'
|
||||
import type { Capabilities } from '@wdio/types'
|
||||
|
||||
import { FirefoxProfileOptions } from './types'
|
||||
import type { FirefoxProfileOptions } from './types'
|
||||
|
||||
export default class FirefoxProfileLauncher {
|
||||
private _profile?: Profile
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"env": {
|
||||
"jest": true
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
const FirefoxProfile = jest.fn(() => ({
|
||||
setPreference : jest.fn(),
|
||||
setProxy : jest.fn(),
|
||||
updatePreferences : jest.fn(),
|
||||
addExtensions : jest.fn((extensions, cb) => {
|
||||
cb()
|
||||
}),
|
||||
encoded : jest.fn((cb) => {
|
||||
cb(null, 'foobar')
|
||||
}),
|
||||
}))
|
||||
|
||||
FirefoxProfile['copy'] = jest.fn((profileDirectory, cb) => {
|
||||
cb(null, new FirefoxProfile())
|
||||
})
|
||||
|
||||
export default FirefoxProfile
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
import Launcher from '../src/launcher'
|
||||
import FirefoxProfile from 'firefox-profile'
|
||||
import type WebDriver from 'webdriver'
|
||||
import type WebdriverIO from 'webdriverio'
|
||||
|
||||
import { describe, expect, test, vi } from 'vitest'
|
||||
|
||||
vi.mock('firefox-profile')
|
||||
|
||||
describe('Firefox profile service', () => {
|
||||
describe('onPrepare', () => {
|
||||
test('should return when no firefoxProfile set in the config', async () => {
|
||||
|
|
@ -10,7 +13,7 @@ describe('Firefox profile service', () => {
|
|||
const capabilities = [{}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(capabilities).toEqual([{}])
|
||||
})
|
||||
|
|
@ -24,12 +27,12 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile'].updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile'].addExtensions).not.toHaveBeenCalled()
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile']!.updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile']!.addExtensions).not.toHaveBeenCalled()
|
||||
|
||||
expect(capabilities[0].firefox_profile).toBe(undefined)
|
||||
expect(capabilities[0]['moz:firefoxOptions']).toEqual({ profile : 'foobar' })
|
||||
|
|
@ -45,12 +48,12 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile'].updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile'].addExtensions).not.toHaveBeenCalled()
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile']!.updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile']!.addExtensions).not.toHaveBeenCalled()
|
||||
|
||||
expect(capabilities[0].firefox_profile).toBe('foobar')
|
||||
expect(capabilities[0]['moz:firefoxOptions']).toEqual(undefined)
|
||||
|
|
@ -68,12 +71,12 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile'].updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile'].addExtensions).not.toHaveBeenCalled()
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile']!.updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile']!.addExtensions).not.toHaveBeenCalled()
|
||||
|
||||
expect(capabilities[0]['moz:firefoxOptions']).toEqual({ args: ['-headless'], profile : 'foobar' })
|
||||
})
|
||||
|
|
@ -83,10 +86,10 @@ describe('Firefox profile service', () => {
|
|||
const capabilities: WebDriver.DesiredCapabilities[] = [{ browserName : 'firefox' }]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(capabilities[0]['moz:firefoxOptions']).toEqual({ profile : 'foobar' })
|
||||
expect((service['_profile'].addExtensions as jest.Mock).mock.calls[0][0]).toBe(options.extensions)
|
||||
expect(vi.mocked(service['_profile']!.addExtensions).mock.calls[0][0]).toBe(options.extensions)
|
||||
})
|
||||
|
||||
test('should not set capabilities when not firefox browser', async () => {
|
||||
|
|
@ -100,7 +103,7 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(capabilities[0]['moz:firefoxOptions']).toEqual({ profile : 'foobar' })
|
||||
expect(capabilities[1]).not.toHaveProperty('firefox_profile')
|
||||
|
|
@ -111,6 +114,7 @@ describe('Firefox profile service', () => {
|
|||
const options = {
|
||||
'browser.startup.homepage': 'https://webdriver.io',
|
||||
}
|
||||
// @ts-expect-error
|
||||
const capabilities: WebdriverIO.MultiRemoteCapabilities = {
|
||||
firefox : {
|
||||
capabilities : {
|
||||
|
|
@ -120,7 +124,7 @@ describe('Firefox profile service', () => {
|
|||
}
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(capabilities.firefox.capabilities['moz:firefoxOptions']).toEqual({ profile : 'foobar' })
|
||||
})
|
||||
|
|
@ -129,6 +133,7 @@ describe('Firefox profile service', () => {
|
|||
const options = {
|
||||
'browser.startup.homepage': 'https://webdriver.io',
|
||||
}
|
||||
// @ts-expect-error
|
||||
const capabilities: WebdriverIO.MultiRemoteCapabilities = {
|
||||
foo : {
|
||||
capabilities: {
|
||||
|
|
@ -138,7 +143,7 @@ describe('Firefox profile service', () => {
|
|||
}
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(capabilities.foo.capabilities).not.toHaveProperty('firefox_profile')
|
||||
expect(capabilities.foo.capabilities).not.toHaveProperty('moz:firefoxOptions')
|
||||
|
|
@ -155,14 +160,14 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(service['_profile'].setProxy).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile'].setProxy).toHaveBeenCalledWith(options.proxy)
|
||||
expect(service['_profile']!.setProxy).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile']!.setProxy).toHaveBeenCalledWith(options.proxy)
|
||||
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile'].setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile'].updatePreferences).toHaveBeenCalled()
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledTimes(1)
|
||||
expect(service['_profile']!.setPreference).toHaveBeenCalledWith('browser.startup.homepage', 'https://webdriver.io')
|
||||
expect(service['_profile']!.updatePreferences).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('should load from directory if profileDirectory is set', async () => {
|
||||
|
|
@ -174,7 +179,7 @@ describe('Firefox profile service', () => {
|
|||
}]
|
||||
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
|
||||
expect(FirefoxProfile.copy).toHaveBeenCalledWith(options.profileDirectory, expect.any(Function))
|
||||
})
|
||||
|
|
@ -187,9 +192,9 @@ describe('Firefox profile service', () => {
|
|||
browserName : 'firefox',
|
||||
}]
|
||||
|
||||
;(FirefoxProfile.copy as any as jest.Mock).mockImplementationOnce((profileDirectory: string, cb: Function) => cb())
|
||||
vi.mocked(FirefoxProfile.copy).mockImplementationOnce((profileDirectory: string, cb: Function) => cb())
|
||||
const service = new Launcher(options)
|
||||
await service.onPrepare({}, capabilities)
|
||||
await service.onPrepare({} as never, capabilities)
|
||||
// no assertion needed as we return early and no failure due to
|
||||
// undefined profile is checked
|
||||
})
|
||||
|
|
|
|||
|
|
@ -15,7 +15,8 @@ export default defineConfig({
|
|||
'packages/wdio-concise-reporter/**/*.test.ts',
|
||||
'packages/wdio-crossbrowsertesting-service/**/*.test.ts',
|
||||
'packages/wdio-cucumber-framework/**/*.test.ts',
|
||||
'packages/wdio-dot-reporter/**/*.test.ts'
|
||||
'packages/wdio-dot-reporter/**/*.test.ts',
|
||||
'packages/wdio-firefox-profile-service/**/*.test.ts'
|
||||
],
|
||||
coverage: {
|
||||
enabled: false,
|
||||
|
|
|
|||
Loading…
Reference in a new issue