|
1 |
| -import { CHROMIUM, FIREFOX, WEBKIT } from './constants'; |
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { promisify } from 'util'; |
| 4 | +import { CHROMIUM, FIREFOX, WEBKIT, DEFAULT_CONFIG } from './constants'; |
2 | 5 |
|
3 |
| -function checkBrowserEnv(param) { |
| 6 | +const exists = promisify(fs.exists); |
| 7 | + |
| 8 | +export function checkBrowserEnv(param) { |
4 | 9 | if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
|
5 | 10 | throw new Error(`Wrong browser type. Should be one of [${CHROMIUM}, ${FIREFOX}, ${WEBKIT}], but got ${param}`)
|
6 | 11 | }
|
7 | 12 | }
|
8 | 13 |
|
9 |
| -export default checkBrowserEnv |
| 14 | +export function getBrowserType(config) { |
| 15 | + const processBrowser = process.env.BROWSER; |
| 16 | + if (processBrowser) { |
| 17 | + return processBrowser |
| 18 | + } else { |
| 19 | + return config.browser || CHROMIUM |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export async function readConfig() { |
| 24 | + const defaultConfig = DEFAULT_CONFIG; |
| 25 | + |
| 26 | + const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG; |
| 27 | + const configPath = |
| 28 | + process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js'; |
| 29 | + const absConfigPath = path.resolve(process.cwd(), configPath) |
| 30 | + const configExists = await exists(absConfigPath); |
| 31 | + |
| 32 | + if (hasCustomConfigPath && !configExists) { |
| 33 | + throw new Error( |
| 34 | + `Error: Can't find a root directory while resolving a config file path.\nProvided path to resolve: ${configPath}`, |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + if (!hasCustomConfigPath && !configExists) { |
| 39 | + return defaultConfig |
| 40 | + } |
| 41 | + |
| 42 | + const localConfig = await require(absConfigPath); |
| 43 | + return Object.assign({}, defaultConfig, localConfig); |
| 44 | +} |
0 commit comments