Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpile ts to js to handle ts types #4133

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/runner/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,29 @@ class CliRunner {
return path.resolve(CliRunner.CONFIG_FILE_JS);
}

transpileTStoJS(tsFile, filename) {
const ts = require('typescript');
const fs = require('fs');
const tsFileContent = fs.readFileSync(tsFile, 'utf8');

// Define TypeScript compiler options
const compilerOptions = {
target: ts.ScriptTarget.ES5,
module: ts.ModuleKind.CommonJS
};
const transpiledCode = ts.transpileModule(tsFileContent, {compilerOptions});
const jsCode = transpiledCode.outputText;
var Module = module.constructor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, we can also put a const Module = require('module'); statement at the top of the function definition (where other requires are done).

var m = new Module();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use const instead of var for declaring variables. Also, we should define proper variable names, module would be a better name instead of m here.

m._compile(jsCode, filename);

return m.exports;
}

loadConfig() {
if (!this.argv.config) {
return null;
}

const localJsOrTsValue = this.getLocalConfigFileName();

// use default nightwatch.json file if we haven't received another value
Expand All @@ -354,7 +372,11 @@ class CliRunner {
newConfigCreated = CliRunner.createDefaultConfig(localJsOrTsValue);
}

if (hasJsOrTsConfig || newConfigCreated) {
if (Utils.fileExistsSync(CliRunner.CONFIG_FILE_TS)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should ideally use localJsOrTsValue here as our source of truth to decide if the ts config file is our primary config for the project. This is because the logic used to find localJsOrTsValue may change later and we may give preference to some other config file even if a nightwatch.conf.ts file is present.

This could be solved by changing this statement to:
localJsOrTsValue.includes(path.basename(CliRunner.CONFIG_FILE_TS))

And for the issue you were facing with tests, that could be solved by changing the basename definition in mockery to:

    basename(a) {
        if (a === './globals.json') {
          return 'globals';
        }

        return origPath.basename(a);
      },

this.argv.config = localJsOrTsValue;

return this.transpileTStoJS(localJsOrTsValue, '');
} else if (hasJsOrTsConfig || newConfigCreated) {
this.argv.config = localJsOrTsValue;
} else if (hasJsonConfig) {
this.argv.config = path.join(path.resolve('./'), this.argv.config);
Expand Down
41 changes: 30 additions & 11 deletions test/src/cli/testCliRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ describe('Test CLI Runner', function() {

done();
});


it('testReadSettingsDeprecated', function(done) {
mockery.registerMock('fs', {
Expand Down Expand Up @@ -497,8 +496,6 @@ describe('Test CLI Runner', function() {
done();
});



it('testCustomSettingsFileAndEnvironment', function() {
mockery.registerMock('fs', {
existsSync() {
Expand Down Expand Up @@ -537,7 +534,7 @@ describe('Test CLI Runner', function() {
it('testGetTestSourceSingle', function() {
let statCalled = false;
let statSyncCalled = false;

mockery.registerMock('fs', {
existsSync() {
return false;
Expand All @@ -559,7 +556,6 @@ describe('Test CLI Runner', function() {

throw new Error('Does not exist');
},


stat(file, cb) {
if (file === 'demoTest') {
Expand Down Expand Up @@ -1054,11 +1050,34 @@ describe('Test CLI Runner', function() {
}
};
}
},
readFileSync: (file, encoding) => {
return 'TypeScript file content'; // Mocking the readFileSync function
},
constants,
rmdirSync
});

mockery.registerMock('typescript', {
ScriptTarget: {
ES5: 'ES5' // Mocking only the properties used in the transpileTStoJS function
},
ModuleKind: {
CommonJS: 'CommonJS' // Mocking only the properties used in the transpileTStoJS function
},
transpileModule: (code, options) => {
return {outputText: ''}; // Mocking the transpileModule function
}
});

mockery.registerMock('module', {
constructor: function Module() {
this._compile = (code, filename) => {
// Mocking the _compile function
};
}
});

const CliRunner = common.require('runner/cli/cli.js');
const runner = new CliRunner({
config: './nightwatch.json'
Expand Down Expand Up @@ -1412,13 +1431,13 @@ describe('Test CLI Runner', function() {
const listFileOutput = JSON.stringify({
default: testsPath
});

const origConsoleLog = console.log;

console.log = function (data) {
consoleData.push(data);
};

mockery.registerMock('./runner/cli/argv-setup.js', {
argv: {
_: testsPath,
Expand All @@ -1437,13 +1456,13 @@ describe('Test CLI Runner', function() {
const listFileOutput = JSON.stringify({
chrome: testsPath
});

const origConsoleLog = console.log;

console.log = function (data) {
consoleData.push(data);
};

mockery.registerMock('./runner/cli/argv-setup.js', {
argv: {
_: testsPath,
Expand Down