Skip to content

Commit

Permalink
Add possibility to load config file from cjs file (#7614)
Browse files Browse the repository at this point in the history
[closes #6911]

## Purpose
The newest NodeJS version requires indicating modules loading framework
to use (CommonJS or ESM) by using extension file. `.js` extension is now
for using ESM and `.cjs` for CommonJS.
Currently, from newest NodeJS, it is not possible to use CommonJS
framework for TestCafe global configuration file (`.testcaferc.js`),
because this file has to be named `.testcaferc.cjs`

## Approach
This PR allows loading `.testcaferc.cjs` as TestCafe global
configuration file.

---------

Co-authored-by: Damien Guérin <damien.guerin@lyra-network.com>
  • Loading branch information
gigaga and Damien Guérin committed May 3, 2023
1 parent ff843d6 commit 2e6dee3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/configuration/configuration-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import renderTemplate from '../utils/render-template';
import WARNING_MESSAGES from '../notifications/warning-message';
import log from '../cli/log';
import { Dictionary } from './interfaces';
import { JS_CONFIGURATION_EXTENSION, JSON_CONFIGURATION_EXTENSION } from './formats';
import Extensions from './formats';

const DEBUG_LOGGER = debug('testcafe:configuration');

Expand Down Expand Up @@ -205,11 +205,11 @@ export default class Configuration {
}

protected _isJSConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, JS_CONFIGURATION_EXTENSION);
return Configuration._hasExtension(filePath, Extensions.js) || Configuration._hasExtension(filePath, Extensions.cjs);
}

protected _isJSONConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, JSON_CONFIGURATION_EXTENSION);
return Configuration._hasExtension(filePath, Extensions.json);
}

public _readJsConfigurationFileContent (filePath = this.filePath): object | null {
Expand Down
10 changes: 7 additions & 3 deletions src/configuration/formats.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const JS_CONFIGURATION_EXTENSION = '.js';
export const JSON_CONFIGURATION_EXTENSION = '.json';
export const CONFIGURATION_EXTENSIONS = [JS_CONFIGURATION_EXTENSION, JSON_CONFIGURATION_EXTENSION];
enum Extensions {
js = '.js',
json = '.json',
cjs = '.cjs',
}

export default Extensions;
5 changes: 2 additions & 3 deletions src/configuration/testcafe-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ import { DEPRECATED, getDeprecationMessage } from '../notifications/deprecated';
import WarningLog from '../notifications/warning-log';
import browserProviderPool from '../browser/provider/pool';
import BrowserConnection, { BrowserConnectionOptions, BrowserInfo } from '../browser/connection';
import { CONFIGURATION_EXTENSIONS } from './formats';
import Extensions from './formats';
import { GeneralError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';
import { LOCALHOST_NAMES } from '../utils/localhost-names';
import { BrowserConnectionGatewayOptions } from '../browser/connection/gateway';
import { getValidHostname } from './utils';

const BASE_CONFIGURATION_FILENAME = '.testcaferc';
const CONFIGURATION_FILENAMES = CONFIGURATION_EXTENSIONS.map(ext => `${BASE_CONFIGURATION_FILENAME}${ext}`);

const CONFIGURATION_FILENAMES = (Object.keys(Extensions) as Array<keyof typeof Extensions>).map(ext => `${BASE_CONFIGURATION_FILENAME}${Extensions[ext]}`);
const DEFAULT_SCREENSHOTS_DIRECTORY = 'screenshots';

const OPTION_FLAG_NAMES = [
Expand Down
11 changes: 3 additions & 8 deletions test/server/configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ const { DEFAULT_TYPESCRIPT_COMPILER_OPTIONS } = require('../../lib/configuration
const RunnerCtor = require('../../lib/runner');
const OptionNames = require('../../lib/configuration/option-names');
const consoleWrapper = require('./helpers/console-wrapper');

const {
CONFIGURATION_EXTENSIONS,
JS_CONFIGURATION_EXTENSION,
JSON_CONFIGURATION_EXTENSION,
} = require('../../lib/configuration/formats');
const Extensions = require('../../lib/configuration/formats');

const tsConfigPath = 'tsconfig.json';
const customTSConfigFilePath = 'custom-config.json';
Expand All @@ -40,8 +35,8 @@ const createJsConfig = (path, options) => {
fs.writeFileSync(path, `module.exports = ${JSON.stringify(options)}`);
};

const jsConfigIndex = CONFIGURATION_EXTENSIONS.indexOf(JS_CONFIGURATION_EXTENSION);
const jsonConfigIndex = CONFIGURATION_EXTENSIONS.indexOf(JSON_CONFIGURATION_EXTENSION);
const jsConfigIndex = TestCafeConfiguration.FILENAMES.findIndex(file=>file.includes(Extensions.js));
const jsonConfigIndex = TestCafeConfiguration.FILENAMES.findIndex(file=>file.includes(Extensions.json));

const createJsTestCafeConfigurationFile = createJsConfig.bind(null, TestCafeConfiguration.FILENAMES[jsConfigIndex]);
const createJSONTestCafeConfigurationFile = createJSONConfig.bind(null, TestCafeConfiguration.FILENAMES[jsonConfigIndex]);
Expand Down
16 changes: 9 additions & 7 deletions test/server/create-testcafe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ const { expect } = require('chai');
const url = require('url');
const net = require('net');
const path = require('path');
const proxyquire = require('proxyquire');
const createTestCafe = require('../../lib/');
const exportableLib = require('../../lib/api/exportable-lib');
const selfSignedCertificate = require('openssl-self-signed-certificate');
const Extensions = require('../../lib/configuration/formats');

const {
CONFIGURATION_EXTENSIONS,
JS_CONFIGURATION_EXTENSION,
JSON_CONFIGURATION_EXTENSION,
} = require('../../lib/configuration/formats');
const TestCafeConfiguration = proxyquire('../../lib/configuration/testcafe-configuration', {
'./utils': {
getValidHostname: hostname => hostname || 'calculated-hostname',
},
});

const jsConfigIndex = CONFIGURATION_EXTENSIONS.indexOf(JS_CONFIGURATION_EXTENSION);
const jsonConfigIndex = CONFIGURATION_EXTENSIONS.indexOf(JSON_CONFIGURATION_EXTENSION);
const jsConfigIndex = TestCafeConfiguration.FILENAMES.findIndex(file=>file.includes(Extensions.js));
const jsonConfigIndex = TestCafeConfiguration.FILENAMES.findIndex(file=>file.includes(Extensions.json));

describe('TestCafe factory function', function () {
let testCafe = null;
Expand Down

0 comments on commit 2e6dee3

Please sign in to comment.