Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 49 additions & 39 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,55 @@ const {
getBrowserOptions,
} = Utils

jest.spyOn(fs, 'exists')

beforeEach(() => {
jest.resetModules()
})

describe('readConfig', () => {
it('should return the default configuration if there was no separate configuration specified', async () => {
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
(_, cb: (exists: boolean) => void) => cb(false),
)
const config = await readConfig()
expect(config).toMatchObject(DEFAULT_CONFIG)
})
it('should overwrite with a custom configuration', async () => {
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
(_, cb: (exists: boolean) => void) => cb(true),
)
jest.mock(
path.join(__dirname, '..', 'jest-playwright.config.js'),
() => ({
launchOptions: {
headless: true,
},
browser: 'chromium',
contextOptions: {
ignoreHTTPSErrors: true,
},
}),
() => ({}),
{ virtual: true },
)
const config = await readConfig()
const expectedConfig = {
expect(config).toMatchObject(DEFAULT_CONFIG)
})
it('should overwrite with a custom configuration', async () => {
const configObject = {
launchOptions: {
headless: true,
},
browser: 'chromium',
contextOptions: {
ignoreHTTPSErrors: true,
},
browser: 'chromium',
exitOnPageError: true,
}
expect(config).toMatchObject(expectedConfig)
})
it('should overwrite with a custom configuration and spread the "launchOptions" and "contextOptions" setting', async () => {
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
(_, cb: (exists: boolean) => void) => cb(true),
)
jest.mock(
path.join(__dirname, '..', 'jest-playwright.config.js'),
() => ({
launchOptions: {
headless: true,
},
contextOptions: {
foo: true,
},
}),
() => configObject,
{ virtual: true },
)
const config = await readConfig()
const expectedConfig = {
expect(config).toMatchObject(configObject)
})
it('should overwrite with a custom configuration and spread the "launchOptions" and "contextOptions" setting', async () => {
const configObject = {
launchOptions: {
headless: true,
},
contextOptions: {
foo: true,
},
}
jest.mock(
path.join(__dirname, '..', 'jest-playwright.config.js'),
() => configObject,
{ virtual: true },
)
const config = await readConfig()
const expectedConfig = {
...configObject,
browsers: ['chromium'],
exitOnPageError: true,
}
Expand All @@ -100,6 +81,35 @@ describe('readConfig', () => {
expect(error).toBeTruthy()
delete process.env.JEST_PLAYWRIGHT_CONFIG
})
it('should check cjs config if npm_package_type is module', async () => {
process.env.npm_package_type = 'module'
const configPath = path.join(__dirname, '..', 'jest-playwright.config.cjs')
const configObject = {
browsers: ['webkit'],
launchOptions: {
headless: true,
},
contextOptions: {
foo: true,
},
}
fs.writeFileSync(configPath, '')
jest.mock(
path.join(__dirname, '..', 'jest-playwright.config.cjs'),
() => configObject,
{
virtual: true,
},
)
const expectedConfig = {
...configObject,
exitOnPageError: true,
}
const config = await readConfig()
expect(config).toMatchObject(expectedConfig)
delete process.env.npm_package_type
fs.unlinkSync(configPath)
})
})

describe('getDisplayName', () => {
Expand Down
17 changes: 13 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import type {
BrowserType,
DeviceType,
Expand All @@ -19,7 +18,7 @@ import {
PACKAGE_NAME,
} from './constants'

const exists = promisify(fs.exists)
const fsPromises = fs.promises

export const checkBrowserEnv = (param: BrowserType): void => {
if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
Expand Down Expand Up @@ -173,10 +172,20 @@ export const readConfig = async (
rootDir: string = process.cwd(),
): Promise<JestPlaywrightConfig> => {
const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG
let fileExtension = 'js'
if (process.env.npm_package_type === 'module') {
fileExtension = 'cjs'
}
const configPath =
process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js'
process.env.JEST_PLAYWRIGHT_CONFIG ||
`jest-playwright.config.${fileExtension}`
const absConfigPath = path.resolve(rootDir, configPath)
const configExists = await exists(absConfigPath)
let configExists = true
try {
await fsPromises.access(absConfigPath)
} catch (e) {
configExists = false
}

if (hasCustomConfigPath && !configExists) {
throw new Error(
Expand Down