Skip to content

Commit 8373270

Browse files
[Backport 2.x] Configure unit test environment; add build & test CI (#106)
* Configure unit test environment; add build & test CI (#104) Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> (cherry picked from commit 1690766) * Update build-and-test.yml Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> --------- Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> Co-authored-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent 9b7024b commit 8373270

11 files changed

+246
-3
lines changed
 

‎.github/workflows/build-and-test.yml

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Build & test
2+
on:
3+
push:
4+
branches:
5+
- "*"
6+
pull_request:
7+
branches:
8+
- "*"
9+
env:
10+
OPENSEARCH_DASHBOARDS_VERSION: '2.x'
11+
12+
jobs:
13+
Get-CI-Image-Tag:
14+
uses: opensearch-project/opensearch-build/.github/workflows/get-ci-image-tag.yml@main
15+
with:
16+
product: opensearch-dashboards
17+
18+
build-and-test-linux:
19+
needs: Get-CI-Image-Tag
20+
name: Build & test
21+
strategy:
22+
matrix:
23+
os: [ubuntu-latest]
24+
runs-on: ${{ matrix.os }}
25+
container:
26+
image: ${{ needs.Get-CI-Image-Tag.outputs.ci-image-version-linux }}
27+
options: --user root
28+
29+
steps:
30+
- name: Checkout OpenSearch Dashboards
31+
uses: actions/checkout@v2
32+
with:
33+
repository: opensearch-project/OpenSearch-Dashboards
34+
ref: ${{ env.OPENSEARCH_DASHBOARDS_VERSION }}
35+
path: OpenSearch-Dashboards
36+
- name: Checkout plugin
37+
uses: actions/checkout@v2
38+
with:
39+
path: OpenSearch-Dashboards/plugins/dashboards-flow-framework
40+
- name: Bootstrap / build / unit test the plugin
41+
run: |
42+
chown -R 1000:1000 `pwd`
43+
cd ./OpenSearch-Dashboards/
44+
su `id -un 1000` -c "source $NVM_DIR/nvm.sh && nvm use && node -v && yarn -v &&
45+
cd ./plugins/dashboards-flow-framework &&
46+
whoami && yarn osd bootstrap && yarn build && yarn run test:jest --coverage"
47+
- name: Uploads coverage
48+
uses: codecov/codecov-action@v1
49+
50+
# TODO: once github actions supports windows and macos docker containers, we can
51+
# merge these in to the above step's matrix, including adding windows support
52+
build-and-test-windows-macos:
53+
name: Build & test
54+
strategy:
55+
matrix:
56+
os: [macos-latest]
57+
runs-on: ${{ matrix.os }}
58+
steps:
59+
# Enable longer filenames for windows
60+
- name: Enable longer filenames
61+
if: ${{ matrix.os == 'windows-latest' }}
62+
run: git config --system core.longpaths true
63+
- name: Checkout OpenSearch Dashboards
64+
uses: actions/checkout@v2
65+
with:
66+
repository: opensearch-project/OpenSearch-Dashboards
67+
ref: ${{ env.OPENSEARCH_DASHBOARDS_VERSION }}
68+
path: OpenSearch-Dashboards
69+
- name: Setup Node
70+
uses: actions/setup-node@v3
71+
with:
72+
node-version-file: './OpenSearch-Dashboards/.nvmrc'
73+
registry-url: 'https://registry.npmjs.org'
74+
- name: Install Yarn
75+
# Need to use bash to avoid having a windows/linux specific step
76+
shell: bash
77+
run: |
78+
YARN_VERSION=$(node -p "require('./OpenSearch-Dashboards/package.json').engines.yarn")
79+
echo "Installing yarn@$YARN_VERSION"
80+
npm i -g yarn@$YARN_VERSION
81+
- run: node -v
82+
- run: yarn -v
83+
- name: Checkout plugin
84+
uses: actions/checkout@v2
85+
with:
86+
path: OpenSearch-Dashboards/plugins/dashboards-flow-framework
87+
- name: Bootstrap the plugin
88+
run: |
89+
cd OpenSearch-Dashboards/plugins/dashboards-flow-framework
90+
yarn osd bootstrap
91+
- name: Build the plugin
92+
run: |
93+
cd OpenSearch-Dashboards/plugins/dashboards-flow-framework
94+
yarn build
95+
- name: Run unit tests
96+
run: |
97+
cd OpenSearch-Dashboards/plugins/dashboards-flow-framework
98+
yarn run test:jest --coverage
99+
- name: Uploads coverage
100+
uses: codecov/codecov-action@v1
101+

‎babel.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
module.exports = {
7+
presets: [
8+
require('@babel/preset-env'),
9+
require('@babel/preset-react'),
10+
require('@babel/preset-typescript'),
11+
],
12+
plugins: [
13+
['@babel/plugin-transform-modules-commonjs', { allowTopLevelThis: true }],
14+
[require('@babel/plugin-transform-runtime'), { regenerator: true }],
15+
],
16+
};

‎opensearch_dashboards.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"opensearchDashboardsVersion": "2.13.0",
55
"server": true,
66
"ui": true,
7-
"requiredBundles": [
8-
"opensearchDashboardsUtils"
9-
],
7+
"requiredBundles": [],
108
"requiredPlugins": [
119
"navigation"
1210
],

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"opensearch": "../../scripts/use_node ../../scripts/opensearch",
1515
"lint:es": "../../scripts/use_node ../../scripts/eslint -c eslintrc.json",
1616
"lint:es:precommit": "yarn lint:es common/* public/* server/*",
17+
"test:jest": "../../node_modules/.bin/jest --config ./test/jest.config.js",
1718
"build": "yarn plugin-helpers build && echo Renaming artifact to $npm_package_config_plugin_zip_name-$npm_package_config_plugin_version.zip && mv ./build/$npm_package_config_plugin_name*.zip ./build/$npm_package_config_plugin_zip_name-$npm_package_config_plugin_version.zip"
1819
},
1920
"repository": {
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import React from 'react';
7+
import { render } from '@testing-library/react';
8+
import { Provider } from 'react-redux';
9+
10+
import {
11+
BrowserRouter as Router,
12+
RouteComponentProps,
13+
Route,
14+
Switch,
15+
} from 'react-router-dom';
16+
import { store } from '../../store';
17+
import { Workflows } from './workflows';
18+
19+
jest.mock('../../services', () => {
20+
const { mockCoreServices } = require('../../../test');
21+
return {
22+
...jest.requireActual('../../services'),
23+
...mockCoreServices,
24+
};
25+
});
26+
27+
const renderWithRouter = () => ({
28+
...render(
29+
<Provider store={store}>
30+
<Router>
31+
<Switch>
32+
<Route
33+
render={(props: RouteComponentProps) => <Workflows {...props} />}
34+
/>
35+
</Switch>
36+
</Router>
37+
</Provider>
38+
),
39+
});
40+
41+
describe('Workflows', () => {
42+
test('renders the page', () => {
43+
const { getByText } = renderWithRouter();
44+
expect(getByText('Workflows')).not.toBeNull();
45+
});
46+
});

‎test/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export * from './mocks';

‎test/jest.config.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
module.exports = {
7+
rootDir: '../',
8+
roots: ['<rootDir>'],
9+
coverageDirectory: './coverage',
10+
// we mock any style-related files and return an empty module. This is needed due to errors
11+
// when jest tries to interpret these types of files.
12+
moduleNameMapper: {
13+
'\\.(css|less|scss|sass)$': '<rootDir>/test/mocks/style_mock.ts',
14+
},
15+
testEnvironment: 'jest-environment-jsdom',
16+
coverageReporters: ['lcov', 'text', 'cobertura'],
17+
testMatch: ['**/*.test.js', '**/*.test.jsx', '**/*.test.ts', '**/*.test.tsx'],
18+
collectCoverageFrom: [
19+
'**/*.ts',
20+
'**/*.tsx',
21+
'**/*.js',
22+
'**/*.jsx',
23+
'!**/models/**',
24+
'!**/node_modules/**',
25+
'!**/index.js',
26+
'!<rootDir>/public/app.js',
27+
'!<rootDir>/index.js',
28+
'!<rootDir>/babel.config.js',
29+
'!<rootDir>/test/**',
30+
'!<rootDir>/server/**',
31+
'!<rootDir>/coverage/**',
32+
'!<rootDir>/scripts/**',
33+
'!<rootDir>/build/**',
34+
'!**/vendor/**',
35+
],
36+
clearMocks: true,
37+
modulePathIgnorePatterns: ['<rootDir>/offline-module-cache/'],
38+
testPathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/node_modules/'],
39+
transformIgnorePatterns: ['<rootDir>/node_modules'],
40+
};

‎test/mocks/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export { mockCoreServices } from './mock_core_services';

‎test/mocks/mock_core_services.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export const mockCoreServices = {
7+
getCore: () => {
8+
return {
9+
chrome: {
10+
setBreadcrumbs: jest.fn(),
11+
},
12+
};
13+
},
14+
getNotifications: () => {
15+
return {
16+
toasts: {
17+
addDanger: jest.fn().mockName('addDanger'),
18+
addSuccess: jest.fn().mockName('addSuccess'),
19+
},
20+
};
21+
},
22+
};

‎test/mocks/style_mock.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export default {};

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"public/**/*.ts",
5555
"public/**/*.tsx",
5656
"server/**/*.ts",
57+
"test/**/*",
5758
"../../typings/**/*"
5859
],
5960
"exclude": ["node_modules", "*/node_modules/"]

0 commit comments

Comments
 (0)