Skip to content

Commit

Permalink
[Tests] Migrate mocha tests to jest (opensearch-project#1553) (opense…
Browse files Browse the repository at this point in the history
…arch-project#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 opensearch-project#1572)
Jest: 11155 passing tests (11155 = 10741 previous jest tests + 4 newly added jest test + 410 migrated mocha tests)

Resolves opensearch-project#215

Signed-off-by: Josh Romero <rmerqg@amazon.com>
(cherry picked from commit 0e41477)
  • Loading branch information
opensearch-trigger-bot[bot] authored and Chang Liu committed Jun 30, 2022
1 parent 04183b7 commit a99a731
Show file tree
Hide file tree
Showing 64 changed files with 401 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
}
Expand All @@ -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 () {
Expand All @@ -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();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -54,37 +54,37 @@ 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/**/*',
},
],
},
],
},
{
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/**/*',
},
],
},
],
},
{
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,
Expand All @@ -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/**/*',
},
],
},
Expand Down Expand Up @@ -153,31 +153,34 @@ 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/**/*',
},
],
},
],
},
{
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*',
],
},
],
},
Expand All @@ -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,
},
Expand All @@ -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/**/*',
},
],
},
Expand All @@ -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/**/*',
},
],
},
Expand All @@ -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',
},
],
},
Expand All @@ -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,
Expand All @@ -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/**/*',
Expand All @@ -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/**/*',
},
],
},
Expand All @@ -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/**/*',
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
],
Expand All @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading

0 comments on commit a99a731

Please sign in to comment.