feat(medusa-payment-paypal,medusa-payment-stripe): speed up unit tests in CI (#6806)

* chore: shard unit tests

* chore: move paypal and stripe to swc

* chore: update actions

* chore: refactor http paypal client

* chore: added changeset

* chore: remove medusa changes
This commit is contained in:
Riqwan Thamir
2024-03-25 15:27:40 +01:00
committed by GitHub
parent 9073d7aba3
commit bacfa3e17b
14 changed files with 141 additions and 144 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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",

View File

@@ -0,0 +1,11 @@
{
"jsc": {
"target": "es5",
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "commonjs"
}
}

View File

@@ -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`],
}

View File

@@ -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",

View File

@@ -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))
},
})
}

View File

@@ -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"],
}

View File

@@ -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",

View File

@@ -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

View File

@@ -6,15 +6,12 @@
"outputs": ["!node_modules/**", "!src/**", "*/**"]
},
"test": {
"dependsOn": ["build"],
"outputs": []
},
"test:integration": {
"dependsOn": ["build"],
"outputs": []
},
"test:integration:chunk": {
"dependsOn": ["build"],
"outputs": []
}
},

View File

@@ -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