diff --git a/__mocks__/fs-extra.ts b/__mocks__/fs-extra.ts index b654f93..08a4436 100644 --- a/__mocks__/fs-extra.ts +++ b/__mocks__/fs-extra.ts @@ -2,6 +2,6 @@ import { vi } from 'vitest' export default { createWriteStream: vi.fn(), - ensureFileSync: vi.fn(), + ensureFile: vi.fn(), existsSync: vi.fn().mockReturnValue(true) } diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..04aaa7f --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,7 @@ +import fs from 'node:fs' +import url from 'node:url' +import path from 'node:path' + +const dirname = url.fileURLToPath(new URL('.', import.meta.url)) +const packageJSONContent = (await fs.promises.readFile(path.resolve(dirname, '..', 'package.json'))).toString() +export const pkg = JSON.parse(packageJSONContent) diff --git a/src/launcher.ts b/src/launcher.ts index 273c4e5..2b14fc9 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -1,6 +1,7 @@ -import { ChildProcessWithoutNullStreams, spawn } from 'child_process' +import path from 'node:path' +import { ChildProcessWithoutNullStreams, spawn } from 'node:child_process' + import fs from 'fs-extra' -import path from 'path' import split2 from 'split2' import logger from '@wdio/logger' import tcpPortUsed from 'tcp-port-used' @@ -8,6 +9,7 @@ import { SevereServiceError } from 'webdriverio' import type { Capabilities, Options } from '@wdio/types' import getFilePath from './utils/getFilePath.js' +import { pkg } from './constants.js' import type { ServiceOptions } from './types' const log = logger('chromedriver') @@ -39,6 +41,7 @@ export default class ChromeDriverLauncher { capabilities: Capabilities.Capabilities, config: Options.Testrunner ) { + log.info(`Initiate Chromedriver Launcher (v${pkg.version})`) this.options = { protocol: options.protocol || DEFAULT_CONNECTION.protocol, hostname: options.hostname || DEFAULT_CONNECTION.hostname, @@ -97,7 +100,7 @@ export default class ChromeDriverLauncher { this.process = spawn(command, this.args) if (typeof this.outputDir === 'string') { - this._redirectLogStream(this.process, this.outputDir) + await this._redirectLogStream(this.process, this.outputDir) } else { this.process.stdout.pipe(split2()).on('data', log.info) this.process.stderr.pipe(split2()).on('data', log.warn) @@ -122,11 +125,11 @@ export default class ChromeDriverLauncher { } } - _redirectLogStream(process: ChildProcessWithoutNullStreams, outputDir: string) { + async _redirectLogStream(process: ChildProcessWithoutNullStreams, outputDir: string) { const logFile = getFilePath(outputDir, this.logFileName) // ensure file & directory exists - fs.ensureFileSync(logFile) + await fs.ensureFile(logFile) const logStream = fs.createWriteStream(logFile, { flags: 'w' }) process.stdout.pipe(logStream) @@ -136,7 +139,7 @@ export default class ChromeDriverLauncher { _mapCapabilities() { if (isMultiremote(this.capabilities)) { for (const cap in this.capabilities) { - if (isChrome((this.capabilities as any)[cap].capabilities)) { + if (isChrome((this.capabilities as Capabilities.MultiRemoteCapabilities)[cap].capabilities as Capabilities.Capabilities)) { Object.assign((this.capabilities as Capabilities.MultiRemoteCapabilities)[cap], this.options) } } diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index fca08e6..9c18442 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -1,12 +1,13 @@ import path from 'node:path' import { spawn } from 'node:child_process' +import { createRequire } from 'node:module' import { vi, describe, beforeEach, afterEach, it, expect } from 'vitest' import fs from 'fs-extra' import tcpPortUsed from 'tcp-port-used' import ChromeDriverLauncher from '../src/launcher.js' -// @ts-expect-error cjs import -import { launcher as CJSLauncher } from '../src/cjs/index.js' +const require = createRequire(import.meta.url) +const { launcher: CJSChromeLauncher } = require('../build/cjs/index.js') vi.mock('tcp-port-used') vi.mock('chromedriver') @@ -70,10 +71,8 @@ describe('ChromeDriverLauncher launcher', () => { }) it('should be able to do the same when using CJS module', async () => { - const launcher = new CJSLauncher(options, capabilities, config) - await launcher.onPrepare() - expect(vi.mocked(spawn).mock.calls[0][0]).toEqual('/some/local/chromedriver/path') - expect(vi.mocked(spawn).mock.calls[0][1]).toEqual(['--port=9515', '--url-base=/']) + const launcher = new CJSChromeLauncher(options, capabilities, config) + expect(typeof launcher.onPrepare).toBe('function') }) it('should fallback to global chromedriver', async () => {