Skip to content

Commit 7c4a021

Browse files
fix(tooling): fix jest setup for UI testing (#460)
1 parent d867d2f commit 7c4a021

43 files changed

Lines changed: 180 additions & 291 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.babelrc.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
run: yarn workspace @webex/widgets run build:src
7575

7676
- name: Build cc widgets
77-
run: yarn run build
77+
run: yarn run build:prod
7878

7979
- name: Cache Distributables
8080
uses: actions/cache@v4

babel.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', {targets: {node: 'current'}, modules: 'commonjs'}], // Transform to CommonJS
4+
['@babel/preset-react', {runtime: 'automatic'}],
5+
'@babel/preset-typescript',
6+
],
7+
plugins: [
8+
'@babel/plugin-transform-runtime', // Optional: For optimized helpers
9+
],
10+
};

jest.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
rootDir: '.',
5+
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
6+
moduleNameMapper: {
7+
'^.+\\.(css|less|scss)$': 'babel-jest',
8+
},
9+
testEnvironment: 'jsdom',
10+
testMatch: ['**/tooling/tests/**/*.js'],
11+
transformIgnorePatterns: [
12+
'/node_modules/(?!(@momentum-design/components|@momentum-ui/react-collaboration|@lit|lit|cheerio))',
13+
],
14+
// Use babel-jest or ts-jest depending on your setup
15+
transform: {
16+
'\\.[jt]sx?$': 'babel-jest',
17+
'\\.[jt]s?$': 'babel-jest',
18+
},
19+
moduleDirectories: ['node_modules', 'src'],
20+
};

jest.setup.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Mock canvas methods to prevent errors in tests
2+
// This is a workaround for the fact that JSDOM does not support canvas methods like getContext.
3+
import 'jest-canvas-mock';
4+
5+
// Web components used in @momentum-design imports rely on browser-only APIs like attachInternals.
6+
// Jest (via JSDOM) doesn't support these, causing runtime errors in tests.
7+
// We mock these methods on HTMLElement to prevent test failures.
8+
window.HTMLElement.prototype.attachInternals = function () {
9+
return {
10+
setValidity: () => {},
11+
checkValidity: () => true,
12+
reportValidity: () => true,
13+
setFormValue: () => {},
14+
};
15+
};

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
"@semantic-release/git": "^10.0.1",
1919
"@semantic-release/github": "^11.0.1",
2020
"@testing-library/react-hooks": "^8.0.1",
21+
"babel-jest": "^29.7.0",
2122
"crypto-browserify": "^3.12.1",
2223
"css-loader": "^7.1.2",
2324
"html-webpack-plugin": "^5.6.3",
2425
"http-server": "^14.1.1",
2526
"husky": "^9.1.7",
2627
"jest": "29.7.0",
28+
"jest-canvas-mock": "^2.5.2",
2729
"node-gyp": "^10.2.0",
2830
"os-browserify": "^0.3.0",
2931
"process": "^0.11.10",
@@ -62,10 +64,5 @@
6264
"release:widgets": "semantic-release",
6365
"postinstall": "husky install",
6466
"prepare": "husky"
65-
},
66-
"jest": {
67-
"testMatch": [
68-
"**/tooling/tests/*.js"
69-
]
7067
}
7168
}

packages/contact-center/cc-components/.babelrc.js

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const baseConfig = require('../../../babel.config.js');
2+
3+
module.exports = baseConfig;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const jestConfig = require('../../../jest.config.js');
2+
3+
jestConfig.rootDir = '../../../';
4+
jestConfig.testMatch = ['**/cc-components/tests/**/*.ts', '**/cc-components/tests/**/*.tsx'];
5+
6+
module.exports = jestConfig;

packages/contact-center/cc-components/package.json

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@testing-library/react": "16.0.1",
3939
"@types/jest": "29.5.14",
4040
"@types/react-test-renderer": "18",
41-
"babel-jest": "29.7.0",
4241
"babel-loader": "9.2.1",
4342
"eslint": "^9.20.1",
4443
"eslint-config-prettier": "^10.0.1",
@@ -60,19 +59,9 @@
6059
"webpack-cli": "5.1.4",
6160
"webpack-merge": "6.0.1"
6261
},
63-
"jest": {
64-
"testEnvironment": "jsdom",
65-
"testMatch": [
66-
"**/tests/**/*.ts",
67-
"**/tests/**/*.tsx"
68-
],
69-
"moduleNameMapper": {
70-
"^.+\\.(css|less|scss)$": "babel-jest"
71-
}
72-
},
7362
"peerDependencies": {
7463
"@momentum-ui/react-collaboration": ">=26.197.0",
7564
"react": ">=18.3.1",
7665
"react-dom": ">=18.3.1"
7766
}
78-
}
67+
}

0 commit comments

Comments
 (0)