Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make Jest unit tests run faster in GitHub actions #25726

Merged
merged 15 commits into from
Jul 12, 2024
Merged
44 changes: 32 additions & 12 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
# WARNING! It is currently being investigated how to make this faster
# DO NOT blindly copy this workflow, not noticing the slow down,
# because suddenly our tests will take hours to pass CI.
# Hopefully this comment here will help prevent that.
# https://github.com/MetaMask/metamask-extension/issues/25680

name: Run unit tests

on:
push:
branches: [develop, master]
pull_request:
types: [opened,reopened,synchronize]

jobs:
test-unit-jest:
test-unit:
runs-on: ubuntu-latest
strategy:
matrix:
shard: [1, 2, 3, 4, 5, 6, 7, 8]
itsyoboieltr marked this conversation as resolved.
Show resolved Hide resolved
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: ./.github/actions/setup-environment

- name: test:coverage:jest:dev
run: yarn test:coverage:jest:dev
- name: test:unit:coverage
run: yarn test:unit:coverage --shard=${{ matrix.shard }}/${{ strategy.job-total }}

- name: Rename coverage to shard coverage
run: mv coverage/coverage-final.json coverage/coverage-${{matrix.shard}}.json

- uses: actions/upload-artifact@v4
with:
name: coverage-${{matrix.shard}}
path: coverage/coverage-${{matrix.shard}}.json

report-coverage:
runs-on: ubuntu-latest
needs:
- test-unit
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: test:coverage:jest
run: yarn test:coverage:jest
- name: Download coverage from shards
uses: actions/download-artifact@v4
with:
path: coverage
pattern: coverage-*
merge-multiple: true

- uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673
- name: Upload coverage to Codecov
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
4 changes: 4 additions & 0 deletions development/build/transforms/remove-fenced-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('build/transforms/remove-fenced-code', () => {
lintTransformedFileMock.mockImplementation(() => Promise.resolve());
});

afterEach(() => {
itsyoboieltr marked this conversation as resolved.
Show resolved Hide resolved
jest.resetAllMocks();
});

it('returns a PassThrough stream for files with ignored extensions', async () => {
const fileContent = '"Valid JSON content"\n';
const stream = createRemoveFencedCodeTransform(
Expand Down
11 changes: 0 additions & 11 deletions development/jest.config.js

This file was deleted.

8 changes: 2 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
module.exports = {
collectCoverageFrom: [
itsyoboieltr marked this conversation as resolved.
Show resolved Hide resolved
'<rootDir>/app/scripts/**/*.(js|ts|tsx)',
'<rootDir>/shared/**/*.(js|ts|tsx)',
'<rootDir>/ui/**/*.(js|ts|tsx)',
],
coverageDirectory: './coverage',
coveragePathIgnorePatterns: ['.stories.*', '.snap'],
coverageReporters: ['html', 'json'],
coverageReporters: ['json'],
itsyoboieltr marked this conversation as resolved.
Show resolved Hide resolved
reporters: [
'default',
[
Expand All @@ -28,6 +23,7 @@ module.exports = {
'<rootDir>/ui/**/*.test.(js|ts|tsx)',
'<rootDir>/development/fitness-functions/**/*.test.(js|ts|tsx)',
'<rootDir>/test/e2e/helpers.test.js',
'<rootDir>/development/build/transforms/**/*.test.(js|ts|tsx)',
],
testTimeout: 5500,
// We have to specify the environment we are running in, which is jsdom. The
Expand Down
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
"dapp-chain": "GANACHE_ARGS='-b 2' concurrently -k -n ganache,dapp -p '[{time}][{name}]' 'yarn ganache:start' 'sleep 5 && yarn dapp'",
"forwarder": "node ./development/static-server.js ./node_modules/@metamask/forwarder/dist/ --port 9010",
"dapp-forwarder": "concurrently -k -n forwarder,dapp -p '[{time}][{name}]' 'yarn forwarder' 'yarn dapp'",
"test:unit": "jest",
"test:unit:watch": "jest --watch",
"test:unit:global": "mocha test/unit-global/*.test.js",
"test:unit:coverage": "jest --coverage",
"test:integration": "jest --config jest.integration.config.js",
"test:integration:coverage": "jest --config jest.integration.config.js --coverage",
"test:unit": "node ./test/run-unit-tests.js --jestGlobal --jestDev",
"test:unit:jest": "node ./test/run-unit-tests.js --jestGlobal --jestDev",
"test:unit:jest:watch": "node --inspect ./node_modules/.bin/jest --watch",
"test:unit:global": "mocha test/unit-global/*.test.js",
"test:e2e:chrome": "SELENIUM_BROWSER=chrome node test/e2e/run-all.js",
"test:e2e:chrome:mmi": "SELENIUM_BROWSER=chrome node test/e2e/run-all.js --mmi",
"test:e2e:chrome:flask": "SELENIUM_BROWSER=chrome node test/e2e/run-all.js --build-type flask",
Expand All @@ -61,10 +61,6 @@
"test:e2e:firefox": "SELENIUM_BROWSER=firefox node test/e2e/run-all.js",
"test:e2e:firefox:flask": "SELENIUM_BROWSER=firefox node test/e2e/run-all.js --build-type flask",
"test:e2e:single": "node test/e2e/run-e2e-test.js",
"test:coverage:jest": "node ./test/run-unit-tests.js --jestGlobal --coverage",
"test:coverage:jest:dev": "node ./test/run-unit-tests.js --jestDev --coverage",
"test:coverage": "node ./test/run-unit-tests.js --jestGlobal --jestDev --coverage",
"test:coverage:html": "yarn test:coverage --html",
"ganache:start": "./development/run-ganache.sh",
"sentry:publish": "node ./development/sentry-publish.js",
"lint": "yarn lint:prettier && yarn lint:eslint && yarn lint:tsc && yarn lint:styles",
Expand Down
156 changes: 0 additions & 156 deletions test/run-unit-tests.js

This file was deleted.