Skip to content

Commit

Permalink
Add CJS priority for Puppeteer environment (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: Aliaksandr-Lapeta <[email protected]>
  • Loading branch information
avlapeto and Aliaksandr-Lapeta authored Oct 23, 2024
1 parent ac214d1 commit 7226fdb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-puppeteer-react",
"version": "12.0.0",
"version": "12.0.1",
"description": "screenshot tests for your react components in chromium using puppeteer & jest",
"main": "src/index.js",
"types": "src/index.d.ts",
Expand Down
16 changes: 15 additions & 1 deletion src/PuppeteerReactEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const path = require('path');
const { TestEnvironment } = require('jest-environment-puppeteer');
const { promisify } = require('util');
const fs = require('fs');

class PuppeteerReactEnvironment extends TestEnvironment {
async setup() {
await super.setup();

const rootPath = process.cwd(); // of your project under test

const config = require(path.join(rootPath, 'jest-puppeteer-react.config.js'));
const configName = 'jest-puppeteer-react.config';
const statPromisified = promisify(fs.stat);

let configExt = '.cjs';

try {
await statPromisified(path.join(process.cwd(), `${configName}${configExt}`));
} catch (e) {
// Fallback extension if CommonJS module not exist
configExt = '.js';
}

const config = require(path.join(rootPath, `${configName}${configExt}`));

if (!config.port) {
config.port = 1111;
Expand Down
6 changes: 3 additions & 3 deletions src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const DIR = path.join(os.tmpdir(), 'jest_puppeteer_react_global_setup');
let webpackDevServer;

const getConfig = async () => {
const congifName = 'jest-puppeteer-react.config';
const configName = 'jest-puppeteer-react.config';
const statPromisified = promisify(fs.stat);

let configExt = '.cjs';

try {
await statPromisified(path.join(process.cwd(), `${congifName}${configExt}`));
await statPromisified(path.join(process.cwd(), `${configName}${configExt}`));
} catch (e) {
// Fallback extension if CommonJS module not exist
configExt = '.js';
}

return require(path.join(process.cwd(), `${congifName}${configExt}`));
return require(path.join(process.cwd(), `${configName}${configExt}`));
};

module.exports.setup = async function setup(jestConfig) {
Expand Down

0 comments on commit 7226fdb

Please sign in to comment.