Skip to content

Commit

Permalink
Revert jest tests config to use babel (opensearch-project#1319)
Browse files Browse the repository at this point in the history
* update jest config to use babel instead of ts-jest

Signed-off-by: Shenoy Pratik <[email protected]>

* remove ts-jest package

Signed-off-by: Shenoy Pratik <[email protected]>

* update yarn lock

Signed-off-by: Shenoy Pratik <[email protected]>

---------

Signed-off-by: Shenoy Pratik <[email protected]>
  • Loading branch information
ps48 authored Dec 21, 2023
1 parent 1110e0c commit 172faa2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 195 deletions.
18 changes: 0 additions & 18 deletions .babelrc

This file was deleted.

26 changes: 26 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// babelrc doesn't respect NODE_PATH anymore but using require does.
// Alternative to install them locally in node_modules
module.exports = function (api) {
// ensure env is test so that this config won't impact build or dev server
if (api.env('test')) {
return {
presets: [
require('@babel/preset-env'),
require('@babel/preset-react'),
require('@babel/preset-typescript'),
],
plugins: [
[require('@babel/plugin-transform-runtime'), { regenerator: true }],
require('@babel/plugin-transform-class-properties'),
require('@babel/plugin-transform-object-rest-spread'),
[require('@babel/plugin-transform-modules-commonjs'), { allowTopLevelThis: true }],
],
};
}
return {};
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
"eslint": "^6.8.0",
"husky": "^8.0.3",
"jest-dom": "^4.0.0",
"lint-staged": "^13.1.0",
"ts-jest": "^29.1.0"
"lint-staged": "^13.1.0"
},
"resolutions": {
"react-syntax-highlighter": "^15.4.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpResponse, HttpSetup } from '../../../../../../../src/core/public';
import { coreStartMock } from '../../../../../test/__mocks__/coreMocks';
import {
checkDataSourceName,
doTypeValidation,
doExistingDataSourceValidation,
doNestedPropertyValidation,
doPropertyValidation,
doTypeValidation,
fetchDataSourceMappings,
fetchIntegrationMappings,
doExistingDataSourceValidation,
} from '../create_integration_helpers';
import * as create_integration_helpers from '../create_integration_helpers';
import { HttpSetup } from '../../../../../../../src/core/public';

describe('doTypeValidation', () => {
it('should return true if required type is not specified', () => {
Expand Down Expand Up @@ -261,73 +261,88 @@ describe('fetchIntegrationMappings', () => {

describe('doExistingDataSourceValidation', () => {
it('Catches and returns checkDataSourceName errors', async () => {
const mockHttp = {} as Partial<HttpSetup>;
jest
.spyOn(create_integration_helpers, 'checkDataSourceName')
.mockReturnValue({ ok: false, errors: ['mock'] });

const result = doExistingDataSourceValidation('target', 'name', 'type', mockHttp as HttpSetup);

await expect(result).resolves.toHaveProperty('ok', false);
coreStartMock.http.post = jest.fn(() =>
Promise.resolve(({
logs: { mappings: { properties: { test: true } } },
} as unknown) as HttpResponse)
);
coreStartMock.http.get = jest.fn(() =>
Promise.resolve(({
data: { mappings: { logs: { template: { mappings: { properties: { test: true } } } } } },
statusCode: 200,
} as unknown) as HttpResponse)
);

const result = await doExistingDataSourceValidation('ss4o_metrics-test-test', 'target', 'logs');

expect(result).toHaveProperty('ok', false);
});

it('Catches data stream fetch errors', async () => {
const mockHttp = {} as Partial<HttpSetup>;
jest.spyOn(create_integration_helpers, 'checkDataSourceName').mockReturnValue({ ok: true });
jest.spyOn(create_integration_helpers, 'fetchDataSourceMappings').mockResolvedValue(null);
jest
.spyOn(create_integration_helpers, 'fetchIntegrationMappings')
.mockResolvedValue({ test: { template: { mappings: {} } } });

const result = doExistingDataSourceValidation('target', 'name', 'type', mockHttp as HttpSetup);

await expect(result).resolves.toHaveProperty('ok', false);
coreStartMock.http.post = jest.fn(() =>
Promise.resolve(({
mock: 'resp',
} as unknown) as HttpResponse)
);

coreStartMock.http.get = jest.fn(() =>
Promise.resolve(({
data: { mappings: { logs: { template: { mappings: { properties: { test: true } } } } } },
statusCode: 200,
} as unknown) as HttpResponse)
);

const result = await doExistingDataSourceValidation('ss4o_logs-test-test', 'target', 'logs');
expect(result).toHaveProperty('ok', false);
});

it('Catches integration fetch errors', async () => {
const mockHttp = {} as Partial<HttpSetup>;
jest.spyOn(create_integration_helpers, 'checkDataSourceName').mockReturnValue({ ok: true });
jest
.spyOn(create_integration_helpers, 'fetchDataSourceMappings')
.mockResolvedValue({ test: { properties: {} } });
jest.spyOn(create_integration_helpers, 'fetchIntegrationMappings').mockResolvedValue(null);

const result = doExistingDataSourceValidation('target', 'name', 'type', mockHttp as HttpSetup);

await expect(result).resolves.toHaveProperty('ok', false);
coreStartMock.http.post = jest.fn(() =>
Promise.resolve(({
logs: { mappings: { properties: { test: true } } },
} as unknown) as HttpResponse)
);
coreStartMock.http.get = jest.fn(() =>
Promise.resolve(({
mock: 'resp',
} as unknown) as HttpResponse)
);

const result = await doExistingDataSourceValidation('ss4o_logs-test-test', 'target', 'logs');
expect(result).toHaveProperty('ok', false);
});

it('Catches type validation issues', async () => {
const mockHttp = {} as Partial<HttpSetup>;
jest.spyOn(create_integration_helpers, 'checkDataSourceName').mockReturnValue({ ok: true });
jest
.spyOn(create_integration_helpers, 'fetchDataSourceMappings')
.mockResolvedValue({ test: { properties: {} } });
jest
.spyOn(create_integration_helpers, 'fetchIntegrationMappings')
.mockResolvedValue({ test: { template: { mappings: {} } } });
jest
.spyOn(create_integration_helpers, 'doPropertyValidation')
.mockReturnValue({ ok: false, errors: ['mock'] });

const result = doExistingDataSourceValidation('target', 'name', 'type', mockHttp as HttpSetup);

await expect(result).resolves.toHaveProperty('ok', false);
coreStartMock.http.post = jest.fn(() =>
Promise.resolve(({
logs: { mappings: { properties: { test: true } } },
} as unknown) as HttpResponse)
);
coreStartMock.http.get = jest.fn(() =>
Promise.resolve(({
data: { mappings: { template: { mappings: { properties: { test: true } } } } },
statusCode: 200,
} as unknown) as HttpResponse)
);

const result = await doExistingDataSourceValidation('ss4o_logs-test-test', 'target', 'logs');
expect(result).toHaveProperty('ok', false);
});

it('Returns no errors if everything passes', async () => {
const mockHttp = {} as Partial<HttpSetup>;
jest.spyOn(create_integration_helpers, 'checkDataSourceName').mockReturnValue({ ok: true });
jest
.spyOn(create_integration_helpers, 'fetchDataSourceMappings')
.mockResolvedValue({ test: { properties: {} } });
jest
.spyOn(create_integration_helpers, 'fetchIntegrationMappings')
.mockResolvedValue({ test: { template: { mappings: {} } } });
jest.spyOn(create_integration_helpers, 'doPropertyValidation').mockReturnValue({ ok: true });

const result = doExistingDataSourceValidation('target', 'name', 'type', mockHttp as HttpSetup);

await expect(result).resolves.toHaveProperty('ok', true);
coreStartMock.http.post = jest.fn(() =>
Promise.resolve(({
logs: { mappings: { properties: { test: true } } },
} as unknown) as HttpResponse)
);
coreStartMock.http.get = jest.fn(() =>
Promise.resolve(({
data: { mappings: { logs: { template: { mappings: { properties: { test: true } } } } } },
statusCode: 200,
} as unknown) as HttpResponse)
);

const result = await doExistingDataSourceValidation('ss4o_logs-test-test', 'target', 'logs');
expect(result).toHaveProperty('ok', true);
});
});
3 changes: 0 additions & 3 deletions test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ module.exports = {
'<rootDir>/test/',
'<rootDir>/public/requests/',
],
transform: {
'^.+\\.tsx?$': ['ts-jest', { diagnostics: false }],
},
transformIgnorePatterns: ['<rootDir>/node_modules'],
moduleNameMapper: {
'\\.(css|less|sass|scss)$': '<rootDir>/test/__mocks__/styleMock.js',
Expand Down
Loading

0 comments on commit 172faa2

Please sign in to comment.