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

Migrate utils modules to TS. #3869

Merged
merged 13 commits into from
Aug 22, 2023
19 changes: 10 additions & 9 deletions lib/utils/addDetailedError.js → lib/utils/addDetailedError.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NightwatchError } from "./nightwatchInterfaces";

/**
* @method addDetailedError
* @param {Error} err
*/
module.exports = function(err) {
let detailedErr;
export = function(err: NightwatchError) {
let detailedErr: string | undefined;

if (err instanceof TypeError) {
if (err.detailedErr && /browser\..+ is not a function$/.test(err.detailedErr)) {
Expand All @@ -25,14 +26,14 @@ module.exports = function(err) {
detailedErr = ' - writing an ES6 async test case? - keep in mind that commands return a Promise; \n - writing unit tests? - make sure to specify "unit_tests_mode=true" in your config.';
}
} else if (err instanceof SyntaxError) {
const stackParts = err.stack.split('SyntaxError:');
detailedErr = stackParts[0];
let modulePath = err.stack.split('\n')[0];
if (modulePath.includes(':')) {
const stackParts = err.stack?.split('SyntaxError:');
detailedErr = stackParts?.[0];
let modulePath = err.stack?.split('\n')[0];
if (modulePath && modulePath.includes(':')) {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
modulePath = modulePath.split(':')[0];
}

if (stackParts[1]) {
if (stackParts?.[1]) {
if (detailedErr) {
err.stack = '';
}
Expand All @@ -46,7 +47,7 @@ module.exports = function(err) {
detailedErr = header + detailedErr;
}

if (modulePath.endsWith('.jsx') || modulePath.endsWith('.tsx')) {
if (modulePath && (modulePath.endsWith('.jsx') || modulePath.endsWith('.tsx'))) {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
detailedErr = `\n In order to be able to load JSX files, one of these plugins is needed:
- @nightwatch/react
- @nightwatch/storybook (only if using Storybook in your project)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(err) {
export = function(err: any) {
vaibhavsingh97 marked this conversation as resolved.
Show resolved Hide resolved
return (err instanceof Error) && [
'TypeError', 'SyntaxError', 'ReferenceError', 'RangeError'
].includes(err.name);
};
};
4 changes: 3 additions & 1 deletion lib/utils/browsername.js → lib/utils/browsername.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BrowserName = module.exports = {
const BrowserName = {
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
get CHROME() {
return 'chrome';
},
Expand All @@ -25,3 +25,5 @@ const BrowserName = module.exports = {
};

Object.freeze(BrowserName);

export = BrowserName;
6 changes: 4 additions & 2 deletions lib/utils/createPromise.js → lib/utils/createPromise.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Deferred } from "./nightwatchInterfaces";

/**
* @return {{resolve, reject, promise}}
*/
module.exports = function createPromise() {
const deferred = {
export = function createPromise<T>() {
const deferred: Deferred<T> = {
resolve: null,
reject: null,
promise: null
Expand Down
19 changes: 13 additions & 6 deletions lib/utils/getFreePort.js → lib/utils/getFreePort.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import * as net from 'net';

/**
* @method getFreePort
* @param host
* @returns {Promise<number>}
*/
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
module.exports = function(host = 'localhost') {
const net = require('net');

export = function(host = 'localhost'): Promise<number> {
vaibhavsingh97 marked this conversation as resolved.
Show resolved Hide resolved
return new Promise((resolve, reject) => {
const server = net.createServer();

server.on('listening', function () {
resolve(server.address().port);
const serverAddress = server.address();

if (!serverAddress || typeof serverAddress === 'string') {
reject(new Error('Unable to get port from server address.'));
} else {
resolve(serverAddress.port);
}

server.close();
});

server.on('error', (e) => {
server.on('error', (e: NodeJS.ErrnoException) => {
let err;
if (e.code === 'EADDRINUSE' || e.code === 'EACCES') {
err = new Error('Unable to find a free port');
Expand All @@ -28,4 +35,4 @@ module.exports = function(host = 'localhost') {
// By providing 0 we let the operative system find an arbitrary port
server.listen(0, host);
});
};
};
4 changes: 2 additions & 2 deletions lib/utils/isErrorObject.js → lib/utils/isErrorObject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function(err) {
export = function(err: any) {
vaibhavsingh97 marked this conversation as resolved.
Show resolved Hide resolved
return err instanceof Error || Object.prototype.toString.call(err) === '[object Error]';
};
};
12 changes: 12 additions & 0 deletions lib/utils/nightwatchInterfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

garg3133 marked this conversation as resolved.
Show resolved Hide resolved
export interface NightwatchError extends Error {
detailedErr: string;
link: string;
help: string[];
}

export interface Deferred<T> {
promise: Promise<T> | null;
resolve: ((value: T | PromiseLike<T>) => void) | null;
reject: ((reason?: any) => void) | null;
}
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"devDependencies": {
"@cucumber/cucumber": "^8.2.1",
"@swc/core": "^1.3.67",
"@types/node": "^18.11.7",
"@types/node": "^18.17.3",
"copyfiles": "^2.4.1",
"eslint": "^8.9.0",
"husky": "^8.0.0",
Expand Down