Skip to content

Commit fddb9ee

Browse files
authored
Add ability to check cjs config (#215)
* Replace fs.exists with fs.promises.access * Simplify some utils tests * Add ability to check npm_package_type
1 parent f780a67 commit fddb9ee

File tree

2 files changed

+62
-43
lines changed

2 files changed

+62
-43
lines changed

src/utils.test.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,55 @@ const {
1616
getBrowserOptions,
1717
} = Utils
1818

19-
jest.spyOn(fs, 'exists')
20-
2119
beforeEach(() => {
2220
jest.resetModules()
2321
})
2422

2523
describe('readConfig', () => {
2624
it('should return the default configuration if there was no separate configuration specified', async () => {
27-
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
28-
(_, cb: (exists: boolean) => void) => cb(false),
29-
)
30-
const config = await readConfig()
31-
expect(config).toMatchObject(DEFAULT_CONFIG)
32-
})
33-
it('should overwrite with a custom configuration', async () => {
34-
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
35-
(_, cb: (exists: boolean) => void) => cb(true),
36-
)
3725
jest.mock(
3826
path.join(__dirname, '..', 'jest-playwright.config.js'),
39-
() => ({
40-
launchOptions: {
41-
headless: true,
42-
},
43-
browser: 'chromium',
44-
contextOptions: {
45-
ignoreHTTPSErrors: true,
46-
},
47-
}),
27+
() => ({}),
4828
{ virtual: true },
4929
)
5030
const config = await readConfig()
51-
const expectedConfig = {
31+
expect(config).toMatchObject(DEFAULT_CONFIG)
32+
})
33+
it('should overwrite with a custom configuration', async () => {
34+
const configObject = {
5235
launchOptions: {
5336
headless: true,
5437
},
38+
browser: 'chromium',
5539
contextOptions: {
5640
ignoreHTTPSErrors: true,
5741
},
58-
browser: 'chromium',
59-
exitOnPageError: true,
6042
}
61-
expect(config).toMatchObject(expectedConfig)
62-
})
63-
it('should overwrite with a custom configuration and spread the "launchOptions" and "contextOptions" setting', async () => {
64-
;((fs.exists as unknown) as jest.Mock).mockImplementationOnce(
65-
(_, cb: (exists: boolean) => void) => cb(true),
66-
)
6743
jest.mock(
6844
path.join(__dirname, '..', 'jest-playwright.config.js'),
69-
() => ({
70-
launchOptions: {
71-
headless: true,
72-
},
73-
contextOptions: {
74-
foo: true,
75-
},
76-
}),
45+
() => configObject,
7746
{ virtual: true },
7847
)
7948
const config = await readConfig()
80-
const expectedConfig = {
49+
expect(config).toMatchObject(configObject)
50+
})
51+
it('should overwrite with a custom configuration and spread the "launchOptions" and "contextOptions" setting', async () => {
52+
const configObject = {
8153
launchOptions: {
8254
headless: true,
8355
},
8456
contextOptions: {
8557
foo: true,
8658
},
59+
}
60+
jest.mock(
61+
path.join(__dirname, '..', 'jest-playwright.config.js'),
62+
() => configObject,
63+
{ virtual: true },
64+
)
65+
const config = await readConfig()
66+
const expectedConfig = {
67+
...configObject,
8768
browsers: ['chromium'],
8869
exitOnPageError: true,
8970
}
@@ -100,6 +81,35 @@ describe('readConfig', () => {
10081
expect(error).toBeTruthy()
10182
delete process.env.JEST_PLAYWRIGHT_CONFIG
10283
})
84+
it('should check cjs config if npm_package_type is module', async () => {
85+
process.env.npm_package_type = 'module'
86+
const configPath = path.join(__dirname, '..', 'jest-playwright.config.cjs')
87+
const configObject = {
88+
browsers: ['webkit'],
89+
launchOptions: {
90+
headless: true,
91+
},
92+
contextOptions: {
93+
foo: true,
94+
},
95+
}
96+
fs.writeFileSync(configPath, '')
97+
jest.mock(
98+
path.join(__dirname, '..', 'jest-playwright.config.cjs'),
99+
() => configObject,
100+
{
101+
virtual: true,
102+
},
103+
)
104+
const expectedConfig = {
105+
...configObject,
106+
exitOnPageError: true,
107+
}
108+
const config = await readConfig()
109+
expect(config).toMatchObject(expectedConfig)
110+
delete process.env.npm_package_type
111+
fs.unlinkSync(configPath)
112+
})
103113
})
104114

105115
describe('getDisplayName', () => {

src/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import { promisify } from 'util'
43
import type {
54
BrowserType,
65
DeviceType,
@@ -19,7 +18,7 @@ import {
1918
PACKAGE_NAME,
2019
} from './constants'
2120

22-
const exists = promisify(fs.exists)
21+
const fsPromises = fs.promises
2322

2423
export const checkBrowserEnv = (param: BrowserType): void => {
2524
if (param !== CHROMIUM && param !== FIREFOX && param !== WEBKIT) {
@@ -173,10 +172,20 @@ export const readConfig = async (
173172
rootDir: string = process.cwd(),
174173
): Promise<JestPlaywrightConfig> => {
175174
const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG
175+
let fileExtension = 'js'
176+
if (process.env.npm_package_type === 'module') {
177+
fileExtension = 'cjs'
178+
}
176179
const configPath =
177-
process.env.JEST_PLAYWRIGHT_CONFIG || 'jest-playwright.config.js'
180+
process.env.JEST_PLAYWRIGHT_CONFIG ||
181+
`jest-playwright.config.${fileExtension}`
178182
const absConfigPath = path.resolve(rootDir, configPath)
179-
const configExists = await exists(absConfigPath)
183+
let configExists = true
184+
try {
185+
await fsPromises.access(absConfigPath)
186+
} catch (e) {
187+
configExists = false
188+
}
180189

181190
if (hasCustomConfigPath && !configExists) {
182191
throw new Error(

0 commit comments

Comments
 (0)