Skip to content

Commit

Permalink
Migrate utils modules to TS.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Aug 8, 2023
1 parent 05ee135 commit dde6388
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 22 deletions.
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(':')) {
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'))) {
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) {
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 = {
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>}
*/
module.exports = function(host = 'localhost') {
const net = require('net');

export = function(host = 'localhost'): Promise<number> {
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) {
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 @@

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;
}

0 comments on commit dde6388

Please sign in to comment.