From b806db35f8d94b0e6e70c3b4e8232b6fa816a716 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 7 Jun 2022 09:48:01 -0500 Subject: [PATCH] [Tests] Migrate mocha tests to jest (#1553) (#1678) - `src/dev` - migrate mocha tests to jest - `src/test_utils` - migrate mocha tests to jest - `src/dev/license_checker` - migrate mocha tests to jest - `src/legacy/utils` - migrate mocha tests to jest - convert to typescript - add unit test for `version.ts` - `src/plugins/console/server` - migrate mocha tests to jest - convert to typescript - fix type errors - improve test cases - update URL parsing - `packages/opensearch-datemath - migrate mocha tests to jest - convert to typescript - `packages/osd-eslint-plugin-eslint` - migrate mocha tests to jest - rename test file directory - `packages/osd-opensearch-archiver` - migrate mocha tests to jest - rename stub directory - `packages/osd-test` - migrate mocha tests to jest - remove mocha timeouts (not needed in jest) - linting fixes - `packages/osd-test-subject-selector` - migrate mocha tests to jest - convert to typescript - update tsconfig to transpile ts - switch to named exports/imports Verified that 410 of previous 411 mocha test cases are correctly ran by jest. Mocha: 1 passing test (mocha report generation, which will be removed in https://github.com/opensearch-project/OpenSearch-Dashboards/issues/1572) Jest: 11155 passing tests (11155 = 10741 previous jest tests + 4 newly added jest test + 410 migrated mocha tests) Resolves #215 Signed-off-by: Josh Romero (cherry picked from commit 0e414776353111149da8c6819e86f47b53d9a44f) --- .../{__tests__/index.js => index.test.ts} | 13 +- ...rs.js => disallow_license_headers.test.js} | 2 +- ...d_paths.js => no_restricted_paths.test.js} | 99 ++++++++------- ...ader.js => require_license_header.test.js} | 2 +- .../no_restricted_paths/client/a.js | 0 .../no_restricted_paths/server/b.js | 0 .../no_restricted_paths/server/c.js | 0 .../no_restricted_paths/server/deep/d.js | 0 .../server/index_patterns/index.js | 0 .../{__tests__/format.ts => format.test.ts} | 4 +- .../{__tests__/parse.ts => parse.test.ts} | 10 +- ...ts => generate_doc_records_stream.test.ts} | 8 +- ...am.ts => index_doc_records_stream.test.ts} | 11 +- .../{__tests__/stubs.ts => test_stubs.ts} | 2 +- ..._stream.ts => create_index_stream.test.ts} | 6 +- ..._stream.ts => delete_index_stream.test.ts} | 6 +- ... => generate_index_records_stream.test.ts} | 7 +- .../{__tests__/stubs.ts => test_stubs.ts} | 2 +- ...tream.ts => filter_records_stream.test.ts} | 4 +- .../lib/{__tests__/stats.ts => stats.test.ts} | 5 +- packages/osd-test-subj-selector/index.d.ts | 32 ----- packages/osd-test-subj-selector/package.json | 6 +- .../{__tests__/index.js => src/index.test.ts} | 4 +- .../{index.js => src/index.ts} | 12 +- packages/osd-test-subj-selector/tsconfig.json | 10 +- .../fixtures/failure_hooks/config.js | 10 +- .../failure_hooks/tests/after_hook.js | 0 .../failure_hooks/tests/before_hook.js | 0 .../fixtures/failure_hooks/tests/it.js | 0 .../fixtures/simple_project/config.js | 4 +- .../fixtures/simple_project/tests.js | 0 .../basic.js => integration/basic.test.ts} | 4 +- .../failure_hooks.test.js} | 4 +- .../{__tests__ => }/fixtures/config.1.js | 4 +- .../{__tests__ => }/fixtures/config.2.js | 5 +- .../{__tests__ => }/fixtures/config.3.js | 8 +- .../{__tests__ => }/fixtures/config.4.js | 4 +- .../fixtures/config.invalid.js | 2 +- ...onfig_file.js => read_config_file.test.ts} | 7 +- .../fatal_errors/fatal_errors_screen.test.tsx | 2 +- src/dev/{__tests__/file.js => file.test.ts} | 9 +- .../{__tests__/valid.js => valid.test.ts} | 15 +-- ...ch.js => node_versions_must_match.test.ts} | 2 +- src/legacy/utils/index.d.ts | 31 ----- src/legacy/utils/{index.js => index.ts} | 0 .../{__tests__/unset.js => unset.test.ts} | 8 +- src/legacy/utils/{unset.js => unset.ts} | 14 ++- src/legacy/utils/version.test.ts | 36 ++++++ src/legacy/utils/{version.js => version.ts} | 4 +- .../opensearch_proxy_config.test.ts} | 94 +++++++++----- .../server/lib/opensearch_proxy_config.ts | 2 +- .../proxy_config.test.ts} | 119 +++++++++++++----- .../console/server/lib/proxy_config.ts | 5 +- .../proxy_config_collection.test.ts} | 18 +-- .../server/lib/proxy_config_collection.ts | 8 +- .../console/server/lib/proxy_request.test.ts | 3 +- .../console/server/lib/proxy_request.ts | 2 +- .../set_headers.test.ts} | 4 +- .../wildcard_matcher.test.ts} | 16 +-- src/plugins/console/server/types.ts | 11 ++ .../{__tests__/get_url.js => get_url.test.ts} | 2 +- .../services/common/test_subjects.ts | 2 +- .../web_element_wrapper.ts | 7 +- .../services/visual_testing/visual_testing.ts | 2 +- 64 files changed, 401 insertions(+), 312 deletions(-) rename packages/opensearch-datemath/{__tests__/index.js => index.test.ts} (98%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/disallow_license_headers.js => disallow_license_headers.test.js} (98%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/no_restricted_paths.js => no_restricted_paths.test.js} (71%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/require_license_header.js => require_license_header.test.js} (99%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/files => testfiles}/no_restricted_paths/client/a.js (100%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/files => testfiles}/no_restricted_paths/server/b.js (100%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/files => testfiles}/no_restricted_paths/server/c.js (100%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/files => testfiles}/no_restricted_paths/server/deep/d.js (100%) rename packages/osd-eslint-plugin-eslint/rules/{__tests__/files => testfiles}/no_restricted_paths/server/index_patterns/index.js (100%) rename packages/osd-opensearch-archiver/src/lib/archives/{__tests__/format.ts => format.test.ts} (97%) rename packages/osd-opensearch-archiver/src/lib/archives/{__tests__/parse.ts => parse.test.ts} (95%) rename packages/osd-opensearch-archiver/src/lib/docs/{__tests__/generate_doc_records_stream.ts => generate_doc_records_stream.test.ts} (95%) rename packages/osd-opensearch-archiver/src/lib/docs/{__tests__/index_doc_records_stream.ts => index_doc_records_stream.test.ts} (95%) rename packages/osd-opensearch-archiver/src/lib/docs/{__tests__/stubs.ts => test_stubs.ts} (98%) rename packages/osd-opensearch-archiver/src/lib/indices/{__tests__/create_index_stream.ts => create_index_stream.test.ts} (98%) rename packages/osd-opensearch-archiver/src/lib/indices/{__tests__/delete_index_stream.ts => delete_index_stream.test.ts} (93%) rename packages/osd-opensearch-archiver/src/lib/indices/{__tests__/generate_index_records_stream.ts => generate_index_records_stream.test.ts} (96%) rename packages/osd-opensearch-archiver/src/lib/indices/{__tests__/stubs.ts => test_stubs.ts} (99%) rename packages/osd-opensearch-archiver/src/lib/records/{__tests__/filter_records_stream.ts => filter_records_stream.test.ts} (96%) rename packages/osd-opensearch-archiver/src/lib/{__tests__/stats.ts => stats.test.ts} (98%) delete mode 100644 packages/osd-test-subj-selector/index.d.ts rename packages/osd-test-subj-selector/{__tests__/index.js => src/index.test.ts} (95%) rename packages/osd-test-subj-selector/{index.js => src/index.ts} (88%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/failure_hooks/config.js (93%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/failure_hooks/tests/after_hook.js (100%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/failure_hooks/tests/before_hook.js (100%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/failure_hooks/tests/it.js (100%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/simple_project/config.js (95%) rename packages/osd-test/src/functional_test_runner/{__tests__ => }/fixtures/simple_project/tests.js (100%) rename packages/osd-test/src/functional_test_runner/{__tests__/integration/basic.js => integration/basic.test.ts} (95%) rename packages/osd-test/src/functional_test_runner/{__tests__/integration/failure_hooks.js => integration/failure_hooks.test.js} (96%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__ => }/fixtures/config.1.js (96%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__ => }/fixtures/config.2.js (94%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__ => }/fixtures/config.3.js (94%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__ => }/fixtures/config.4.js (97%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__ => }/fixtures/config.invalid.js (98%) rename packages/osd-test/src/functional_test_runner/lib/config/{__tests__/read_config_file.js => read_config_file.test.ts} (93%) rename src/dev/{__tests__/file.js => file.test.ts} (91%) rename src/dev/license_checker/{__tests__/valid.js => valid.test.ts} (87%) rename src/dev/{__tests__/node_versions_must_match.js => node_versions_must_match.test.ts} (97%) delete mode 100644 src/legacy/utils/index.d.ts rename src/legacy/utils/{index.js => index.ts} (100%) rename src/legacy/utils/{__tests__/unset.js => unset.test.ts} (96%) rename src/legacy/utils/{unset.js => unset.ts} (82%) create mode 100644 src/legacy/utils/version.test.ts rename src/legacy/utils/{version.js => version.ts} (91%) rename src/plugins/console/server/{__tests__/opensearch_proxy_config.js => lib/opensearch_proxy_config.test.ts} (70%) rename src/plugins/console/server/{__tests__/proxy_config.js => lib/proxy_config.test.ts} (64%) rename src/plugins/console/server/{__tests__/proxy_config_collection.js => lib/proxy_config_collection.test.ts} (93%) rename src/plugins/console/server/{__tests__/set_headers.js => lib/set_headers.test.ts} (96%) rename src/plugins/console/server/{__tests__/wildcard_matcher.js => lib/wildcard_matcher.test.ts} (83%) rename src/test_utils/{__tests__/get_url.js => get_url.test.ts} (98%) diff --git a/packages/opensearch-datemath/__tests__/index.js b/packages/opensearch-datemath/index.test.ts similarity index 98% rename from packages/opensearch-datemath/__tests__/index.js rename to packages/opensearch-datemath/index.test.ts index 8188a2920026..e293da72ac7f 100644 --- a/packages/opensearch-datemath/__tests__/index.js +++ b/packages/opensearch-datemath/index.test.ts @@ -28,10 +28,10 @@ * under the License. */ -const dateMath = require('../index'); -const moment = require('moment'); -const sinon = require('sinon'); -const expect = require('@osd/expect'); +import dateMath from './index'; +import moment from 'moment'; +import sinon from 'sinon'; +import expect from '@osd/expect'; /** * Require a new instance of the moment library, bypassing the require cache. @@ -43,6 +43,7 @@ const expect = require('@osd/expect'); * of moment, by deleting the require cache and require the library again. */ function momentClone() { + jest.resetModules(); delete require.cache[require.resolve('moment')]; return require('moment'); } @@ -58,7 +59,7 @@ describe('dateMath', function () { describe('errors', function () { it('should return undefined if passed something falsy', function () { - expect(dateMath.parse()).to.be(undefined); + expect(dateMath.parse('')).to.be(undefined); }); it('should return undefined if I pass an operator besides [+-/]', function () { @@ -82,11 +83,13 @@ describe('dateMath', function () { describe('forceNow', function () { it('should throw an Error if passed a string', function () { + // @ts-ignore bad arg const fn = () => dateMath.parse('now', { forceNow: '2000-01-01T00:00:00.000Z' }); expect(fn).to.throwError(); }); it('should throw an Error if passed a moment', function () { + // @ts-ignore bad arg expect(() => dateMath.parse('now', { forceNow: moment() })).to.throwError(); }); diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/disallow_license_headers.js b/packages/osd-eslint-plugin-eslint/rules/disallow_license_headers.test.js similarity index 98% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/disallow_license_headers.js rename to packages/osd-eslint-plugin-eslint/rules/disallow_license_headers.test.js index 1afb406ea493..afe55f701f6e 100644 --- a/packages/osd-eslint-plugin-eslint/rules/__tests__/disallow_license_headers.js +++ b/packages/osd-eslint-plugin-eslint/rules/disallow_license_headers.test.js @@ -29,7 +29,7 @@ */ const { RuleTester } = require('eslint'); -const rule = require('../disallow_license_headers'); +const rule = require('./disallow_license_headers'); const dedent = require('dedent'); const ruleTester = new RuleTester({ diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js similarity index 71% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js rename to packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js index 29788eb5456b..06e7a9c39414 100644 --- a/packages/osd-eslint-plugin-eslint/rules/__tests__/no_restricted_paths.js +++ b/packages/osd-eslint-plugin-eslint/rules/no_restricted_paths.test.js @@ -40,7 +40,7 @@ const path = require('path'); const { RuleTester } = require('eslint'); -const rule = require('../no_restricted_paths'); +const rule = require('./no_restricted_paths'); const ruleTester = new RuleTester({ parser: require.resolve('babel-eslint'), @@ -54,14 +54,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { valid: [ { code: 'import a from "../client/a.js"', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/server/**/*', - from: 'files/no_restricted_paths/other/**/*', + target: 'testfiles/no_restricted_paths/server/**/*', + from: 'testfiles/no_restricted_paths/other/**/*', }, ], }, @@ -69,14 +69,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'const a = require("../client/a.js")', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/server/**/*', - from: 'files/no_restricted_paths/other/**/*', + target: 'testfiles/no_restricted_paths/server/**/*', + from: 'testfiles/no_restricted_paths/other/**/*', }, ], }, @@ -84,7 +84,7 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'import b from "../server/b.js"', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, @@ -109,14 +109,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'notrequire("../server/b.js")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/client/**/*', - from: 'files/no_restricted_paths/server/**/*', + target: 'testfiles/no_restricted_paths/client/**/*', + from: 'testfiles/no_restricted_paths/server/**/*', }, ], }, @@ -153,15 +153,15 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { code: 'const d = require("./deep/d.js")', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { allowSameFolder: true, - target: 'files/no_restricted_paths/**/*', - from: 'files/no_restricted_paths/**/*', + target: 'testfiles/no_restricted_paths/**/*', + from: 'testfiles/no_restricted_paths/**/*', }, ], }, @@ -169,15 +169,18 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'const d = require("./deep/d.js")', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { allowSameFolder: true, - target: 'files/no_restricted_paths/**/*', - from: ['files/no_restricted_paths/**/*', '!files/no_restricted_paths/server/b*'], + target: 'testfiles/no_restricted_paths/**/*', + from: [ + 'testfiles/no_restricted_paths/**/*', + '!testfiles/no_restricted_paths/server/b*', + ], }, ], }, @@ -187,16 +190,16 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { // Check if dirs that start with 'index' work correctly. code: 'import { X } from "./index_patterns"', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: ['files/no_restricted_paths/(public|server)/**/*'], + target: ['testfiles/no_restricted_paths/(public|server)/**/*'], from: [ - 'files/no_restricted_paths/server/**/*', - '!files/no_restricted_paths/server/index.{ts,tsx}', + 'testfiles/no_restricted_paths/server/**/*', + '!testfiles/no_restricted_paths/server/index.{ts,tsx}', ], allowSameFolder: true, }, @@ -209,14 +212,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { invalid: [ { code: 'export { b } from "../server/b.js"', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/client/**/*', - from: 'files/no_restricted_paths/server/**/*', + target: 'testfiles/no_restricted_paths/client/**/*', + from: 'testfiles/no_restricted_paths/server/**/*', }, ], }, @@ -231,14 +234,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'import b from "../server/b.js"', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/client/**/*', - from: 'files/no_restricted_paths/server/**/*', + target: 'testfiles/no_restricted_paths/client/**/*', + from: 'testfiles/no_restricted_paths/server/**/*', }, ], }, @@ -253,18 +256,18 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'import a from "../client/a"\nimport c from "./c"', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/server/**/*', - from: 'files/no_restricted_paths/client/**/*', + target: 'testfiles/no_restricted_paths/server/**/*', + from: 'testfiles/no_restricted_paths/client/**/*', }, { - target: 'files/no_restricted_paths/server/**/*', - from: 'files/no_restricted_paths/server/c.js', + target: 'testfiles/no_restricted_paths/server/**/*', + from: 'testfiles/no_restricted_paths/server/c.js', }, ], }, @@ -284,7 +287,7 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'const b = require("../server/b.js")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, @@ -306,10 +309,10 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { }, { code: 'const b = require("../server/b.js")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { - basePath: path.join(__dirname, 'files', 'no_restricted_paths'), + basePath: path.join(__dirname, 'testfiles', 'no_restricted_paths'), zones: [ { target: 'client/**/*', @@ -329,14 +332,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { code: 'const d = require("./deep/d.js")', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/**/*', - from: 'files/no_restricted_paths/**/*', + target: 'testfiles/no_restricted_paths/**/*', + from: 'testfiles/no_restricted_paths/**/*', }, ], }, @@ -353,13 +356,13 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { // Does not allow to import deeply within Core, using "src/core/..." Webpack alias. code: 'const d = require("src/core/server/saved_objects")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { - target: 'files/no_restricted_paths/**/*', + target: 'testfiles/no_restricted_paths/**/*', from: 'src/core/server/**/*', }, ], @@ -377,14 +380,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { // Does not allow to import "ui/kfetch". code: 'const d = require("ui/kfetch")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { from: ['src/legacy/ui/**/*', 'ui/**/*'], - target: 'files/no_restricted_paths/**/*', + target: 'testfiles/no_restricted_paths/**/*', allowSameFolder: true, }, ], @@ -402,14 +405,14 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { { // Does not allow to import deeply "ui/kfetch/public/index". code: 'const d = require("ui/kfetch/public/index")', - filename: path.join(__dirname, './files/no_restricted_paths/client/a.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/client/a.js'), options: [ { basePath: __dirname, zones: [ { from: ['src/legacy/ui/**/*', 'ui/**/*'], - target: 'files/no_restricted_paths/**/*', + target: 'testfiles/no_restricted_paths/**/*', allowSameFolder: true, }, ], @@ -428,16 +431,16 @@ ruleTester.run('@osd/eslint/no-restricted-paths', rule, { // Don't use index*. // It won't work with dirs that start with 'index'. code: 'import { X } from "./index_patterns"', - filename: path.join(__dirname, './files/no_restricted_paths/server/b.js'), + filename: path.join(__dirname, './testfiles/no_restricted_paths/server/b.js'), options: [ { basePath: __dirname, zones: [ { - target: ['files/no_restricted_paths/(public|server)/**/*'], + target: ['testfiles/no_restricted_paths/(public|server)/**/*'], from: [ - 'files/no_restricted_paths/server/**/*', - '!files/no_restricted_paths/server/index*', + 'testfiles/no_restricted_paths/server/**/*', + '!testfiles/no_restricted_paths/server/index*', ], allowSameFolder: true, }, diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/require_license_header.js b/packages/osd-eslint-plugin-eslint/rules/require_license_header.test.js similarity index 99% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/require_license_header.js rename to packages/osd-eslint-plugin-eslint/rules/require_license_header.test.js index 7de94356b860..b67aa986aa67 100644 --- a/packages/osd-eslint-plugin-eslint/rules/__tests__/require_license_header.js +++ b/packages/osd-eslint-plugin-eslint/rules/require_license_header.test.js @@ -29,7 +29,7 @@ */ const { RuleTester } = require('eslint'); -const rule = require('../require_license_header'); +const rule = require('./require_license_header'); const dedent = require('dedent'); const ruleTester = new RuleTester({ diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/client/a.js b/packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/client/a.js similarity index 100% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/client/a.js rename to packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/client/a.js diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/b.js b/packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/b.js similarity index 100% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/b.js rename to packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/b.js diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/c.js b/packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/c.js similarity index 100% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/c.js rename to packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/c.js diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/deep/d.js b/packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/deep/d.js similarity index 100% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/deep/d.js rename to packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/deep/d.js diff --git a/packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/index_patterns/index.js b/packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/index_patterns/index.js similarity index 100% rename from packages/osd-eslint-plugin-eslint/rules/__tests__/files/no_restricted_paths/server/index_patterns/index.js rename to packages/osd-eslint-plugin-eslint/rules/testfiles/no_restricted_paths/server/index_patterns/index.js diff --git a/packages/osd-opensearch-archiver/src/lib/archives/__tests__/format.ts b/packages/osd-opensearch-archiver/src/lib/archives/format.test.ts similarity index 97% rename from packages/osd-opensearch-archiver/src/lib/archives/__tests__/format.ts rename to packages/osd-opensearch-archiver/src/lib/archives/format.test.ts index 3c9f62b10e59..fe303cd2e082 100644 --- a/packages/osd-opensearch-archiver/src/lib/archives/__tests__/format.ts +++ b/packages/osd-opensearch-archiver/src/lib/archives/format.test.ts @@ -33,9 +33,9 @@ import { createGunzip } from 'zlib'; import expect from '@osd/expect'; -import { createListStream, createPromiseFromStreams, createConcatStream } from '../../streams'; +import { createListStream, createPromiseFromStreams, createConcatStream } from '../streams'; -import { createFormatArchiveStreams } from '../format'; +import { createFormatArchiveStreams } from './format'; const INPUTS = [1, 2, { foo: 'bar' }, [1, 2]]; const INPUT_JSON = INPUTS.map((i) => JSON.stringify(i, null, 2)).join('\n\n'); diff --git a/packages/osd-opensearch-archiver/src/lib/archives/__tests__/parse.ts b/packages/osd-opensearch-archiver/src/lib/archives/parse.test.ts similarity index 95% rename from packages/osd-opensearch-archiver/src/lib/archives/__tests__/parse.ts rename to packages/osd-opensearch-archiver/src/lib/archives/parse.test.ts index bdcd9d0cd908..9f574a1aacff 100644 --- a/packages/osd-opensearch-archiver/src/lib/archives/__tests__/parse.ts +++ b/packages/osd-opensearch-archiver/src/lib/archives/parse.test.ts @@ -33,9 +33,9 @@ import { createGzip } from 'zlib'; import expect from '@osd/expect'; -import { createConcatStream, createListStream, createPromiseFromStreams } from '../../streams'; +import { createConcatStream, createListStream, createPromiseFromStreams } from '../streams'; -import { createParseArchiveStreams } from '../parse'; +import { createParseArchiveStreams } from './parse'; describe('opensearchArchiver createParseArchiveStreams', () => { describe('{ gzip: false }', () => { @@ -120,7 +120,8 @@ describe('opensearchArchiver createParseArchiveStreams', () => { ] as [Readable, ...Writable[]]); throw new Error('should have failed'); } catch (err) { - expect(err.message).to.contain('Unexpected number'); + const { message } = err as Error; + expect(message).to.contain('Unexpected number'); } }); }); @@ -183,7 +184,8 @@ describe('opensearchArchiver createParseArchiveStreams', () => { ] as [Readable, ...Writable[]]); throw new Error('should have failed'); } catch (err) { - expect(err.message).to.contain('incorrect header check'); + const { message } = err as Error; + expect(message).to.contain('incorrect header check'); } }); }); diff --git a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/generate_doc_records_stream.ts b/packages/osd-opensearch-archiver/src/lib/docs/generate_doc_records_stream.test.ts similarity index 95% rename from packages/osd-opensearch-archiver/src/lib/docs/__tests__/generate_doc_records_stream.ts rename to packages/osd-opensearch-archiver/src/lib/docs/generate_doc_records_stream.test.ts index 49e8a7cbeea2..462aba6c234a 100644 --- a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/generate_doc_records_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/docs/generate_doc_records_stream.test.ts @@ -32,11 +32,11 @@ import sinon from 'sinon'; import expect from '@osd/expect'; import { delay } from 'bluebird'; -import { createListStream, createPromiseFromStreams, createConcatStream } from '../../streams'; +import { createListStream, createPromiseFromStreams, createConcatStream } from '../streams'; -import { createGenerateDocRecordsStream } from '../generate_doc_records_stream'; -import { Progress } from '../../progress'; -import { createStubStats, createStubClient } from './stubs'; +import { createGenerateDocRecordsStream } from './generate_doc_records_stream'; +import { Progress } from '../progress'; +import { createStubStats, createStubClient } from './test_stubs'; describe('opensearchArchiver: createGenerateDocRecordsStream()', () => { it('scolls 1000 documents at a time', async () => { diff --git a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/index_doc_records_stream.ts b/packages/osd-opensearch-archiver/src/lib/docs/index_doc_records_stream.test.ts similarity index 95% rename from packages/osd-opensearch-archiver/src/lib/docs/__tests__/index_doc_records_stream.ts rename to packages/osd-opensearch-archiver/src/lib/docs/index_doc_records_stream.test.ts index 481a7ef02614..00b9e22660ca 100644 --- a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/index_doc_records_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/docs/index_doc_records_stream.test.ts @@ -31,11 +31,11 @@ import expect from '@osd/expect'; import { delay } from 'bluebird'; -import { createListStream, createPromiseFromStreams } from '../../streams'; +import { createListStream, createPromiseFromStreams } from '../streams'; -import { Progress } from '../../progress'; -import { createIndexDocRecordsStream } from '../index_doc_records_stream'; -import { createStubStats, createStubClient, createPersonDocRecords } from './stubs'; +import { Progress } from '../progress'; +import { createIndexDocRecordsStream } from './index_doc_records_stream'; +import { createStubStats, createStubClient, createPersonDocRecords } from './test_stubs'; const recordsToBulkBody = (records: any[]) => { return records.reduce((acc, record) => { @@ -201,7 +201,8 @@ describe('opensearchArchiver: createIndexDocRecordsStream()', () => { ]); throw new Error('expected stream to emit error'); } catch (err) { - expect(err.message).to.match(/"forcedError":\s*true/); + const { message } = err as Error; + expect(message).to.match(/"forcedError":\s*true/); } client.assertNoPendingResponses(); diff --git a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/stubs.ts b/packages/osd-opensearch-archiver/src/lib/docs/test_stubs.ts similarity index 98% rename from packages/osd-opensearch-archiver/src/lib/docs/__tests__/stubs.ts rename to packages/osd-opensearch-archiver/src/lib/docs/test_stubs.ts index c5a64a8bd28d..27b5803cc31b 100644 --- a/packages/osd-opensearch-archiver/src/lib/docs/__tests__/stubs.ts +++ b/packages/osd-opensearch-archiver/src/lib/docs/test_stubs.ts @@ -33,7 +33,7 @@ import sinon from 'sinon'; import Chance from 'chance'; import { times } from 'lodash'; -import { Stats } from '../../stats'; +import { Stats } from '../stats'; const chance = new Chance(); diff --git a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/create_index_stream.ts b/packages/osd-opensearch-archiver/src/lib/indices/create_index_stream.test.ts similarity index 98% rename from packages/osd-opensearch-archiver/src/lib/indices/__tests__/create_index_stream.ts rename to packages/osd-opensearch-archiver/src/lib/indices/create_index_stream.test.ts index af6000860295..200a6a79f965 100644 --- a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/create_index_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/indices/create_index_stream.test.ts @@ -32,9 +32,9 @@ import expect from '@osd/expect'; import sinon from 'sinon'; import Chance from 'chance'; -import { createPromiseFromStreams, createConcatStream, createListStream } from '../../streams'; +import { createPromiseFromStreams, createConcatStream, createListStream } from '../streams'; -import { createCreateIndexStream } from '../create_index_stream'; +import { createCreateIndexStream } from './create_index_stream'; import { createStubStats, @@ -42,7 +42,7 @@ import { createStubDocRecord, createStubClient, createStubLogger, -} from './stubs'; +} from './test_stubs'; const chance = new Chance(); diff --git a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/delete_index_stream.ts b/packages/osd-opensearch-archiver/src/lib/indices/delete_index_stream.test.ts similarity index 93% rename from packages/osd-opensearch-archiver/src/lib/indices/__tests__/delete_index_stream.ts rename to packages/osd-opensearch-archiver/src/lib/indices/delete_index_stream.test.ts index a4148e8ed9bf..19b3322d501f 100644 --- a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/delete_index_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/indices/delete_index_stream.test.ts @@ -30,16 +30,16 @@ import sinon from 'sinon'; -import { createListStream, createPromiseFromStreams } from '../../streams'; +import { createListStream, createPromiseFromStreams } from '../streams'; -import { createDeleteIndexStream } from '../delete_index_stream'; +import { createDeleteIndexStream } from './delete_index_stream'; import { createStubStats, createStubClient, createStubIndexRecord, createStubLogger, -} from './stubs'; +} from './test_stubs'; const log = createStubLogger(); diff --git a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/generate_index_records_stream.ts b/packages/osd-opensearch-archiver/src/lib/indices/generate_index_records_stream.test.ts similarity index 96% rename from packages/osd-opensearch-archiver/src/lib/indices/__tests__/generate_index_records_stream.ts rename to packages/osd-opensearch-archiver/src/lib/indices/generate_index_records_stream.test.ts index 360061fd1aa1..a961cca3550e 100644 --- a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/generate_index_records_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/indices/generate_index_records_stream.test.ts @@ -31,11 +31,10 @@ import sinon from 'sinon'; import expect from '@osd/expect'; -import { createListStream, createPromiseFromStreams, createConcatStream } from '../../streams'; +import { createListStream, createPromiseFromStreams, createConcatStream } from '../streams'; -import { createStubClient, createStubStats } from './stubs'; - -import { createGenerateIndexRecordsStream } from '../generate_index_records_stream'; +import { createStubClient, createStubStats } from './test_stubs'; +import { createGenerateIndexRecordsStream } from './generate_index_records_stream'; describe('opensearchArchiver: createGenerateIndexRecordsStream()', () => { it('consumes index names and queries for the mapping of each', async () => { diff --git a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/stubs.ts b/packages/osd-opensearch-archiver/src/lib/indices/test_stubs.ts similarity index 99% rename from packages/osd-opensearch-archiver/src/lib/indices/__tests__/stubs.ts rename to packages/osd-opensearch-archiver/src/lib/indices/test_stubs.ts index 10160ebc11f1..64b42a629cd6 100644 --- a/packages/osd-opensearch-archiver/src/lib/indices/__tests__/stubs.ts +++ b/packages/osd-opensearch-archiver/src/lib/indices/test_stubs.ts @@ -31,7 +31,7 @@ import { Client } from 'elasticsearch'; import sinon from 'sinon'; import { ToolingLog } from '@osd/dev-utils'; -import { Stats } from '../../stats'; +import { Stats } from '../stats'; type StubStats = Stats & { getTestSummary: () => Record; diff --git a/packages/osd-opensearch-archiver/src/lib/records/__tests__/filter_records_stream.ts b/packages/osd-opensearch-archiver/src/lib/records/filter_records_stream.test.ts similarity index 96% rename from packages/osd-opensearch-archiver/src/lib/records/__tests__/filter_records_stream.ts rename to packages/osd-opensearch-archiver/src/lib/records/filter_records_stream.test.ts index f670cf466475..ba93a31ec417 100644 --- a/packages/osd-opensearch-archiver/src/lib/records/__tests__/filter_records_stream.ts +++ b/packages/osd-opensearch-archiver/src/lib/records/filter_records_stream.test.ts @@ -31,9 +31,9 @@ import Chance from 'chance'; import expect from '@osd/expect'; -import { createListStream, createPromiseFromStreams, createConcatStream } from '../../streams'; +import { createListStream, createPromiseFromStreams, createConcatStream } from '../streams'; -import { createFilterRecordsStream } from '../filter_records_stream'; +import { createFilterRecordsStream } from './filter_records_stream'; const chance = new Chance(); diff --git a/packages/osd-opensearch-archiver/src/lib/__tests__/stats.ts b/packages/osd-opensearch-archiver/src/lib/stats.test.ts similarity index 98% rename from packages/osd-opensearch-archiver/src/lib/__tests__/stats.ts rename to packages/osd-opensearch-archiver/src/lib/stats.test.ts index a622599b205c..da7c6d240d0d 100644 --- a/packages/osd-opensearch-archiver/src/lib/__tests__/stats.ts +++ b/packages/osd-opensearch-archiver/src/lib/stats.test.ts @@ -33,7 +33,7 @@ import sinon from 'sinon'; import expect from '@osd/expect'; import { ToolingLog } from '@osd/dev-utils'; -import { createStats } from '../'; +import { createStats } from './'; function createBufferedLog(): ToolingLog & { buffer: string } { const log: ToolingLog = new ToolingLog({ @@ -65,10 +65,11 @@ function assertDeepClones(a: any, b: any) { }); })(a, b); } catch (err) { + const { stack } = err as Error; throw new Error( `Expected a and b to be deep clones of each other, error at:\n\n` + ` "${path.join('.') || '-'}"\n\n` + - err.stack + stack ); } } diff --git a/packages/osd-test-subj-selector/index.d.ts b/packages/osd-test-subj-selector/index.d.ts deleted file mode 100644 index 3ac568b2efd8..000000000000 --- a/packages/osd-test-subj-selector/index.d.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - * - * Any modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -// eslint-disable-next-line import/no-default-export -export default function osdTestSubjSelector(selector: string): string; diff --git a/packages/osd-test-subj-selector/package.json b/packages/osd-test-subj-selector/package.json index ef38899cadaf..b078e6d3a7af 100755 --- a/packages/osd-test-subj-selector/package.json +++ b/packages/osd-test-subj-selector/package.json @@ -2,10 +2,14 @@ "name": "@osd/test-subj-selector", "version": "0.2.1", "description": "", - "main": "index.js", + "main": "./target/index.js", "keywords": [], "author": "Spencer Alger ", "license": "Apache-2.0", + "scripts": { + "build": "tsc", + "osd:bootstrap": "yarn build" + }, "opensearchDashboards": { "devOnly": true } diff --git a/packages/osd-test-subj-selector/__tests__/index.js b/packages/osd-test-subj-selector/src/index.test.ts similarity index 95% rename from packages/osd-test-subj-selector/__tests__/index.js rename to packages/osd-test-subj-selector/src/index.test.ts index 8b87ab8f6cd1..1bd2ea05aa7a 100755 --- a/packages/osd-test-subj-selector/__tests__/index.js +++ b/packages/osd-test-subj-selector/src/index.test.ts @@ -28,8 +28,8 @@ * under the License. */ -const testSubjSelector = require('../'); -const expect = require('@osd/expect'); +import { testSubjSelector } from '.'; +import expect from '@osd/expect'; describe('testSubjSelector()', function () { it('converts subjectSelectors to cssSelectors', function () { diff --git a/packages/osd-test-subj-selector/index.js b/packages/osd-test-subj-selector/src/index.ts similarity index 88% rename from packages/osd-test-subj-selector/index.js rename to packages/osd-test-subj-selector/src/index.ts index c61360ed9f91..67d0be3ed946 100755 --- a/packages/osd-test-subj-selector/index.js +++ b/packages/osd-test-subj-selector/src/index.ts @@ -28,7 +28,7 @@ * under the License. */ -function selectorToTerms(selector) { +function selectorToTerms(selector: string) { return selector .replace(/\s*~\s*/g, '~') // css locator with '~' operator cannot contain spaces .replace(/\s*>\s*/g, '>') // remove all whitespace around joins > @@ -36,7 +36,7 @@ function selectorToTerms(selector) { .split(/>+/); } -function termToCssSelector(term) { +function termToCssSelector(term: string) { if (term) { return term.startsWith('~') ? '[data-test-subj~="' + term.substring(1).replace(/\s/g, '') + '"]' @@ -46,15 +46,17 @@ function termToCssSelector(term) { } } -module.exports = function testSubjSelector(selector) { +export function testSubjSelector(selector: string) { const cssSelectors = []; const terms = selectorToTerms(selector); while (terms.length) { const term = terms.shift(); // split each term by joins/& and map to css selectors - cssSelectors.push(term.split('&').map(termToCssSelector).join('')); + if (term) { + cssSelectors.push(term.split('&').map(termToCssSelector).join('')); + } } return cssSelectors.join(' '); -}; +} diff --git a/packages/osd-test-subj-selector/tsconfig.json b/packages/osd-test-subj-selector/tsconfig.json index f47485f218ad..3572ab11c2bd 100644 --- a/packages/osd-test-subj-selector/tsconfig.json +++ b/packages/osd-test-subj-selector/tsconfig.json @@ -1,9 +1,15 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "tsBuildInfoFile": "../../build/tsbuildinfo/packages/osd-test-subj-selector" + "outDir": "./target", + "declaration": true, + "sourceMap": true, + "types": [ + "jest", + "node" + ] }, "include": [ - "index.d.ts" + "src" ] } diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/config.js b/packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/config.js similarity index 93% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/config.js rename to packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/config.js index 11d5731c3fff..2da407401623 100644 --- a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/config.js +++ b/packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/config.js @@ -35,12 +35,12 @@ export default function () { testFiles: [ require.resolve('./tests/before_hook'), require.resolve('./tests/it'), - require.resolve('./tests/after_hook') + require.resolve('./tests/after_hook'), ], services: { hookIntoLIfecycle({ getService }) { const log = getService('log'); - const lifecycle = getService('lifecycle') + const lifecycle = getService('lifecycle'); lifecycle.testFailure.add(async (err, test) => { log.info('testFailure %s %s', err.message, test.fullTitle()); @@ -53,10 +53,10 @@ export default function () { await delay(10); log.info('testHookFailureAfterDelay %s %s', err.message, test.fullTitle()); }); - } + }, }, mochaReporter: { - captureLogOutput: false - } + captureLogOutput: false, + }, }; } diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/after_hook.js b/packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/after_hook.js similarity index 100% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/after_hook.js rename to packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/after_hook.js diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/before_hook.js b/packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/before_hook.js similarity index 100% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/before_hook.js rename to packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/before_hook.js diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/it.js b/packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/it.js similarity index 100% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/failure_hooks/tests/it.js rename to packages/osd-test/src/functional_test_runner/fixtures/failure_hooks/tests/it.js diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/simple_project/config.js b/packages/osd-test/src/functional_test_runner/fixtures/simple_project/config.js similarity index 95% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/simple_project/config.js rename to packages/osd-test/src/functional_test_runner/fixtures/simple_project/config.js index 6eb3f860a6a1..85563b214181 100644 --- a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/simple_project/config.js +++ b/packages/osd-test/src/functional_test_runner/fixtures/simple_project/config.js @@ -31,7 +31,5 @@ import { resolve } from 'path'; export default () => ({ - testFiles: [ - resolve(__dirname, 'tests.js') - ] + testFiles: [resolve(__dirname, 'tests.js')], }); diff --git a/packages/osd-test/src/functional_test_runner/__tests__/fixtures/simple_project/tests.js b/packages/osd-test/src/functional_test_runner/fixtures/simple_project/tests.js similarity index 100% rename from packages/osd-test/src/functional_test_runner/__tests__/fixtures/simple_project/tests.js rename to packages/osd-test/src/functional_test_runner/fixtures/simple_project/tests.js diff --git a/packages/osd-test/src/functional_test_runner/__tests__/integration/basic.js b/packages/osd-test/src/functional_test_runner/integration/basic.test.ts similarity index 95% rename from packages/osd-test/src/functional_test_runner/__tests__/integration/basic.js rename to packages/osd-test/src/functional_test_runner/integration/basic.test.ts index 7f484bfb62a5..e776797334d6 100644 --- a/packages/osd-test/src/functional_test_runner/__tests__/integration/basic.js +++ b/packages/osd-test/src/functional_test_runner/integration/basic.test.ts @@ -38,11 +38,9 @@ const SCRIPT = resolve(REPO_ROOT, 'scripts/functional_test_runner.js'); const BASIC_CONFIG = require.resolve('../fixtures/simple_project/config.js'); describe('basic config file with a single app and test', function () { - this.timeout(60 * 1000); - it('runs and prints expected output', () => { const proc = spawnSync(process.execPath, [SCRIPT, '--config', BASIC_CONFIG]); - const stdout = proc.stdout.toString('utf8'); + const stdout = proc.stdout.toString(); expect(stdout).to.contain('$BEFORE$'); expect(stdout).to.contain('$TESTNAME$'); expect(stdout).to.contain('$INTEST$'); diff --git a/packages/osd-test/src/functional_test_runner/__tests__/integration/failure_hooks.js b/packages/osd-test/src/functional_test_runner/integration/failure_hooks.test.js similarity index 96% rename from packages/osd-test/src/functional_test_runner/__tests__/integration/failure_hooks.js rename to packages/osd-test/src/functional_test_runner/integration/failure_hooks.test.js index d23685a6423d..2047a6c3065f 100644 --- a/packages/osd-test/src/functional_test_runner/__tests__/integration/failure_hooks.js +++ b/packages/osd-test/src/functional_test_runner/integration/failure_hooks.test.js @@ -39,11 +39,9 @@ const SCRIPT = resolve(REPO_ROOT, 'scripts/functional_test_runner.js'); const FAILURE_HOOKS_CONFIG = require.resolve('../fixtures/failure_hooks/config.js'); describe('failure hooks', function () { - this.timeout(60 * 1000); - it('runs and prints expected output', () => { const proc = spawnSync(process.execPath, [SCRIPT, '--config', FAILURE_HOOKS_CONFIG]); - const lines = stripAnsi(proc.stdout.toString('utf8')).split(/\r?\n/); + const lines = stripAnsi(proc.stdout.toString()).split(/\r?\n/); const tests = [ { flag: '$FAILING_BEFORE_HOOK$', diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.1.js b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.1.js similarity index 96% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.1.js rename to packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.1.js index 78add463947d..85e420d8a8ca 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.1.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.1.js @@ -30,8 +30,6 @@ export default function () { return { - testFiles: [ - 'config.1' - ] + testFiles: ['config.1'], }; } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.2.js b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.2.js similarity index 94% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.2.js rename to packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.2.js index b4ba66968687..dcb6d81f52bb 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.2.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.2.js @@ -32,9 +32,6 @@ export default async function ({ readConfigFile }) { const config1 = await readConfigFile(require.resolve('./config.1.js')); return { - testFiles: [ - ...config1.get('testFiles'), - 'config.2' - ] + testFiles: [...config1.get('testFiles'), 'config.2'], }; } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.3.js b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.3.js similarity index 94% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.3.js rename to packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.3.js index 5d13d1c11ab0..7876b50a311f 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.3.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.3.js @@ -31,11 +31,9 @@ export default async function ({ readConfigFile }) { const config4 = await readConfigFile(require.resolve('./config.4')); return { - testFiles: [ - 'baz' - ], + testFiles: ['baz'], screenshots: { - ...config4.get('screenshots') - } + ...config4.get('screenshots'), + }, }; } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.4.js b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.4.js similarity index 97% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.4.js rename to packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.4.js index ae9d5dc0194a..8c969bbb9cf5 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.4.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.4.js @@ -31,7 +31,7 @@ export default function () { return { screenshots: { - directory: 'bar' - } + directory: 'bar', + }, }; } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.invalid.js b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.invalid.js similarity index 98% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.invalid.js rename to packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.invalid.js index 621312c7c716..fb4ea52b9a27 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/fixtures/config.invalid.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/fixtures/config.invalid.js @@ -30,6 +30,6 @@ export default async function () { return { - foo: 'bar' + foo: 'bar', }; } diff --git a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/read_config_file.js b/packages/osd-test/src/functional_test_runner/lib/config/read_config_file.test.ts similarity index 93% rename from packages/osd-test/src/functional_test_runner/lib/config/__tests__/read_config_file.js rename to packages/osd-test/src/functional_test_runner/lib/config/read_config_file.test.ts index ff5980ffcb3d..adaae7d2beba 100644 --- a/packages/osd-test/src/functional_test_runner/lib/config/__tests__/read_config_file.js +++ b/packages/osd-test/src/functional_test_runner/lib/config/read_config_file.test.ts @@ -31,8 +31,8 @@ import expect from '@osd/expect'; import { ToolingLog } from '@osd/dev-utils'; -import { readConfigFile } from '../read_config_file'; -import { Config } from '../config'; +import { readConfigFile } from './read_config_file'; +import { Config } from './config'; const log = new ToolingLog(); @@ -63,7 +63,8 @@ describe('readConfigFile()', () => { await readConfigFile(log, require.resolve('./fixtures/config.invalid')); throw new Error('expected readConfigFile() to fail'); } catch (err) { - expect(err.message).to.match(/"foo"/); + const { message } = err as Error; + expect(message).to.match(/"foo"/); } }); }); diff --git a/src/core/public/fatal_errors/fatal_errors_screen.test.tsx b/src/core/public/fatal_errors/fatal_errors_screen.test.tsx index 22d063656862..7b9e70f4495d 100644 --- a/src/core/public/fatal_errors/fatal_errors_screen.test.tsx +++ b/src/core/public/fatal_errors/fatal_errors_screen.test.tsx @@ -29,7 +29,7 @@ */ import { EuiCallOut } from '@elastic/eui'; -import testSubjSelector from '@osd/test-subj-selector'; +import { testSubjSelector } from '@osd/test-subj-selector'; import React from 'react'; import * as Rx from 'rxjs'; import { mountWithIntl, shallowWithIntl } from 'test_utils/enzyme_helpers'; diff --git a/src/dev/__tests__/file.js b/src/dev/file.test.ts similarity index 91% rename from src/dev/__tests__/file.js rename to src/dev/file.test.ts index 2ed904d53822..164d65b325e0 100644 --- a/src/dev/__tests__/file.js +++ b/src/dev/file.test.ts @@ -32,16 +32,20 @@ import { resolve, sep } from 'path'; import expect from '@osd/expect'; -import { File } from '../file'; +import { File } from './file'; const HERE = resolve(__dirname, __filename); describe('dev/File', () => { describe('constructor', () => { it('throws if path is not a string', () => { + // @ts-ignore to test wrong args expect(() => new File()).to.throwError(); + // @ts-ignore to test wrong args expect(() => new File(1)).to.throwError(); + // @ts-ignore to test wrong args expect(() => new File(false)).to.throwError(); + // @ts-ignore to test wrong args expect(() => new File(null)).to.throwError(); }); }); @@ -49,7 +53,7 @@ describe('dev/File', () => { describe('#getRelativePath()', () => { it('returns the path relative to the repo root', () => { const file = new File(HERE); - expect(file.getRelativePath()).to.eql(['src', 'dev', '__tests__', 'file.js'].join(sep)); + expect(file.getRelativePath()).to.eql(['src', 'dev', 'file.test.ts'].join(sep)); }); }); @@ -80,7 +84,6 @@ describe('dev/File', () => { it('returns the parents of a file, stopping at the repo root, in descending order', () => { const file = new File(HERE); expect(file.getRelativeParentDirs()).to.eql([ - ['src', 'dev', '__tests__'].join(sep), // src/dev/__tests__ ['src', 'dev'].join(sep), // src/dev 'src', ]); diff --git a/src/dev/license_checker/__tests__/valid.js b/src/dev/license_checker/valid.test.ts similarity index 87% rename from src/dev/license_checker/__tests__/valid.js rename to src/dev/license_checker/valid.test.ts index 80caf230b50d..8bbb4a40e53c 100644 --- a/src/dev/license_checker/__tests__/valid.js +++ b/src/dev/license_checker/valid.test.ts @@ -32,9 +32,9 @@ import { resolve } from 'path'; import expect from '@osd/expect'; -import { assertLicensesValid } from '../valid'; +import { assertLicensesValid } from './valid'; -const ROOT = resolve(__dirname, '../../../../'); +const ROOT = resolve(__dirname, '../../../'); const NODE_MODULES = resolve(ROOT, './node_modules'); const PACKAGE = { @@ -62,7 +62,7 @@ describe('tasks/lib/licenses', () => { packages: [PACKAGE], validLicenses: [`not ${PACKAGE.licenses[0]}`], }); - }).to.throwError(PACKAGE.name); + }).to.throwError(() => PACKAGE.name); }); it('throws an error when the package has no licenses', () => { @@ -76,7 +76,7 @@ describe('tasks/lib/licenses', () => { ], validLicenses: [...PACKAGE.licenses], }); - }).to.throwError(PACKAGE.name); + }).to.throwError(() => PACKAGE.name); }); it('includes the relative path to packages in error message', () => { @@ -86,9 +86,10 @@ describe('tasks/lib/licenses', () => { validLicenses: ['none'], }); throw new Error('expected assertLicensesValid() to throw'); - } catch (error) { - expect(error.message).to.contain(PACKAGE.relative); - expect(error.message).to.not.contain(PACKAGE.directory); + } catch (error: unknown) { + const { message } = error as Error; + expect(message).to.contain(PACKAGE.relative); + expect(message).to.not.contain(PACKAGE.directory); } }); }); diff --git a/src/dev/__tests__/node_versions_must_match.js b/src/dev/node_versions_must_match.test.ts similarity index 97% rename from src/dev/__tests__/node_versions_must_match.js rename to src/dev/node_versions_must_match.test.ts index 1008afb50752..7ddaa54b121c 100644 --- a/src/dev/__tests__/node_versions_must_match.js +++ b/src/dev/node_versions_must_match.test.ts @@ -29,7 +29,7 @@ */ import fs from 'fs'; -import { engines } from '../../../package.json'; +import { engines } from '../../package.json'; import { promisify } from 'util'; const readFile = promisify(fs.readFile); import expect from '@osd/expect'; diff --git a/src/legacy/utils/index.d.ts b/src/legacy/utils/index.d.ts deleted file mode 100644 index b2fbbfce78a8..000000000000 --- a/src/legacy/utils/index.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - * - * Any modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -export function unset(object: object, rawPath: string): void; diff --git a/src/legacy/utils/index.js b/src/legacy/utils/index.ts similarity index 100% rename from src/legacy/utils/index.js rename to src/legacy/utils/index.ts diff --git a/src/legacy/utils/__tests__/unset.js b/src/legacy/utils/unset.test.ts similarity index 96% rename from src/legacy/utils/__tests__/unset.js rename to src/legacy/utils/unset.test.ts index 8214800a8cc7..c441bbbaa7fe 100644 --- a/src/legacy/utils/__tests__/unset.js +++ b/src/legacy/utils/unset.test.ts @@ -28,19 +28,21 @@ * under the License. */ -import { unset } from '../unset'; +import { unset } from './unset'; import expect from '@osd/expect'; describe('unset(obj, key)', function () { describe('invalid input', function () { it('should do nothing if not given an object', function () { const obj = 'hello'; + // @ts-ignore bad args unset(obj, 'e'); expect(obj).to.equal('hello'); }); it('should do nothing if not given a key', function () { const obj = { one: 1 }; + // @ts-ignore missing args unset(obj); expect(obj).to.eql({ one: 1 }); }); @@ -53,7 +55,7 @@ describe('unset(obj, key)', function () { }); describe('shallow removal', function () { - let obj; + let obj: {}; beforeEach(function () { obj = { one: 1, two: 2, deep: { three: 3, four: 4 } }; @@ -71,7 +73,7 @@ describe('unset(obj, key)', function () { }); describe('deep removal', function () { - let obj; + let obj: {}; beforeEach(function () { obj = { one: 1, two: 2, deep: { three: 3, four: 4 } }; diff --git a/src/legacy/utils/unset.js b/src/legacy/utils/unset.ts similarity index 82% rename from src/legacy/utils/unset.js rename to src/legacy/utils/unset.ts index 19b508838eb8..cb95e0899764 100644 --- a/src/legacy/utils/unset.js +++ b/src/legacy/utils/unset.ts @@ -30,7 +30,7 @@ import _ from 'lodash'; -export function unset(object, rawPath) { +export function unset(object: Record, rawPath: string | string[]) { if (!object) return; const path = _.toPath(rawPath); @@ -39,16 +39,18 @@ export function unset(object, rawPath) { return; case 1: - delete object[rawPath]; + delete object[path[0]]; break; default: const leaf = path.pop(); const parentPath = path.slice(); - const parent = _.get(object, parentPath); - unset(parent, leaf); - if (!_.size(parent)) { - unset(object, parentPath); + if (leaf && parentPath) { + const parent = _.get(object, parentPath); + unset(parent, leaf); + if (!_.size(parent)) { + unset(object, parentPath); + } } break; } diff --git a/src/legacy/utils/version.test.ts b/src/legacy/utils/version.test.ts new file mode 100644 index 000000000000..b7b0b3fb7e71 --- /dev/null +++ b/src/legacy/utils/version.test.ts @@ -0,0 +1,36 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { versionSatisfies, cleanVersion } from './version'; +import expect from '@osd/expect'; + +describe('versionSatisfies', function () { + it('returns false if versions are not strings', () => { + // @ts-ignore to test wrong args + expect(versionSatisfies('a')).to.eql(false); + // @ts-ignore to test wrong args + expect(versionSatisfies('a', 1.2)).to.eql(false); + // @ts-ignore to test wrong args + expect(versionSatisfies(1.2, 'a')).to.eql(false); + // @ts-ignore to test wrong args + expect(versionSatisfies(1.2, '1.2')).to.eql(false); + }); + + it('returns true if versions are exact match', () => { + expect(versionSatisfies('3.14.159', '3.14.159')).to.eql(true); + }); +}); + +describe('cleanVersion', function () { + it('should not alter non-matching strings', () => { + const version = 'hello world'; + expect(cleanVersion(version)).to.eql(version); + }); + + it('should return the first major.minor.patch version', function () { + const version = 'hello world 1.12.35.7 7.12.19'; + expect(cleanVersion(version)).to.eql('1.12.35'); + }); +}); diff --git a/src/legacy/utils/version.js b/src/legacy/utils/version.ts similarity index 91% rename from src/legacy/utils/version.js rename to src/legacy/utils/version.ts index 777b3b7d16f0..c9b06a775113 100644 --- a/src/legacy/utils/version.js +++ b/src/legacy/utils/version.ts @@ -28,7 +28,7 @@ * under the License. */ -export function versionSatisfies(cleanActual, cleanExpected) { +export function versionSatisfies(cleanActual: string, cleanExpected: string) { try { return cleanActual === cleanExpected; } catch (err) { @@ -36,7 +36,7 @@ export function versionSatisfies(cleanActual, cleanExpected) { } } -export function cleanVersion(version) { +export function cleanVersion(version: string) { const match = version.match(/\d+\.\d+\.\d+/); if (!match) return version; return match[0]; diff --git a/src/plugins/console/server/__tests__/opensearch_proxy_config.js b/src/plugins/console/server/lib/opensearch_proxy_config.test.ts similarity index 70% rename from src/plugins/console/server/__tests__/opensearch_proxy_config.js rename to src/plugins/console/server/lib/opensearch_proxy_config.test.ts index 7861412faad8..3c79941a2a78 100644 --- a/src/plugins/console/server/__tests__/opensearch_proxy_config.js +++ b/src/plugins/console/server/lib/opensearch_proxy_config.test.ts @@ -30,15 +30,18 @@ import expect from '@osd/expect'; import moment from 'moment'; -import { getOpenSearchProxyConfig } from '../lib/opensearch_proxy_config'; +import { getOpenSearchProxyConfig } from './opensearch_proxy_config'; import https from 'https'; import http from 'http'; +import { PeerCertificate } from 'tls'; const getDefaultOpenSearchConfig = () => { return { hosts: ['http://localhost:9200', 'http://192.168.1.1:1234'], requestTimeout: moment.duration(30000), - ssl: { verificationMode: 'full' }, + ssl: { verificationMode: 'full' as const, alwaysPresentCertificate: true }, + requestHeadersWhitelist: [], + customHeaders: {}, }; }; @@ -67,20 +70,20 @@ describe('plugins/console', function () { }); describe('ssl', function () { - let config; - beforeEach(function () { - config = { - ...getDefaultOpenSearchConfig(), - hosts: ['https://localhost:9200'], - }; - }); + const config = { + ...getDefaultOpenSearchConfig(), + hosts: ['https://localhost:9200'], + }; it('sets rejectUnauthorized to false when verificationMode is none', function () { const { agent } = getOpenSearchProxyConfig({ ...config, ssl: { ...config.ssl, verificationMode: 'none' }, }); - expect(agent.options.rejectUnauthorized).to.be(false); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.rejectUnauthorized).to.be(false); + } }); it('sets rejectUnauthorized to true when verificationMode is certificate', function () { @@ -88,7 +91,10 @@ describe('plugins/console', function () { ...config, ssl: { ...config.ssl, verificationMode: 'certificate' }, }); - expect(agent.options.rejectUnauthorized).to.be(true); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.rejectUnauthorized).to.be(true); + } }); it('sets checkServerIdentity to not check hostname when verificationMode is certificate', function () { @@ -103,11 +109,14 @@ describe('plugins/console', function () { }, }; - expect(agent.options.checkServerIdentity) - .withArgs('right.com', cert) - .to.not.throwException(); - const result = agent.options.checkServerIdentity('right.com', cert); - expect(result).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.checkServerIdentity) + .withArgs('right.com', cert) + .to.not.throwException(); + const result = agent.options.checkServerIdentity?.('right.com', cert as PeerCertificate); + expect(result).to.be(undefined); + } }); it('sets rejectUnauthorized to true when verificationMode is full', function () { @@ -116,7 +125,10 @@ describe('plugins/console', function () { ssl: { ...config.ssl, verificationMode: 'full' }, }); - expect(agent.options.rejectUnauthorized).to.be(true); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.rejectUnauthorized).to.be(true); + } }); it(`doesn't set checkServerIdentity when verificationMode is full`, function () { @@ -125,7 +137,10 @@ describe('plugins/console', function () { ssl: { ...config.ssl, verificationMode: 'full' }, }); - expect(agent.options.checkServerIdentity).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.checkServerIdentity).to.be(undefined); + } }); it(`sets ca when certificateAuthorities are specified`, function () { @@ -134,7 +149,10 @@ describe('plugins/console', function () { ssl: { ...config.ssl, certificateAuthorities: ['content-of-some-path'] }, }); - expect(agent.options.ca).to.contain('content-of-some-path'); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.ca).to.contain('content-of-some-path'); + } }); describe('when alwaysPresentCertificate is false', () => { @@ -149,8 +167,11 @@ describe('plugins/console', function () { }, }); - expect(agent.options.cert).to.be(undefined); - expect(agent.options.key).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.cert).to.be(undefined); + expect(agent.options.key).to.be(undefined); + } }); it(`doesn't set passphrase when certificate, key and keyPassphrase are specified`, function () { @@ -165,7 +186,10 @@ describe('plugins/console', function () { }, }); - expect(agent.options.passphrase).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.passphrase).to.be(undefined); + } }); }); @@ -181,8 +205,11 @@ describe('plugins/console', function () { }, }); - expect(agent.options.cert).to.be('content-of-some-path'); - expect(agent.options.key).to.be('content-of-another-path'); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.cert).to.be('content-of-some-path'); + expect(agent.options.key).to.be('content-of-another-path'); + } }); it(`sets passphrase when certificate, key and keyPassphrase are specified`, function () { @@ -197,7 +224,10 @@ describe('plugins/console', function () { }, }); - expect(agent.options.passphrase).to.be('secret'); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.passphrase).to.be('secret'); + } }); it(`doesn't set cert when only certificate path is specified`, async function () { @@ -211,8 +241,11 @@ describe('plugins/console', function () { }, }); - expect(agent.options.cert).to.be(undefined); - expect(agent.options.key).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.cert).to.be(undefined); + expect(agent.options.key).to.be(undefined); + } }); it(`doesn't set key when only key path is specified`, async function () { @@ -226,8 +259,11 @@ describe('plugins/console', function () { }, }); - expect(agent.options.cert).to.be(undefined); - expect(agent.options.key).to.be(undefined); + expect(agent).to.be.a(https.Agent); + if (agent instanceof https.Agent) { + expect(agent.options.cert).to.be(undefined); + expect(agent.options.key).to.be(undefined); + } }); }); }); diff --git a/src/plugins/console/server/lib/opensearch_proxy_config.ts b/src/plugins/console/server/lib/opensearch_proxy_config.ts index 1f733ce978ea..b2b108b20e90 100644 --- a/src/plugins/console/server/lib/opensearch_proxy_config.ts +++ b/src/plugins/console/server/lib/opensearch_proxy_config.ts @@ -35,7 +35,7 @@ import url from 'url'; import { OpenSearchConfigForProxy } from '../types'; -const createAgent = (legacyConfig: OpenSearchConfigForProxy) => { +const createAgent = (legacyConfig: OpenSearchConfigForProxy): http.Agent | https.Agent => { const target = url.parse(_.head(legacyConfig.hosts) as any); if (!/^https/.test(target.protocol || '')) return new http.Agent(); diff --git a/src/plugins/console/server/__tests__/proxy_config.js b/src/plugins/console/server/lib/proxy_config.test.ts similarity index 64% rename from src/plugins/console/server/__tests__/proxy_config.js rename to src/plugins/console/server/lib/proxy_config.test.ts index 9f0b9bbd87d9..05b60651d4fb 100644 --- a/src/plugins/console/server/__tests__/proxy_config.js +++ b/src/plugins/console/server/lib/proxy_config.test.ts @@ -33,38 +33,44 @@ import expect from '@osd/expect'; import sinon from 'sinon'; import https, { Agent as HttpsAgent } from 'https'; -import { parse as parseUrl } from 'url'; -import { ProxyConfig } from '../lib/proxy_config'; +import { ProxyConfig } from './proxy_config'; const matchGoogle = { protocol: 'https', host: 'google.com', path: '/search', }; -const parsedGoogle = parseUrl('https://google.com/search'); -const parsedLocalOpenSearch = parseUrl('https://localhost:5601/search'); +const parsedGoogle = new URL('https://google.com/search'); +const parsedLocalOpenSearch = new URL('https://localhost:5601/search'); +const defaultConfig = { + timeout: 100, +}; describe('ProxyConfig', function () { describe('constructor', function () { + let agentStub: sinon.SinonStub; + beforeEach(function () { - sinon.stub(https, 'Agent'); + agentStub = sinon.stub(https, 'Agent'); }); afterEach(function () { - https.Agent.restore(); + agentStub.restore(); }); it('uses ca to create sslAgent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { ca: ['content-of-some-path'], }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.a(https.Agent); - sinon.assert.calledOnce(https.Agent); - const sslAgentOpts = https.Agent.firstCall.args[0]; + sinon.assert.calledOnce(agentStub); + const sslAgentOpts = agentStub.firstCall.args[0]; expect(sslAgentOpts).to.eql({ ca: ['content-of-some-path'], cert: undefined, @@ -75,15 +81,17 @@ describe('ProxyConfig', function () { it('uses cert, and key to create sslAgent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { cert: 'content-of-some-path', key: 'content-of-another-path', }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.a(https.Agent); - sinon.assert.calledOnce(https.Agent); - const sslAgentOpts = https.Agent.firstCall.args[0]; + sinon.assert.calledOnce(agentStub); + const sslAgentOpts = agentStub.firstCall.args[0]; expect(sslAgentOpts).to.eql({ ca: undefined, cert: 'content-of-some-path', @@ -94,17 +102,18 @@ describe('ProxyConfig', function () { it('uses ca, cert, and key to create sslAgent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { ca: ['content-of-some-path'], cert: 'content-of-another-path', key: 'content-of-yet-another-path', - rejectUnauthorized: true, }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.a(https.Agent); - sinon.assert.calledOnce(https.Agent); - const sslAgentOpts = https.Agent.firstCall.args[0]; + sinon.assert.calledOnce(agentStub); + const sslAgentOpts = agentStub.firstCall.args[0]; expect(sslAgentOpts).to.eql({ ca: ['content-of-some-path'], cert: 'content-of-another-path', @@ -128,75 +137,101 @@ describe('ProxyConfig', function () { describe('parsed url does match', function () { it('assigns timeout value', function () { - const football = {}; + const timeValue = 200; const config = new ProxyConfig({ match: matchGoogle, - timeout: football, + timeout: timeValue, }); - expect(config.getForParsedUri(parsedGoogle).timeout).to.be(football); + expect(config.getForParsedUri(parsedGoogle).timeout).to.be(timeValue); }); it('assigns ssl.verify to rejectUnauthorized', function () { - const football = {}; - const config = new ProxyConfig({ + const configWithVerification = new ProxyConfig({ + ...defaultConfig, match: matchGoogle, ssl: { - verify: football, + verify: true, + }, + }); + const configWithoutVerification = new ProxyConfig({ + ...defaultConfig, + match: matchGoogle, + ssl: { + verify: false, }, }); - expect(config.getForParsedUri(parsedGoogle).rejectUnauthorized).to.be(football); + expect(configWithVerification.getForParsedUri(parsedGoogle).rejectUnauthorized).to.be(true); + expect(configWithoutVerification.getForParsedUri(parsedGoogle).rejectUnauthorized).to.be( + false + ); }); describe('uri us http', function () { describe('ca is set', function () { it('creates but does not output the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { ca: ['path/to/ca'], }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'http:' }).agent).to.be(undefined); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'http:' }).agent).to.be( + undefined + ); }); }); describe('cert is set', function () { it('creates but does not output the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { cert: 'path/to/cert', }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'http:' }).agent).to.be(undefined); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'http:' }).agent).to.be( + undefined + ); }); }); describe('key is set', function () { it('creates but does not output the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { key: 'path/to/key', }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'http:' }).agent).to.be(undefined); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'http:' }).agent).to.be( + undefined + ); }); }); describe('cert + key are set', function () { it('creates but does not output the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { cert: 'path/to/cert', key: 'path/to/key', }, }); + // @ts-ignore private prop access expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'http:' }).agent).to.be(undefined); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'http:' }).agent).to.be( + undefined + ); }); }); }); @@ -205,50 +240,70 @@ describe('ProxyConfig', function () { describe('ca is set', function () { it('creates and outputs the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { ca: ['path/to/ca'], }, }); - expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'https:' }).agent).to.be(config.sslAgent); + // @ts-ignore private prop access + const agent = config.sslAgent; + expect(agent).to.be.an(HttpsAgent); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'https:' }).agent).to.be( + agent + ); }); }); describe('cert is set', function () { it('creates and outputs the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { cert: 'path/to/cert', }, }); - expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'https:' }).agent).to.be(config.sslAgent); + // @ts-ignore private prop access + const agent = config.sslAgent; + expect(agent).to.be.an(HttpsAgent); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'https:' }).agent).to.be( + agent + ); }); }); describe('key is set', function () { it('creates and outputs the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { key: 'path/to/key', }, }); - expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'https:' }).agent).to.be(config.sslAgent); + // @ts-ignore private prop access + const agent = config.sslAgent; + expect(agent).to.be.an(HttpsAgent); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'https:' }).agent).to.be( + agent + ); }); }); describe('cert + key are set', function () { it('creates and outputs the agent', function () { const config = new ProxyConfig({ + ...defaultConfig, ssl: { cert: 'path/to/cert', key: 'path/to/key', }, }); - expect(config.sslAgent).to.be.an(HttpsAgent); - expect(config.getForParsedUri({ protocol: 'https:' }).agent).to.be(config.sslAgent); + // @ts-ignore private prop access + const agent = config.sslAgent; + expect(agent).to.be.an(HttpsAgent); + expect(config.getForParsedUri({ ...parsedGoogle, protocol: 'https:' }).agent).to.be( + agent + ); }); }); }); diff --git a/src/plugins/console/server/lib/proxy_config.ts b/src/plugins/console/server/lib/proxy_config.ts index 311046d398fb..c762462f6de2 100644 --- a/src/plugins/console/server/lib/proxy_config.ts +++ b/src/plugins/console/server/lib/proxy_config.ts @@ -33,6 +33,7 @@ import { format as formatUrl } from 'url'; import { Agent as HttpsAgent, AgentOptions } from 'https'; import { WildcardMatcher } from './wildcard_matcher'; +import { ProxyConfigs } from '../types'; export class ProxyConfig { // @ts-ignore @@ -50,7 +51,7 @@ export class ProxyConfig { private verifySsl: any; - constructor(config: { match: any; timeout: number }) { + constructor(config: ProxyConfigs) { config = { ...config, }; @@ -83,7 +84,7 @@ export class ProxyConfig { this.sslAgent = this._makeSslAgent(config); } - _makeSslAgent(config: any) { + _makeSslAgent(config: ProxyConfigs) { const ssl = config.ssl || {}; this.verifySsl = ssl.verify; diff --git a/src/plugins/console/server/__tests__/proxy_config_collection.js b/src/plugins/console/server/lib/proxy_config_collection.test.ts similarity index 93% rename from src/plugins/console/server/__tests__/proxy_config_collection.js rename to src/plugins/console/server/lib/proxy_config_collection.test.ts index c102d525b749..61d32b587c3d 100644 --- a/src/plugins/console/server/__tests__/proxy_config_collection.js +++ b/src/plugins/console/server/lib/proxy_config_collection.test.ts @@ -28,24 +28,14 @@ * under the License. */ -/* eslint-env mocha */ +/* eslint-env jest */ import expect from '@osd/expect'; -import sinon from 'sinon'; -import fs from 'fs'; import { Agent as HttpsAgent } from 'https'; -import { ProxyConfigCollection } from '../lib/proxy_config_collection'; +import { ProxyConfigCollection } from './proxy_config_collection'; describe('ProxyConfigCollection', function () { - beforeEach(function () { - sinon.stub(fs, 'readFileSync').callsFake(() => Buffer.alloc(0)); - }); - - afterEach(function () { - fs.readFileSync.restore(); - }); - const proxyConfigs = [ { match: { @@ -92,7 +82,7 @@ describe('ProxyConfigCollection', function () { }, ]; - function getTimeout(uri) { + function getTimeout(uri: string) { const collection = new ProxyConfigCollection(proxyConfigs); return collection.configForUri(uri).timeout; } @@ -162,10 +152,12 @@ describe('ProxyConfigCollection', function () { return new ProxyConfigCollection([ { match: { host: '*.internal.org' }, + timeout: 100, ssl: { ca: ['path/to/ca'] }, }, { match: { host: '*' }, + timeout: 200, ssl: { verify: false }, }, ]); diff --git a/src/plugins/console/server/lib/proxy_config_collection.ts b/src/plugins/console/server/lib/proxy_config_collection.ts index f288562fb19d..c222f0d9b035 100644 --- a/src/plugins/console/server/lib/proxy_config_collection.ts +++ b/src/plugins/console/server/lib/proxy_config_collection.ts @@ -29,14 +29,14 @@ */ import { defaultsDeep } from 'lodash'; -import { parse as parseUrl } from 'url'; +import { ProxyConfigs } from '../types'; import { ProxyConfig } from './proxy_config'; export class ProxyConfigCollection { private configs: ProxyConfig[]; - constructor(configs: Array<{ match: any; timeout: number }> = []) { + constructor(configs: ProxyConfigs[] = []) { this.configs = configs.map((settings) => new ProxyConfig(settings)); } @@ -44,8 +44,8 @@ export class ProxyConfigCollection { return Boolean(this.configs.length); } - configForUri(uri: string): object { - const parsedUri = parseUrl(uri); + configForUri(uri: string) { + const parsedUri = new URL(uri); const settings = this.configs.map((config) => config.getForParsedUri(parsedUri as any)); return defaultsDeep({}, ...settings); } diff --git a/src/plugins/console/server/lib/proxy_request.test.ts b/src/plugins/console/server/lib/proxy_request.test.ts index 36a5eb6ac8f0..8b2d05eac6fe 100644 --- a/src/plugins/console/server/lib/proxy_request.test.ts +++ b/src/plugins/console/server/lib/proxy_request.test.ts @@ -68,7 +68,8 @@ describe(`Console's send request`, () => { }); fail('Should not reach here!'); } catch (e) { - expect(e.message).toEqual('Client request timeout'); + const { message } = e as Error; + expect(message).toEqual('Client request timeout'); expect((fakeRequest.abort as sinon.SinonStub).calledOnce).toBe(true); } }); diff --git a/src/plugins/console/server/lib/proxy_request.ts b/src/plugins/console/server/lib/proxy_request.ts index e78263571c37..48cd1638e0d6 100644 --- a/src/plugins/console/server/lib/proxy_request.ts +++ b/src/plugins/console/server/lib/proxy_request.ts @@ -121,7 +121,7 @@ export const proxyRequest = ({ if (!resolved) { timeoutReject(Boom.gatewayTimeout('Client request timeout')); } else { - timeoutResolve(); + timeoutResolve('Success'); } }, timeout); }); diff --git a/src/plugins/console/server/__tests__/set_headers.js b/src/plugins/console/server/lib/set_headers.test.ts similarity index 96% rename from src/plugins/console/server/__tests__/set_headers.js rename to src/plugins/console/server/lib/set_headers.test.ts index b4bb3ad46592..0bb894034ee0 100644 --- a/src/plugins/console/server/__tests__/set_headers.js +++ b/src/plugins/console/server/lib/set_headers.test.ts @@ -29,15 +29,17 @@ */ import expect from '@osd/expect'; -import { setHeaders } from '../lib'; +import { setHeaders } from '.'; describe('#set_headers', function () { it('throws if not given an object as the first argument', function () { + // @ts-ignore bad args const fn = () => setHeaders(null, {}); expect(fn).to.throwError(); }); it('throws if not given an object as the second argument', function () { + // @ts-ignore bad args const fn = () => setHeaders({}, null); expect(fn).to.throwError(); }); diff --git a/src/plugins/console/server/__tests__/wildcard_matcher.js b/src/plugins/console/server/lib/wildcard_matcher.test.ts similarity index 83% rename from src/plugins/console/server/__tests__/wildcard_matcher.js rename to src/plugins/console/server/lib/wildcard_matcher.test.ts index 266d16acf1f5..fce8dc77d06c 100644 --- a/src/plugins/console/server/__tests__/wildcard_matcher.js +++ b/src/plugins/console/server/lib/wildcard_matcher.test.ts @@ -28,18 +28,18 @@ * under the License. */ -/* eslint-env mocha */ -import { WildcardMatcher } from '../lib/wildcard_matcher'; +/* eslint-env jest */ +import { WildcardMatcher } from './wildcard_matcher'; -function should(candidate, ...constructorArgs) { - if (!new WildcardMatcher(...constructorArgs).match(candidate)) { - throw new Error(`Expected pattern ${[...constructorArgs]} to match ${candidate}`); +function should(candidate: string, pattern: string, ...emptyVal: string[]) { + if (!new WildcardMatcher(pattern, ...emptyVal).match(candidate)) { + throw new Error(`Expected pattern ${[pattern, ...emptyVal]} to match ${candidate}`); } } -function shouldNot(candidate, ...constructorArgs) { - if (new WildcardMatcher(...constructorArgs).match(candidate)) { - throw new Error(`Expected pattern ${[...constructorArgs]} to not match ${candidate}`); +function shouldNot(candidate: string, pattern: string, ...emptyVal: string[]) { + if (new WildcardMatcher(pattern, ...emptyVal).match(candidate)) { + throw new Error(`Expected pattern ${[pattern, ...emptyVal]} to not match ${candidate}`); } } diff --git a/src/plugins/console/server/types.ts b/src/plugins/console/server/types.ts index 57dca126bde2..3029d3fc767e 100644 --- a/src/plugins/console/server/types.ts +++ b/src/plugins/console/server/types.ts @@ -29,6 +29,7 @@ */ import { Duration } from 'moment'; +import { SecureContextOptions } from 'tls'; import { ConsoleServerPlugin } from './plugin'; /** @public */ @@ -56,3 +57,13 @@ export interface OpenSearchConfigForProxy { keyPassphrase?: string; }; } + +export interface SslConfigs extends SecureContextOptions { + verify?: boolean; +} + +export interface ProxyConfigs extends SecureContextOptions { + match?: any; + timeout: number; + ssl?: SslConfigs; +} diff --git a/src/test_utils/__tests__/get_url.js b/src/test_utils/get_url.test.ts similarity index 98% rename from src/test_utils/__tests__/get_url.js rename to src/test_utils/get_url.test.ts index 804df3528d46..c784c9c60b95 100644 --- a/src/test_utils/__tests__/get_url.js +++ b/src/test_utils/get_url.test.ts @@ -29,7 +29,7 @@ */ import expect from '@osd/expect'; -import getUrl from '../get_url'; +import getUrl from './get_url'; describe('getUrl', function () { it('should convert to a url', function () { diff --git a/test/functional/services/common/test_subjects.ts b/test/functional/services/common/test_subjects.ts index 09b8df58024d..2c96b61c66da 100644 --- a/test/functional/services/common/test_subjects.ts +++ b/test/functional/services/common/test_subjects.ts @@ -28,7 +28,7 @@ * under the License. */ -import testSubjSelector from '@osd/test-subj-selector'; +import { testSubjSelector } from '@osd/test-subj-selector'; import { map as mapAsync } from 'bluebird'; import { ProvidedType } from '@osd/test/types/ftr'; import { WebElementWrapper } from '../lib/web_element_wrapper'; diff --git a/test/functional/services/lib/web_element_wrapper/web_element_wrapper.ts b/test/functional/services/lib/web_element_wrapper/web_element_wrapper.ts index cc6c0a9eb94d..fa273b318a24 100644 --- a/test/functional/services/lib/web_element_wrapper/web_element_wrapper.ts +++ b/test/functional/services/lib/web_element_wrapper/web_element_wrapper.ts @@ -33,7 +33,7 @@ import { WebElement, WebDriver, By, Key } from 'selenium-webdriver'; import { PNG } from 'pngjs'; // @ts-ignore not supported yet import cheerio from 'cheerio'; -import testSubjSelector from '@osd/test-subj-selector'; +import { testSubjSelector } from '@osd/test-subj-selector'; import { ToolingLog } from '@osd/dev-utils'; import { CustomCheerio, CustomCheerioStatic } from './custom_cheerio_api'; // @ts-ignore not supported yet @@ -131,15 +131,16 @@ export class WebElementWrapper { try { return await fn(this); } catch (err) { + const { name, message } = err as Error; if ( - !RETRY_CLICK_RETRY_ON_ERRORS.includes(err.name) || + !RETRY_CLICK_RETRY_ON_ERRORS.includes(name) || this.locator === null || attemptsRemaining === 0 ) { throw err; } - this.logger.warning(`WebElementWrapper.${fn.name}: ${err.message}`); + this.logger.warning(`WebElementWrapper.${fn.name}: ${message}`); this.logger.debug( `finding element '${this.locator.toString()}' again, ${attemptsRemaining - 1} attempts left` ); diff --git a/test/visual_regression/services/visual_testing/visual_testing.ts b/test/visual_regression/services/visual_testing/visual_testing.ts index b835130e11f7..36851ba41a02 100644 --- a/test/visual_regression/services/visual_testing/visual_testing.ts +++ b/test/visual_regression/services/visual_testing/visual_testing.ts @@ -31,7 +31,7 @@ import { postSnapshot } from '@percy/sdk-utils'; import { Test } from 'mocha'; -import testSubjSelector from '@osd/test-subj-selector'; +import { testSubjSelector } from '@osd/test-subj-selector'; import { pkg } from '../../../../src/core/server/utils'; import { FtrProviderContext } from '../../ftr_provider_context';