Skip to content

Commit

Permalink
add: addded tests for cli compiler options and config path
Browse files Browse the repository at this point in the history
  • Loading branch information
Bayheck committed Oct 30, 2023
1 parent 3a7528b commit 0fa9eab
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 278 deletions.
9 changes: 0 additions & 9 deletions src/cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-debugger */
import chalk from 'chalk';
import { GeneralError, APIError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';
Expand Down Expand Up @@ -89,10 +88,6 @@ async function runTests (argParser) {
compilerOptions,
} = opts;

console.log('Before create');
console.log(opts);
debugger;

const testCafe = await createTestCafe({
developmentMode: dev,
isCli: true,
Expand Down Expand Up @@ -185,9 +180,6 @@ async function listBrowsers (providerName) {
await argParser.parse(process.argv);

logEntry(LOGGER, argParser.opts);
console.log('In CLI');
console.log(argParser);
debugger;

if (argParser.opts.listBrowsers)
await listBrowsers(argParser.opts.providerName);
Expand All @@ -199,4 +191,3 @@ async function listBrowsers (providerName) {
error(err);
}
})();

2 changes: 1 addition & 1 deletion src/compiler/compilers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hammerhead from 'testcafe-hammerhead';
import { Compiler as LegacyTestFileCompiler } from 'testcafe-legacy-api';
import EsNextTestFileCompiler from './test-file/formats/es-next/compiler';
import TypeScriptTestFileCompiler from './test-file/formats/typescript/test-file-compiler';
import TypeScriptTestFileCompiler from './test-file/formats/typescript/compiler';
import CoffeeScriptTestFileCompiler from './test-file/formats/coffeescript/compiler';
import RawTestFileCompiler from './test-file/formats/raw/compiler';
import DevToolsTestFileCompiler from './test-file/formats/dev-tools/compiler';
Expand Down
22 changes: 11 additions & 11 deletions src/compiler/test-file/api-based.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
return this._execAsModule(compiledCode, filename);
}

precompile (testFilesInfo) {
return this._compileCodeForTestFiles(testFilesInfo);
}

execute (compiledCode, filename) {
return this._runCompiledTestCode(compiledCode, filename);
}

async _runCompiledTestCode (compiledCode, filename) {
const testFile = new TestFile(filename);
Expand All @@ -231,15 +224,22 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
}
finally {
this._removeRequireHook();
stackCleaningHook.enabled = false;

if (!this.esm)
this._removeGlobalAPI();
}

stackCleaningHook.enabled = false;
return testFile.getTests();
}

if (!this.esm)
this._removeGlobalAPI();

precompile (testFilesInfo) {
return this._compileCodeForTestFiles(testFilesInfo);
}

return testFile.getTests();
execute (compiledCode, filename) {
return this._runCompiledTestCode(compiledCode, filename);
}

async compile (code, filename) {
Expand Down
18 changes: 6 additions & 12 deletions src/compiler/test-file/formats/typescript/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-console */
/* eslint-disable no-debugger */
import path from 'path';
import { zipObject } from 'lodash';
import OS from 'os-family';
import APIBasedTestFileCompilerBase from '../../api-based';
import ESNextTestFileCompiler from '../es-next/compiler';
import { TypescriptConfigurationBase } from '../../../../configuration/typescript-configuration';
import TypescriptConfiguration from '../../../../configuration/typescript-configuration';
import { GeneralError } from '../../../../errors/runtime';
import { RUNTIME_ERRORS } from '../../../../errors/types';
import debug from 'debug';
Expand Down Expand Up @@ -91,12 +88,12 @@ const RENAMED_DEPENDENCIES_MAP = new Map([['testcafe', getExportableLibPath()]])

const DEFAULT_TYPESCRIPT_COMPILER_PATH = 'typescript';

export default abstract class TypeScriptTestFileCompilerBase extends APIBasedTestFileCompilerBase {
private static tsDefsPath = TypeScriptTestFileCompilerBase._getTSDefsPath();
export default class TypeScriptTestFileCompiler extends APIBasedTestFileCompilerBase {
private static tsDefsPath = TypeScriptTestFileCompiler._getTSDefsPath();

private readonly _tsConfig: TypescriptConfigurationBase;
private readonly _tsConfig: TypescriptConfiguration;
private readonly _compilerPath: string;
private _customCompilerOptions?: Dictionary<boolean | number>;;
private readonly _customCompilerOptions?: object;

public constructor (compilerOptions?: TypeScriptCompilerOptions, { baseUrl, esm }: OptionalCompilerArguments = {}) {
super({ baseUrl, esm });
Expand All @@ -109,8 +106,6 @@ export default abstract class TypeScriptTestFileCompilerBase extends APIBasedTes

const configPath = compilerOptions && compilerOptions.configPath || null;

console.log('Configuration compiler options:');
debugger;
this._customCompilerOptions = compilerOptions && compilerOptions.options;
this._tsConfig = new TypescriptConfiguration(configPath, esm);
this._compilerPath = TypeScriptTestFileCompiler._getCompilerPath(compilerOptions);
Expand Down Expand Up @@ -177,7 +172,6 @@ export default abstract class TypeScriptTestFileCompilerBase extends APIBasedTes
}

public _compileCodeForTestFiles (testFilesInfo: TestFileInfo[]): Promise<string[]> {
debugger;
return this._tsConfig.init(this._customCompilerOptions)
.then(() => {
return super._compileCodeForTestFiles(testFilesInfo);
Expand All @@ -191,7 +185,6 @@ export default abstract class TypeScriptTestFileCompilerBase extends APIBasedTes

const program = ts.createProgram([TypeScriptTestFileCompiler.tsDefsPath, ...filenames], opts);

debugger;
DEBUG_LOGGER('version: %s', ts.version);
DEBUG_LOGGER('options: %O', opts);

Expand Down Expand Up @@ -284,6 +277,7 @@ export default abstract class TypeScriptTestFileCompilerBase extends APIBasedTes
this._setupRequireHook({ });

compiledConfigurationModule = await this._runCompiledCode({}, compiledCode, filename);

this._removeRequireHook();

return compiledConfigurationModule.exports;
Expand Down
116 changes: 0 additions & 116 deletions src/compiler/test-file/formats/typescript/config-compiler.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/compiler/test-file/formats/typescript/test-file-compiler.ts

This file was deleted.

55 changes: 17 additions & 38 deletions src/configuration/configuration-base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/* eslint-disable no-console */
/* eslint-disable no-debugger */
import {
extname, isAbsolute,
} from 'path';
import { extname, isAbsolute } from 'path';
import debug from 'debug';
import JSON5 from 'json5';

Expand All @@ -24,7 +20,6 @@ import { Dictionary } from './interfaces';
import Extensions from './formats';
import { ReadConfigFileError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';
import TypeScriptConfigurationCompiler from '../compiler/test-file/formats/typescript/config-compiler';

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

Expand Down Expand Up @@ -159,26 +154,15 @@ export default class Configuration {
return this._defaultPaths;
}

public async _load (loadInfo: any): Promise<null | object> {
public async _load (customCompilerOptions?: object): Promise<null | object> {
if (!this.defaultPaths?.length)
return null;

const configs = await Promise.all(this.defaultPaths.map(async filePath => {
if (!await this._isConfigurationFileExists(filePath))
return { filePath, options: null };

let options = null as object | null;

if (this._isJSConfiguration(filePath))
options = this._readJsConfigurationFileContent(filePath);
else if (this._isTSConfiguration(filePath))
options = await this._readTsConfigurationFileContent(filePath, loadInfo?.compilerOptions);
else {
const configurationFileContent = await this._readConfigurationFileContent(filePath);

if (configurationFileContent)
options = this._parseConfigurationFileContent(configurationFileContent, filePath);
}
const options = await this._readConfigurationOptions(filePath, customCompilerOptions);

return { filePath, options };
}));
Expand All @@ -196,6 +180,19 @@ export default class Configuration {
return existedConfigs[0].options;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async _readConfigurationOptions (filePath: string, customCompilerOptions?: object): Promise<object | null> {
if (this._isJSConfiguration(filePath))
return this._readJsConfigurationFileContent(filePath);

const configurationFileContent = await this._readConfigurationFileContent(filePath);

if (configurationFileContent)
return this._parseConfigurationFileContent(configurationFileContent, filePath);

return null;
}

protected async _isConfigurationFileExists (filePath = this.filePath): Promise<boolean> {
try {
await stat(filePath);
Expand All @@ -209,18 +206,14 @@ export default class Configuration {
}
}

private static _hasExtension (filePath: string | undefined, extention: string): boolean {
protected static _hasExtension (filePath: string | undefined, extention: string): boolean {
return !!filePath && extname(filePath) === extention;
}

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

protected _isTSConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, Extensions.ts) || Configuration._hasExtension(filePath, Extensions.cjs);
}

protected _isJSONConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, Extensions.json);
}
Expand All @@ -240,20 +233,6 @@ export default class Configuration {
return null;
}

public async _readTsConfigurationFileContent (filePath = this.filePath, loadOptions: any): Promise<object | null> {
if (filePath) {
delete require.cache[filePath];
debugger;
const compiler = new TypeScriptConfigurationCompiler(loadOptions?.typescript);
const options = await compiler.compileConfiguration(filePath);

debugger;
return options;
}

return null;
}

public async _readConfigurationFileContent (filePath = this.filePath): Promise<Buffer | null> {
try {
return await readFile(filePath);
Expand Down
Loading

0 comments on commit 0fa9eab

Please sign in to comment.