From c58e67ee5a2cc2657fc9607ee4393863f276c124 Mon Sep 17 00:00:00 2001 From: mmarkelov Date: Sun, 12 Jul 2020 14:43:50 +0300 Subject: [PATCH 1/3] Replace fs.exists with fs.promises.access --- src/utils.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 8949d75f..3a6f3372 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import fs from 'fs' import path from 'path' -import { promisify } from 'util' import type { BrowserType, DeviceType, @@ -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) { @@ -176,7 +175,12 @@ export const readConfig = async ( const configPath = process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js' 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( From ed68c276456ea9b01dc2a6922b1f2e8b66ee73e9 Mon Sep 17 00:00:00 2001 From: mmarkelov Date: Sun, 12 Jul 2020 14:44:17 +0300 Subject: [PATCH 2/3] Simplify some utils tests --- src/utils.test.ts | 57 +++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index 615349a1..f17b3c2d 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -24,66 +24,49 @@ beforeEach(() => { 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, } From e1bc723743dd655368e478874fd0c91e0feb75d1 Mon Sep 17 00:00:00 2001 From: mmarkelov Date: Sun, 12 Jul 2020 15:46:16 +0300 Subject: [PATCH 3/3] Add ability to check npm_package_type --- src/utils.test.ts | 31 +++++++++++++++++++++++++++++-- src/utils.ts | 7 ++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index f17b3c2d..80e1603b 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -16,8 +16,6 @@ const { getBrowserOptions, } = Utils -jest.spyOn(fs, 'exists') - beforeEach(() => { jest.resetModules() }) @@ -83,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', () => { diff --git a/src/utils.ts b/src/utils.ts index 3a6f3372..5604647c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -172,8 +172,13 @@ export const readConfig = async ( rootDir: string = process.cwd(), ): Promise => { 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) let configExists = true try {