Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Linter no floating promise (#296)
Browse files Browse the repository at this point in the history
* fix: add no-floating-promise linter rule
  • Loading branch information
FelixZilber authored Apr 3, 2022
1 parent a9e1688 commit 5e26fa2
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 96 deletions.
37 changes: 37 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"root": true,
"env": {
"node": true,
"mocha": true
},
"parser": "@typescript-eslint/parser",

"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module",
"project": ["./tsconfig.json"] // Specify it only for TypeScript files

},
"plugins": ["no-floating-promise","@typescript-eslint"],
"extends": [
"plugin:wdio/recommended",
"plugin:@typescript-eslint/recommended",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"rules": {
"@typescript-eslint/no-floating-promises": "error",
// RULES THAT NEED TO BE REMOVED
"class-methods-use-this": 0,
// ////////////////////////////////////////////////
"@typescript-eslint/explicit-module-boundary-types": 2,
"camelcase": 0,
"@typescript-eslint/no-inferrable-types": 0,
"flowtype/no-types-missing-file-annotation": 0,
"@typescript-eslint/ban-types": 1,
"@typescript-eslint/no-namespace": 0,
"no-shadow": "off", // turn off eslint rule and use next one instead
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/no-explicit-any": "error"
}
}
25 changes: 0 additions & 25 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@typescript-eslint/parser": "^4.11.0",
"chai-as-promised": "^7.1.1",
"eslint": "^7.16.0",
"eslint-plugin-no-floating-promise": "^1.0.2",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"eslint-plugin-wdio": "^6.6.0",
Expand Down
20 changes: 10 additions & 10 deletions src/commons/BrowserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ export namespace BrowserUtils {
* to insure the navigation actually happened
* @param url url for navigation
*/
export async function url(url: string): Promise<void> {
await Reporter.debug(`Navigate to '${url}'`);
await tryBlock(async () => await browser.url(url), `Failed to navigate to '${url}'`);
export async function url(urlToNavigate: string): Promise<void> {
await Reporter.debug(`Navigate to '${urlToNavigate}'`);
await tryBlock(async () => await browser.url(urlToNavigate), `Failed to navigate to '${urlToNavigate}'`);
}

/**
Expand Down Expand Up @@ -211,8 +211,8 @@ export namespace BrowserUtils {
* Mainly useful for navigation validation
* @param url expected current url
*/
export async function waitForUrl(url: string): Promise<void> {
const expectedUrl: string = normalizeUrl(url);
export async function waitForUrl(urlToNavigate: string): Promise<void> {
const expectedUrl: string = normalizeUrl(urlToNavigate);
await Reporter.debug(`Wait for URL to be , '${expectedUrl}'`);
await waitUntil(
async () => {
Expand All @@ -231,14 +231,14 @@ export namespace BrowserUtils {
* WDIO return url with backslash at the end of url,
* while user mainly passes without the backslash
* Removing the last backslash will solve error on url comparison
* @param url url to remove backslash from
* @param urlToNormalize url to remove backslash from
*/
export function normalizeUrl(url: string): string {
if (url === null || url === undefined) {
throw new Error(`Illegal URL: '${url}'`);
export function normalizeUrl(urlToNormalize: string): string {
if (urlToNormalize === null || urlToNormalize === undefined) {
throw new Error(`Illegal URL: '${urlToNormalize}'`);
}

return url.replace(/\/+$/, '');
return urlToNormalize.replace(/\/+$/, '');
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/commons/TestRailUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ export namespace TestRailUtil {
* Update an array of tests automation field on testrail to automated
* @param testIDs array of tests Ids
*/
export function setTestsAsAutomatedInTestrail(testIDs: Set<string>): void {
export async function setTestsAsAutomatedInTestrail(testIDs: Set<string>): Promise<void> {
console.log(`About to update ${Array.from(testIDs.values())} on testrail`);
for (const testId of testIDs) {
changeTestField(testId, TestFields.Automation, TestFields.Automation.fieldOptions.automated).then((res) => {
console.log(`Finished update test C${testId} with status code: ${res.status}`);
});
await changeTestField(testId, TestFields.Automation, TestFields.Automation.fieldOptions.automated);
}
}

/**
* Update an array of tests automation field on testrail from specific folder
* @param folderPath path of tests files to update
*/
export function setTestsAsAutomatedInTestrailFromPath(folderPath?: string): void {
export async function setTestsAsAutomatedInTestrailFromPath(folderPath?: string): Promise<void> {
console.log(`Setting tests as 'Automated' from path ${folderPath}`);
const testIDs = TestFilesUtils.getTestIdsFromFolder(folderPath);
setTestsAsAutomatedInTestrail(testIDs);
await setTestsAsAutomatedInTestrail(testIDs);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/commons/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export namespace TestUtils {
* @param dataTag type of file
* configDataFilePath will take from wdio.config file
*/
export function getData<T>(dataTag: string = process.env.TEST_DATA_TAG): T {
export async function getData<T>(dataTag: string = process.env.TEST_DATA_TAG): Promise<T> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const dataFilename = browser.config.configDataFilePath;
Expand All @@ -61,10 +61,10 @@ export namespace TestUtils {
if (dataFilePath === undefined) {
throw new Error('Path to data file is incorrect');
}
Reporter.debug(`Getting data from file ${dataFilePath}`);
await Reporter.debug(`Getting data from file ${dataFilePath}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const data: T = require(dataFilePath);
Reporter.debug(
await Reporter.debug(
`Received a data ${JSON.stringify(data)} from file by provided tag ${JSON.stringify(data[dataTag])}`
);
return data && data[dataTag];
Expand Down
12 changes: 6 additions & 6 deletions src/test/specs/GetTestDataFileSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ interface ITestData {
}

describeCommon('GetTestDataFileSpec', () => {
it('Check get test data from env file', () => {
it('Check get test data from env file', async () => {
process.env.TEST_DATA_TAG = 'test-user';
Reporter.step('getData reads from file');
const data: ITestData = TestUtils.getData();
await Reporter.step('getData reads from file');
const data: ITestData = await TestUtils.getData();
assert.equal(data.fileName, 'example');
});

it('Check incorrect user', () => {
it('Check incorrect user', async () => {
process.env.TEST_DATA_TAG = 'incorrect-user';
Reporter.step('undefined returned in case of incorrect data tag');
const data: ITestData = TestUtils.getData();
await Reporter.step('undefined returned in case of incorrect data tag');
const data: ITestData = await TestUtils.getData();
assert.equal(data, undefined);
});
});
6 changes: 3 additions & 3 deletions src/test/specs/NavigateToUrlSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { describeCommon, sampleAppUrl } from '../TestHelper';
* wdio-allure-ts url action test
*/
describeCommon('url', () => {
it('navigate successfully', () => {
Reporter.step('Navigate to sample app');
BrowserUtils.url(sampleAppUrl);
it('navigate successfully', async () => {
await Reporter.step('Navigate to sample app');
await BrowserUtils.url(sampleAppUrl);
});
});
24 changes: 12 additions & 12 deletions src/test/specs/NormalizeUrlSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ import { expect } from 'chai';
import { BrowserUtils, Reporter } from '../..';

describe('NormalizeUrl', () => {
it('throw if url is null', () => {
it('throw if url is null', async () => {
const url: string = null;
Reporter.step('normalize illegal url');
await Reporter.step('normalize illegal url');
expect(() => BrowserUtils.normalizeUrl(url))
.to.throw(Error)
.with.property('message')
.contains(`Illegal URL: '${url}'`);
});

it('throw if url is undefined', () => {
Reporter.step('throw if url is undefined');
it('throw if url is undefined', async () => {
await Reporter.step('throw if url is undefined');
const url: string = undefined;
expect(() => BrowserUtils.normalizeUrl(url))
.to.throw(Error)
.with.property('message')
.contains(`Illegal URL: '${url}'`);
});

it('expect / removed', () => {
it('expect / removed', async () => {
const url: string = 'someString/';
Reporter.step("expect '/' removed");
await Reporter.step("expect '/' removed");
expect(BrowserUtils.normalizeUrl(url)).to.equal('someString');
});

it('expect // removed', () => {
it('expect // removed', async () => {
const url: string = 'someString/';
Reporter.step("expect '//' removed");
await Reporter.step("expect '//' removed");
expect(BrowserUtils.normalizeUrl(url)).to.equal('someString');
});

it('empty string', () => {
it('empty string', async () => {
const url: string = '';
Reporter.step('empty string');
await Reporter.step('empty string');
expect(BrowserUtils.normalizeUrl(url)).to.equal('');
});

it('//// to be empty', () => {
Reporter.step('//// to be empty');
it('//// to be empty', async () => {
await Reporter.step('//// to be empty');
const url: string = '////';
expect(BrowserUtils.normalizeUrl(url)).to.equal('');
});
Expand Down
38 changes: 19 additions & 19 deletions src/test/specs/TestUtilsSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,59 @@ import { Reporter, TestUtils } from '../..';

describe('TestUtilsSpec', () => {
describe('randomStringTest', () => {
it('default random string length', () => {
Reporter.step('generate random string');
it('default random string length', async () => {
await Reporter.step('generate random string');
const randStr: string = TestUtils.randomString();

Reporter.step('Validate strings default length');
await Reporter.step('Validate strings default length');
assert.equal(randStr.length, 5);
});

it('random string of provided length', () => {
Reporter.step('generate random string with given length');
it('random string of provided length', async () => {
await Reporter.step('generate random string with given length');
const randStr: string = TestUtils.randomString(7);

Reporter.step('Validate strings length');
await Reporter.step('Validate strings length');
assert.equal(randStr.length, 7);
});

it('strings are randoms', () => {
Reporter.step('generate random string 1');
it('strings are randoms', async () => {
await Reporter.step('generate random string 1');
const randStr1: string = TestUtils.randomString();

Reporter.step('generate random string 2');
await Reporter.step('generate random string 2');
const randStr2: string = TestUtils.randomString();

Reporter.step('Validate strings are not equal');
await Reporter.step('Validate strings are not equal');
assert.notEqual(randStr1, randStr2);
});

it('letters only', () => {
Reporter.step('Generate letters only string');
it('letters only', async () => {
await Reporter.step('Generate letters only string');
const randStr = TestUtils.randomString(5, true);

Reporter.step('Validate string contains letters only');
await Reporter.step('Validate string contains letters only');
assert.isTrue(!/\d/.test(randStr));
});
});

describe('extractNumbersFromString', () => {
it('string contains letters and numbers', () => {
Reporter.step('Validate extractNumbersFromString');
it('string contains letters and numbers', async () => {
await Reporter.step('Validate extractNumbersFromString');
const str: string = 'abc2de3mnb';
const expectedNumber = 23;
assert.equal(Number(TestUtils.extractNumbersFromString(str)), expectedNumber);
});
});
describe('isTimePassed', () => {
it('expect to return true', () => {
Reporter.step('Validate isTimePassed - true');
it('expect to return true', async () => {
await Reporter.step('Validate isTimePassed - true');
const expectedDate = new Date(2000, 1);
assert.isTrue(TestUtils.isTimePassed(expectedDate, 5));
});

it('expect to return false', () => {
Reporter.step('Validate isTimePassed - false');
it('expect to return false', async () => {
await Reporter.step('Validate isTimePassed - false');
const expectedDate = new Date(3000, 1);
assert.isNotTrue(TestUtils.isTimePassed(expectedDate, 5));
});
Expand Down
12 changes: 6 additions & 6 deletions src/test/specs/startNetworkAuditSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ interface NetworkLog {
* DevTools - Start network audit
*/
describeCommon('startNetworkAudit', () => {
it('successfully start and read network audit', () => {
it('successfully start and read network audit', async () => {
const expectedLog: NetworkLog = { url: 'http://placekitten.com/480/480', status: 200 };
const networkLogs: Array<NetworkLog> = [];

Reporter.step('Start network log');
await Reporter.step('Start network log');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
browser.on('Network.responseReceived', (params: any) => {
networkLogs.push({
Expand All @@ -23,11 +23,11 @@ describeCommon('startNetworkAudit', () => {
});
});

Reporter.step('navigate to sample app');
BrowserUtils.url(sampleAppUrl);
await Reporter.step('navigate to sample app');
await BrowserUtils.url(sampleAppUrl);

Reporter.step('Wait for new logs');
BrowserUtils.waitUntil(
await Reporter.step('Wait for new logs');
await BrowserUtils.waitUntil(
() => {
return networkLogs.some((log) => log.url === expectedLog.url && Number(log.status) === expectedLog.status);
},
Expand Down
18 changes: 12 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
"types": ["node", "webdriverio/async", "mocha", "@wdio/mocha-framework", "@wdio/devtools-service"],
"target": "es6",
"module": "commonjs",
"declaration": true,
"outDir": "lib/",
"rootDir": "src/",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"resolveJsonModule": true,
"outDir": "lib/",
"rootDir": "src/",
"declaration": true,
"esModuleInterop": true,
"skipLibCheck": true //todo temporary solution since there is type missmatch in the reportportal package
"skipLibCheck": true
},
"exclude": ["node_modules", "lib", "src/test"],

"compileOnSave": false
}
Loading

0 comments on commit 5e26fa2

Please sign in to comment.