Skip to content

fix: issue while running typescript files#3316

Merged
beatfactor merged 25 commits intonightwatchjs:mainfrom
vaibhavsingh97:fix/typescript-run-issue
Oct 14, 2022
Merged

fix: issue while running typescript files#3316
beatfactor merged 25 commits intonightwatchjs:mainfrom
vaibhavsingh97:fix/typescript-run-issue

Conversation

@vaibhavsingh97
Copy link
Copy Markdown
Member

@vaibhavsingh97 vaibhavsingh97 commented Jul 30, 2022

Fixes: #3312
Thanks in advance for your contribution. Please follow the below steps in submitting a pull request, as it will help us with reviewing it quicker.

  • Create a new branch from master (e.g. features/my-new-feature or issue/123-my-bugfix);
  • If you're fixing a bug also create an issue if one doesn't exist yet;
  • If it's a new feature explain why do you think it's necessary. Please check with the maintainers beforehand to make sure it is something that we will accept. Usually we only accept new features if we feel that they will benefit the entire community;
  • Please avoid sending PRs which contain drastic or low level changes. If you are certain that the changes are needed, please discuss them beforehand and indicate what the impact will be;
  • If your change is based on existing functionality please consider refactoring first. Pull requests that duplicate code will most likely be ignored;
  • Do not include changes that are not related to the issue at hand;
  • Follow the same coding style with regards to spaces, semicolons, variable naming etc.;
  • Always add unit tests - PRs without tests are most of the times ignored.

@vaibhavsingh97 vaibhavsingh97 marked this pull request as draft July 30, 2022 08:43
@vaibhavsingh97 vaibhavsingh97 marked this pull request as ready for review July 30, 2022 09:59
@vaibhavsingh97 vaibhavsingh97 requested a review from a team July 30, 2022 10:00
Copy link
Copy Markdown
Member

@beatfactor beatfactor left a comment

Choose a reason for hiding this comment

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

Remove ts-node as a main dependency. See also #3312 (comment)

@beatfactor
Copy link
Copy Markdown
Member

@vaibhavsingh97 any updates here?

Comment thread bin/runner.js Outdated
Nightwatch.cli(function(argv) {
Nightwatch.cli(function (argv) {
const projectTsFilePath = findTSConfigFile();
if (projectTsFilePath !== '') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I actually missed this before, but we also need to check for the disable_typescript setting, which might mean that we need to move all this into the CLIRunner class after we parse the settings.

Comment thread lib/utils/index.js Outdated
const projectTsFileLocation2 = path.join(process.cwd(), 'tsconfig.nightwatch.json');
if (Utils.fileExistsSync(projectTsFileLocation1)) {
return projectTsFileLocation1;
} else if (Utils.fileExistsSync(projectTsFileLocation2)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

remove else

Comment thread lib/utils/index.js Outdated
project: projectTsFile
});
} catch (err) {
Logger.error(err);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

here we can check if ts-node has been installed in the current project and if not, display the appropriate error message.

Comment thread lib/utils/index.js
static findTSConfigFile() {
const projectTsFileLocation1 = path.join(process.cwd(), 'nightwatch', 'tsconfig.json');
const projectTsFileLocation2 = path.join(process.cwd(), 'tsconfig.nightwatch.json');
if (Utils.fileExistsSync(projectTsFileLocation1)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can actually add a new setting called tsconfig_path (empty by default) so users can set that to whatever they want, in which case they can re-use the existing tsconfig file if they want to. If this property is set, then it should take precedence.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vaibhavsingh97 any progress with adding the tsconfig_path setting?

Comment thread lib/utils/index.js Outdated
Comment on lines +537 to +547
static findTSConfigFile() {
const projectTsFileLocation1 = path.join(process.cwd(), 'nightwatch', 'tsconfig.json');
const projectTsFileLocation2 = path.join(process.cwd(), 'tsconfig.nightwatch.json');
if (Utils.fileExistsSync(projectTsFileLocation1)) {
return projectTsFileLocation1;
} else if (Utils.fileExistsSync(projectTsFileLocation2)) {
return projectTsFileLocation2;
}

return '';
}
Copy link
Copy Markdown
Member

@garg3133 garg3133 Aug 3, 2022

Choose a reason for hiding this comment

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

Suggested change
static findTSConfigFile() {
const projectTsFileLocation1 = path.join(process.cwd(), 'nightwatch', 'tsconfig.json');
const projectTsFileLocation2 = path.join(process.cwd(), 'tsconfig.nightwatch.json');
if (Utils.fileExistsSync(projectTsFileLocation1)) {
return projectTsFileLocation1;
} else if (Utils.fileExistsSync(projectTsFileLocation2)) {
return projectTsFileLocation2;
}
return '';
}
static findTSConfigFile() {
const allowedFileLocation = [
path.join('nightwatch', 'tsconfig.json'),
'tsconfig.nightwatch.json'
];
const filesPresent = allowedFileLocation
.map((fileLocation) => path.join(process.cwd(), fileLocation))
.filter((filePath) => Utils.fileExistsSync(filePath));
return filesPresent[0] || '';
}

Copy link
Copy Markdown
Member

@beatfactor beatfactor Aug 3, 2022

Choose a reason for hiding this comment

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

I am in favour of the existing implementation which is a bit simpler and easier to understand. Besides, .filter() will search the whole array even if the first file is found. We should use Array.some() instead.

Comment thread bin/runner.js Outdated
Comment on lines +15 to +18
const projectTsFilePath = findTSConfigFile();
if (projectTsFilePath !== '') {
loadTSNode(projectTsFilePath);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
const projectTsFilePath = findTSConfigFile();
if (projectTsFilePath !== '') {
loadTSNode(projectTsFilePath);
}
const isTypescriptProject = Utils.fileExistsSync(path.join(process.cwd(), 'tsconfig.json');
if (isTypescriptProject) {
const projectTsFilePath = findTSConfigFile();
if (projectTsFilePath !== '') {
loadTSNode(projectTsFilePath);
} else {
console.log('You can now run TS tests directly using Nightwatch.\nFollow this doc to learn more.');
}
}

^ This will also help in cases where a user has mistakenly deleted the nightwatch/tsconfig.json file and is now trying to run ts test file but is unable to do so (because we are not loading ts-node anymore).

Comment thread lib/runner/cli/cli.js
}

static get CONFIG_FILE_TS() {
return './nightwatch.conf.ts';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@vaibhavsingh97 we need a test that checks for this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added

Comment thread lib/runner/cli/cli.js Outdated
return path.resolve(CliRunner.CONFIG_FILE_CJS);
}
// TODO: check for both the `nightwatch/tsconfig.json` and `tsconfig.nightwatch.json`
if (Utils.fileExistsSync(path.resolve('tsconfig.nightwatch.json'))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should be refactored into a usingTS constant, similarly to the usingESM one above and I think we should also check if CliRunner.CONFIG_FILE_TS exists, because you can use a .js or .json config file even if you are using TS.

@garg3133 do we create the nightwatch.conf.ts from the init tool?

@vaibhavsingh97 vaibhavsingh97 marked this pull request as draft August 22, 2022 07:34
@vaibhavsingh97 vaibhavsingh97 marked this pull request as ready for review August 22, 2022 11:27
throw new Error('Does not exist');
}

if (module === './nightwatch.conf.ts'){
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the failures on windows are because of this.

Comment thread lib/runner/cli/cli.js Outdated
loadTypescriptTranspiler() {
const isTypescriptProject = Utils.fileExistsSync(path.join(process.cwd(), 'tsconfig.json'));
if (isTypescriptProject) {
Logger.info('Now you can run TS tests directly using Nightwatch.\nFollow this document to learn more.');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what document?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@vishalshah133 Need to work on it, I had removed the line for now

@beatfactor beatfactor merged commit 92ad22d into nightwatchjs:main Oct 14, 2022
@literallyMello
Copy link
Copy Markdown

What is the new way to extend/override built in types to add properties that would normally be added to the browser (for example custom_commands browser.myCustomCommand())
Before I could use a nightwatch.d.ts file to do that, but with these changes it tries to execute the nightwatch.d.ts file as a test unless I am doing something wrong. #3541

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

Unable to run typescript files directly Nightwatch

5 participants