diff --git a/.changeset/poor-kiwis-rush.md b/.changeset/poor-kiwis-rush.md new file mode 100644 index 0000000000..4fab2281bc --- /dev/null +++ b/.changeset/poor-kiwis-rush.md @@ -0,0 +1,6 @@ +--- +"medusa-payment-paypal": patch +"medusa-payment-stripe": patch +--- + +feat(medusa-payment-paypal,medusa-payment-stripe): speed up unit tests in CI diff --git a/.github/actions/cache-deps/action.yml b/.github/actions/cache-deps/action.yml index 3201c3e5ad..414329e6f0 100644 --- a/.github/actions/cache-deps/action.yml +++ b/.github/actions/cache-deps/action.yml @@ -17,3 +17,6 @@ runs: # We want to only bootstrap and install if no cache is found. - run: yarn install --immutable shell: bash + + - run: yarn build + shell: bash diff --git a/.github/actions/setup-server/action.yml b/.github/actions/setup-server/action.yml index 806d266a21..56b890a6fe 100644 --- a/.github/actions/setup-server/action.yml +++ b/.github/actions/setup-server/action.yml @@ -29,10 +29,6 @@ runs: with: extension: ${{ inputs.cache-extension }} - - name: Build Packages - shell: "bash" - run: yarn build - - name: Install dev cli shell: "bash" run: sudo npm i -g medusa-dev-cli diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 35be9127f8..afe3398dba 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -13,54 +13,8 @@ jobs: module-chunks: ${{ steps.set-module-chunks.outputs.chunks }} api-matrix: ${{ steps.set-api-matrix.outputs.matrix }} api-chunks: ${{ steps.set-api-chunks.outputs.chunks }} - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: "16.10.0" - cache: "yarn" - - - name: Assert changed - run: ./scripts/assert-changed-files-actions.sh "packages" - - - name: Install dependencies - uses: ./.github/actions/cache-deps - with: - extension: pipeline - - - id: set-module-chunks - name: Set Module Chunks - working-directory: integration-tests/modules - run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 3 | ceil)]')" >> $GITHUB_OUTPUT - - - id: set-module-matrix - name: Set Module Matrix - run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT - env: - CHUNKS: ${{ steps.set-module-chunks.outputs.chunks }} - - - id: set-api-chunks - name: Set API Chunks - working-directory: integration-tests/api - run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 3 | ceil)]')" >> $GITHUB_OUTPUT - - - id: set-api-matrix - name: Set API Matrix - run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT - env: - CHUNKS: ${{ steps.set-api-chunks.outputs.chunks }} - - unit-tests: - needs: setup - runs-on: ubuntu-latest - env: - TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} - TURBO_TEAM: ${{ secrets.TURBO_TEAM }} + unit-tests-matrix: ${{ steps.set-unit-tests-matrix.outputs.matrix }} + unit-tests-chunks: ${{ steps.set-unit-tests-chunks.outputs.chunks }} steps: - name: Cancel Previous Runs uses: styfle/cancel-workflow-action@0.11.0 @@ -86,11 +40,68 @@ jobs: with: extension: pipeline - - name: Build Packages - run: yarn build + - id: set-module-chunks + name: Set Module Chunks + working-directory: integration-tests/modules + run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT + + - id: set-module-matrix + name: Set Module Matrix + run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT + env: + CHUNKS: ${{ steps.set-module-chunks.outputs.chunks }} + + - id: set-api-chunks + name: Set API Chunks + working-directory: integration-tests/api + run: echo "chunks=$(yarn run jest --listTests --json | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT + + - id: set-api-matrix + name: Set API Matrix + run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT + env: + CHUNKS: ${{ steps.set-api-chunks.outputs.chunks }} + + - id: set-unit-tests-chunks + name: Set Unit Tests Chunks + run: echo "chunks=$(yarn workspaces list --json | jq -j '[inputs | .name]' | jq -r | jq -cM '[_nwise(length / 2 | ceil)]')" >> $GITHUB_OUTPUT + + - id: set-unit-tests-matrix + name: Set Unit Tests Matrix + run: echo "matrix=$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')" >> $GITHUB_OUTPUT + env: + CHUNKS: ${{ steps.set-unit-tests-chunks.outputs.chunks }} + + unit-tests-matrix: + needs: setup + runs-on: ubuntu-latest + name: Shard (${{ matrix.chunk }}) Unit Tests + strategy: + fail-fast: false + matrix: + chunk: ${{ fromJSON(needs.setup.outputs.unit-tests-matrix) }} + env: + TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }} + TURBO_TEAM: ${{ secrets.TURBO_TEAM }} + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Assert changed + run: ./scripts/assert-changed-files-actions.sh "packages" + + - name: Install dependencies + uses: ./.github/actions/cache-deps + with: + extension: pipeline - name: Run unit tests - run: yarn test + run: yarn test:chunk + env: + CHUNK: ${{ matrix.chunk }} + CHUNKS: ${{ needs.setup.outputs.unit-tests-chunks }} integration-tests-packages: needs: setup @@ -123,30 +134,16 @@ jobs: - 5432:5432 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 - with: - access_token: ${{ github.token }} - - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: "16" - cache: "yarn" - - name: Install dependencies uses: ./.github/actions/cache-deps with: extension: pipeline - - name: Build Packages - run: yarn build - - name: Run integration tests run: yarn test:integration:packages env: @@ -189,30 +186,16 @@ jobs: - 5432:5432 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 - with: - access_token: ${{ github.token }} - - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: "16.10.0" - cache: "yarn" - - name: Install dependencies uses: ./.github/actions/cache-deps with: extension: pipeline - - name: Build Packages - run: yarn build - - name: Run API integration tests run: yarn test:integration:api env: @@ -237,6 +220,21 @@ jobs: - run: exit 0 if: ${{ contains(needs.integration-tests-api-matrix.result, 'success') }} + unit-tests: + if: ${{ always() }} + runs-on: ubuntu-latest + needs: unit-tests-matrix + steps: + - run: exit 1 + if: >- + ${{ + contains(needs.unit-tests-matrix.result, 'failure') + || contains(needs.unit-tests-matrix.result, 'cancelled') + || contains(needs.unit-tests-matrix.result, 'skipped') + }} + - run: exit 0 + if: ${{ contains(needs.unit-tests-matrix.result, 'success') }} + integration-tests-plugins: needs: setup runs-on: ubuntu-latest @@ -259,11 +257,6 @@ jobs: - 5432:5432 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 - with: - access_token: ${{ github.token }} - - name: Checkout uses: actions/checkout@v3 with: @@ -280,9 +273,6 @@ jobs: with: extension: pipeline - - name: Build Packages - run: yarn build - - name: Run plugin integration tests run: yarn test:integration:plugins env: @@ -316,30 +306,16 @@ jobs: - 5432:5432 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 - with: - access_token: ${{ github.token }} - - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: "16.10.0" - cache: "yarn" - - name: Install dependencies uses: ./.github/actions/cache-deps with: extension: pipeline - - name: Build Packages - run: yarn build - - name: Run Modules integration tests run: yarn test:integration:modules env: @@ -386,22 +362,11 @@ jobs: - 5432:5432 steps: - - name: Cancel Previous Runs - uses: styfle/cancel-workflow-action@0.11.0 - with: - access_token: ${{ github.token }} - - name: Checkout uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Setup Node.js environment - uses: actions/setup-node@v3 - with: - node-version: "16.10.0" - cache: "yarn" - - name: Install dependencies uses: ./.github/actions/cache-deps with: diff --git a/package.json b/package.json index a4c52e8730..3b7dce0805 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "prettier": "prettier", "jest": "jest", "test": "turbo run test --concurrency=50% --no-daemon", + "test:chunk": "./scripts/run-workspace-unit-tests-in-chunks.sh", "test:integration:packages": "turbo run test:integration --concurrency=50% --no-daemon --filter='./packages/*'", "test:integration:api": "turbo run test:integration:chunk --concurrency=50% --no-daemon --filter=integration-tests-api", "test:integration:plugins": "turbo run test:integration --concurrency=50% --no-daemon --filter=integration-tests-plugins", diff --git a/packages/medusa-payment-paypal/.swcrc b/packages/medusa-payment-paypal/.swcrc new file mode 100644 index 0000000000..bfc52a2880 --- /dev/null +++ b/packages/medusa-payment-paypal/.swcrc @@ -0,0 +1,11 @@ +{ + "jsc": { + "target": "es5", + "parser": { + "syntax": "typescript" + } + }, + "module": { + "type": "commonjs" + } +} diff --git a/packages/medusa-payment-paypal/jest.config.js b/packages/medusa-payment-paypal/jest.config.js index 212b345660..8746f0f54b 100644 --- a/packages/medusa-payment-paypal/jest.config.js +++ b/packages/medusa-payment-paypal/jest.config.js @@ -1,16 +1,8 @@ module.exports = { - globals: { - "ts-jest": { - tsconfig: "tsconfig.spec.json", - isolatedModules: false, - }, - }, transform: { - "^.+\\.[jt]s?$": "ts-jest", + "^.+\\.[jt]s?$": "@swc/jest", }, - transformIgnorePatterns: [ - "/node_modules/(?!(axios)/).*" - ], + transformIgnorePatterns: ["/node_modules/(?!(axios)/).*", "dist"], testEnvironment: `node`, - moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`], + moduleFileExtensions: [`js`, `ts`], } diff --git a/packages/medusa-payment-paypal/package.json b/packages/medusa-payment-paypal/package.json index 84ea3bd429..8a2fa11aa9 100644 --- a/packages/medusa-payment-paypal/package.json +++ b/packages/medusa-payment-paypal/package.json @@ -18,13 +18,15 @@ "license": "MIT", "scripts": { "prepublishOnly": "cross-env NODE_ENV=production tsc --build", - "test": "jest --passWithNoTests src", + "test": "jest --runInBand --bail --forceExit", "build": "rimraf dist && tsc", "watch": "tsc --watch" }, "devDependencies": { "@medusajs/medusa": "^1.12.2", "@medusajs/types": "^1.8.10", + "@swc/core": "^1.4.8", + "@swc/jest": "^0.2.36", "@types/stripe": "^8.0.417", "cross-env": "^5.2.1", "jest": "^25.5.4", diff --git a/packages/medusa-payment-paypal/src/core/paypal-http-client.ts b/packages/medusa-payment-paypal/src/core/paypal-http-client.ts index 9088557cce..f2d96fd9d6 100644 --- a/packages/medusa-payment-paypal/src/core/paypal-http-client.ts +++ b/packages/medusa-payment-paypal/src/core/paypal-http-client.ts @@ -1,10 +1,10 @@ +import { Logger } from "@medusajs/medusa" import axios, { AxiosInstance, AxiosRequestConfig, Method } from "axios" import { PaypalApiPath, PaypalEnvironmentPaths, PaypalSdkOptions, } from "./types" -import { Logger } from "@medusajs/medusa" const MAX_ATTEMPTS = 2 @@ -31,15 +31,7 @@ export class PaypalHttpClient { this.httpClient_ = new Proxy(axiosInstance, { // Handle automatic retry mechanism get: (target, prop) => { - // @ts-ignore - // eslint-disable-next-line prefer-rest-params - const original = Reflect.get(...arguments) - - if ("request" === (prop as string)) { - return this.retryIfNecessary(original) - } - - return original + return this.retryIfNecessary(target[prop].bind(target)) }, }) } diff --git a/packages/medusa-payment-stripe/jest.config.js b/packages/medusa-payment-stripe/jest.config.js index e564d67c70..16b8fc0518 100644 --- a/packages/medusa-payment-stripe/jest.config.js +++ b/packages/medusa-payment-stripe/jest.config.js @@ -6,8 +6,9 @@ module.exports = { }, }, transform: { - "^.+\\.[jt]s?$": "ts-jest", + "^.+\\.[jt]s?$": "@swc/jest", }, testEnvironment: `node`, - moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`], + moduleFileExtensions: [`js`, `ts`, `json`], + modulePathIgnorePatterns: ["dist", "node_modules"], } diff --git a/packages/medusa-payment-stripe/package.json b/packages/medusa-payment-stripe/package.json index 06145b72fe..04ce7e0958 100644 --- a/packages/medusa-payment-stripe/package.json +++ b/packages/medusa-payment-stripe/package.json @@ -18,13 +18,15 @@ "license": "MIT", "scripts": { "prepublishOnly": "cross-env NODE_ENV=production tsc --build", - "test": "jest --passWithNoTests src", + "test": "jest --runInBand --bail --forceExit", "build": "rimraf dist && tsc -p ./tsconfig.server.json && medusa-admin bundle", "watch": "tsc --watch" }, "devDependencies": { "@medusajs/admin": "^7.1.10", "@medusajs/medusa": "^1.20.3", + "@swc/core": "^1.4.8", + "@swc/jest": "^0.2.36", "@tanstack/react-table": "^8.7.9", "@types/stripe": "^8.0.417", "awilix": "^8.0.1", diff --git a/scripts/run-workspace-unit-tests-in-chunks.sh b/scripts/run-workspace-unit-tests-in-chunks.sh new file mode 100755 index 0000000000..24ece21113 --- /dev/null +++ b/scripts/run-workspace-unit-tests-in-chunks.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Get array of workspaces +# convert NDJSON stream to an array of arrays, divided by chunk size +# The reason we do a conditional here is that github actions thinks that there is +# a secret present with the output of this and therefore refuses to share data between jobs +if [ -z "${CHUNKS}" ]; then + export CHUNKS=$(yarn workspaces list --json | jq -j '[inputs | .name]' | jq -r | jq -cM '[_nwise(length / 2 | ceil)]') +fi + +# get the workspaces of the current CHUNK environment +workspaces=$(echo $CHUNKS | jq -r ".[$CHUNK]") + +echo "workspaces - $workspaces" +# Initialize an empty string for the filters +filters="" + +# Loop through each workspace in the array +for workspace in $(echo "$workspaces" | jq -r '.[]'); do + # Add the workspace name to the filters array as an argument + filters+=" --filter=${workspace}" +done + +# Run the test in the selected chunk +yarn run test $filters diff --git a/turbo.json b/turbo.json index 2a6bccb5d0..f63c2745a5 100644 --- a/turbo.json +++ b/turbo.json @@ -6,15 +6,12 @@ "outputs": ["!node_modules/**", "!src/**", "*/**"] }, "test": { - "dependsOn": ["build"], "outputs": [] }, "test:integration": { - "dependsOn": ["build"], "outputs": [] }, "test:integration:chunk": { - "dependsOn": ["build"], "outputs": [] } }, diff --git a/yarn.lock b/yarn.lock index 73e7ee09cf..8d38f3cb34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -38016,6 +38016,8 @@ __metadata: "@medusajs/medusa": ^1.12.2 "@medusajs/types": ^1.8.10 "@paypal/checkout-server-sdk": ^1.0.3 + "@swc/core": ^1.4.8 + "@swc/jest": ^0.2.36 "@types/stripe": ^8.0.417 axios: ^1.3.4 body-parser: ^1.19.0 @@ -38037,6 +38039,8 @@ __metadata: "@medusajs/admin": ^7.1.10 "@medusajs/medusa": ^1.20.3 "@medusajs/utils": ^1.11.7 + "@swc/core": ^1.4.8 + "@swc/jest": ^0.2.36 "@tanstack/react-table": ^8.7.9 "@types/stripe": ^8.0.417 awilix: ^8.0.1