From ac214d10e84bf2731137bfcc2bc1b7c3e6c38132 Mon Sep 17 00:00:00 2001 From: Aliaksandr-Lapeta Date: Tue, 22 Oct 2024 16:01:13 +0200 Subject: [PATCH] Add cjs modules support --- package.json | 2 +- src/global.js | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0e52a58..b38fceb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jest-puppeteer-react", - "version": "11.0.0", + "version": "12.0.0", "description": "screenshot tests for your react components in chromium using puppeteer & jest", "main": "src/index.js", "types": "src/index.d.ts", diff --git a/src/global.js b/src/global.js index c629fa6..fdb9d84 100644 --- a/src/global.js +++ b/src/global.js @@ -15,7 +15,21 @@ const DIR = path.join(os.tmpdir(), 'jest_puppeteer_react_global_setup'); let webpackDevServer; -const getConfig = () => require(path.join(process.cwd(), 'jest-puppeteer-react.config.js')); +const getConfig = async () => { + const congifName = 'jest-puppeteer-react.config'; + const statPromisified = promisify(fs.stat); + + let configExt = '.cjs'; + + try { + await statPromisified(path.join(process.cwd(), `${congifName}${configExt}`)); + } catch (e) { + // Fallback extension if CommonJS module not exist + configExt = '.js'; + } + + return require(path.join(process.cwd(), `${congifName}${configExt}`)); +}; module.exports.setup = async function setup(jestConfig) { const { noInfo = true, rootDir, testPathPattern, debugOnly = false } = jestConfig; @@ -29,7 +43,7 @@ module.exports.setup = async function setup(jestConfig) { return testPathPatterRe.test(fs.realpathSync(file)); }); - const config = getConfig(); + const config = await getConfig(); const entryFiles = [ path.resolve(__dirname, 'webpack/globals.browser.js'),