Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Sep 30, 2022
1 parent e09269f commit f1f358c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion __mocks__/fs-extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { vi } from 'vitest'

export default {
createWriteStream: vi.fn(),
ensureFileSync: vi.fn(),
ensureFile: vi.fn(),
existsSync: vi.fn().mockReturnValue(true)
}
7 changes: 7 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 9 additions & 6 deletions src/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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'
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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand Down
11 changes: 5 additions & 6 deletions tests/launcher.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit f1f358c

Please sign in to comment.