From dde638873a6ea937adfeef631a27d2c2835f241c Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Tue, 8 Aug 2023 16:49:40 +0530 Subject: [PATCH] Migrate utils modules to TS. --- ...ddDetailedError.js => addDetailedError.ts} | 19 ++++++++++--------- ...sDisplayError.js => alwaysDisplayError.ts} | 4 ++-- lib/utils/{browsername.js => browsername.ts} | 4 +++- .../{createPromise.js => createPromise.ts} | 6 ++++-- lib/utils/{getFreePort.js => getFreePort.ts} | 19 +++++++++++++------ .../{isErrorObject.js => isErrorObject.ts} | 4 ++-- lib/utils/nightwatchInterfaces.ts | 12 ++++++++++++ 7 files changed, 46 insertions(+), 22 deletions(-) rename lib/utils/{addDetailedError.js => addDetailedError.ts} (84%) rename lib/utils/{alwaysDisplayError.js => alwaysDisplayError.ts} (77%) rename lib/utils/{browsername.js => browsername.ts} (87%) rename lib/utils/{createPromise.js => createPromise.ts} (67%) rename lib/utils/{getFreePort.js => getFreePort.ts} (58%) rename lib/utils/{isErrorObject.js => isErrorObject.ts} (72%) create mode 100644 lib/utils/nightwatchInterfaces.ts diff --git a/lib/utils/addDetailedError.js b/lib/utils/addDetailedError.ts similarity index 84% rename from lib/utils/addDetailedError.js rename to lib/utils/addDetailedError.ts index d31983ccac..097702108b 100644 --- a/lib/utils/addDetailedError.js +++ b/lib/utils/addDetailedError.ts @@ -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)) { @@ -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 = ''; } @@ -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) diff --git a/lib/utils/alwaysDisplayError.js b/lib/utils/alwaysDisplayError.ts similarity index 77% rename from lib/utils/alwaysDisplayError.js rename to lib/utils/alwaysDisplayError.ts index 5d14565524..3e5ea2ef19 100644 --- a/lib/utils/alwaysDisplayError.js +++ b/lib/utils/alwaysDisplayError.ts @@ -1,5 +1,5 @@ -module.exports = function(err) { +export = function(err: any) { return (err instanceof Error) && [ 'TypeError', 'SyntaxError', 'ReferenceError', 'RangeError' ].includes(err.name); -}; \ No newline at end of file +}; diff --git a/lib/utils/browsername.js b/lib/utils/browsername.ts similarity index 87% rename from lib/utils/browsername.js rename to lib/utils/browsername.ts index eda7ab1537..44358c9fc4 100644 --- a/lib/utils/browsername.js +++ b/lib/utils/browsername.ts @@ -1,4 +1,4 @@ -const BrowserName = module.exports = { +const BrowserName = { get CHROME() { return 'chrome'; }, @@ -25,3 +25,5 @@ const BrowserName = module.exports = { }; Object.freeze(BrowserName); + +export = BrowserName; diff --git a/lib/utils/createPromise.js b/lib/utils/createPromise.ts similarity index 67% rename from lib/utils/createPromise.js rename to lib/utils/createPromise.ts index 3ec3afaf8c..fb8db2a3e1 100644 --- a/lib/utils/createPromise.js +++ b/lib/utils/createPromise.ts @@ -1,8 +1,10 @@ +import { Deferred } from "./nightwatchInterfaces"; + /** * @return {{resolve, reject, promise}} */ -module.exports = function createPromise() { - const deferred = { +export = function createPromise() { + const deferred: Deferred = { resolve: null, reject: null, promise: null diff --git a/lib/utils/getFreePort.js b/lib/utils/getFreePort.ts similarity index 58% rename from lib/utils/getFreePort.js rename to lib/utils/getFreePort.ts index deb3878eff..a7ca54cbcc 100644 --- a/lib/utils/getFreePort.js +++ b/lib/utils/getFreePort.ts @@ -1,20 +1,27 @@ +import * as net from 'net'; + /** * @method getFreePort * @param host * @returns {Promise} */ -module.exports = function(host = 'localhost') { - const net = require('net'); - +export = function(host = 'localhost'): Promise { 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'); @@ -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); }); -}; \ No newline at end of file +}; diff --git a/lib/utils/isErrorObject.js b/lib/utils/isErrorObject.ts similarity index 72% rename from lib/utils/isErrorObject.js rename to lib/utils/isErrorObject.ts index bb2955ff71..e237f8e56e 100644 --- a/lib/utils/isErrorObject.js +++ b/lib/utils/isErrorObject.ts @@ -1,3 +1,3 @@ -module.exports = function(err) { +export = function(err: any) { return err instanceof Error || Object.prototype.toString.call(err) === '[object Error]'; -}; \ No newline at end of file +}; diff --git a/lib/utils/nightwatchInterfaces.ts b/lib/utils/nightwatchInterfaces.ts new file mode 100644 index 0000000000..6af4b75bd8 --- /dev/null +++ b/lib/utils/nightwatchInterfaces.ts @@ -0,0 +1,12 @@ + +export interface NightwatchError extends Error { + detailedErr: string; + link: string; + help: string[]; +} + +export interface Deferred { + promise: Promise | null; + resolve: ((value: T | PromiseLike) => void) | null; + reject: ((reason?: any) => void) | null; +}