From e581d3bd90f9bc40105e7eaf34e0c94d4f657f7a Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 27 Jan 2023 17:45:54 +0100 Subject: [PATCH] chore(integration-tests): Flaky tests (#3126) --- .changeset/wise-lemons-sneeze.md | 5 ++++ .../api/__tests__/admin/product.js | 21 ++++++++----- .../api/__tests__/store/products.js | 30 ++++++++++++------- packages/medusa/src/repositories/product.ts | 5 +++- .../src/services/__tests__/product-variant.js | 3 ++ .../medusa/src/services/product-variant.ts | 4 ++- 6 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 .changeset/wise-lemons-sneeze.md diff --git a/.changeset/wise-lemons-sneeze.md b/.changeset/wise-lemons-sneeze.md new file mode 100644 index 0000000000..9ea9b83162 --- /dev/null +++ b/.changeset/wise-lemons-sneeze.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chore: fix flaky tests diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index cd17ec513a..3208eb4399 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -1915,9 +1915,10 @@ describe("/admin/products", () => { ], } + const variantId = "test-variant" const response = await api .post( - "/admin/products/test-product/variants/test-variant", + `/admin/products/test-product/variants/${variantId}`, data, adminHeaders ) @@ -1927,9 +1928,12 @@ describe("/admin/products", () => { expect(response.status).toEqual(200) - expect(response.data.product.variants[0].prices.length).toEqual(2) + const variant = response.data.product.variants.find( + (v) => v.id === variantId + ) + expect(variant.prices.length).toEqual(2) - expect(response.data.product.variants[0].prices).toEqual( + expect(variant.prices).toEqual( expect.arrayContaining([ expect.objectContaining({ amount: 8000, @@ -1958,9 +1962,10 @@ describe("/admin/products", () => { ], } + const variantId = "test-variant_3" const response = await api .post( - "/admin/products/test-product1/variants/test-variant_3", + `/admin/products/test-product1/variants/${variantId}`, data, adminHeaders ) @@ -1970,11 +1975,13 @@ describe("/admin/products", () => { expect(response.status).toEqual(200) - expect(response.data.product.variants[0].prices.length).toEqual( - data.prices.length + const variant = response.data.product.variants.find( + (v) => v.id === variantId ) - expect(response.data.product.variants[0].prices).toEqual( + expect(variant.prices.length).toEqual(data.prices.length) + + expect(variant.prices).toEqual( expect.arrayContaining([ expect.objectContaining({ amount: 8000, diff --git a/integration-tests/api/__tests__/store/products.js b/integration-tests/api/__tests__/store/products.js index e07083c20f..78400bb8d0 100644 --- a/integration-tests/api/__tests__/store/products.js +++ b/integration-tests/api/__tests__/store/products.js @@ -471,8 +471,18 @@ describe("/store/products", () => { console.log(err) }) - expect(response.data.products).toHaveLength(5) - expect(response.data.products).toEqual( + const products = response.data.products + + expect(products).toHaveLength(5) + + const testProduct = products.find((p) => p.id === testProductId) + expect(testProduct.variants).toHaveLength(3) + + for (const variant of testProduct.variants) { + expect(variant.prices).toHaveLength(2) + } + + expect(products).toEqual( expect.arrayContaining([ expect.objectContaining({ id: testProductId1, @@ -481,11 +491,11 @@ describe("/store/products", () => { expect.objectContaining({ id: testProductId, collection_id: "test-collection", - variants: [ + variants: expect.arrayContaining([ expect.objectContaining({ original_price: 100, calculated_price: 80, - prices: [ + prices: expect.arrayContaining([ expect.objectContaining({ id: "test-price", currency_code: "usd", @@ -496,12 +506,12 @@ describe("/store/products", () => { currency_code: "usd", amount: 80, }), - ], + ]), }), expect.objectContaining({ original_price: 100, calculated_price: 80, - prices: [ + prices: expect.arrayContaining([ expect.objectContaining({ id: "test-price1", currency_code: "usd", @@ -512,12 +522,12 @@ describe("/store/products", () => { currency_code: "usd", amount: 80, }), - ], + ]), }), expect.objectContaining({ original_price: 100, calculated_price: 80, - prices: [ + prices: expect.arrayContaining([ expect.objectContaining({ id: "test-price2", currency_code: "usd", @@ -528,9 +538,9 @@ describe("/store/products", () => { currency_code: "usd", amount: 80, }), - ], + ]), }), - ], + ]), }), expect.objectContaining({ id: testProductFilteringId2, diff --git a/packages/medusa/src/repositories/product.ts b/packages/medusa/src/repositories/product.ts index 22f7cc7136..98aab6edf1 100644 --- a/packages/medusa/src/repositories/product.ts +++ b/packages/medusa/src/repositories/product.ts @@ -169,7 +169,10 @@ export class ProductRepository extends Repository { "variants.deleted_at IS NULL" ) - order["variants.variant_rank"] = "ASC" + if (!Object.keys(order).some((key) => key.startsWith("variants"))) { + // variant_rank being select false, apply the filter here directly + querybuilder.addOrderBy(`${toplevel}.variant_rank`, "ASC") + } } else { querybuilder = querybuilder.leftJoinAndSelect( `products.${toplevel}`, diff --git a/packages/medusa/src/services/__tests__/product-variant.js b/packages/medusa/src/services/__tests__/product-variant.js index ef465b9423..0b834f2e40 100644 --- a/packages/medusa/src/services/__tests__/product-variant.js +++ b/packages/medusa/src/services/__tests__/product-variant.js @@ -650,6 +650,9 @@ describe("ProductVariantService", () => { .mockImplementation(() => Promise.resolve()) const regionService = { + withTransaction: function () { + return this + }, list: jest.fn().mockImplementation((config) => { const idOrIds = config.id diff --git a/packages/medusa/src/services/product-variant.ts b/packages/medusa/src/services/product-variant.ts index 29c45c69af..1bf5b4392a 100644 --- a/packages/medusa/src/services/product-variant.ts +++ b/packages/medusa/src/services/product-variant.ts @@ -347,9 +347,11 @@ class ProductVariantService extends TransactionBaseService { prices ) + const regionsServiceTx = this.regionService_.withTransaction(manager) + for (const price of prices) { if (price.region_id) { - const region = await this.regionService_.retrieve(price.region_id) + const region = await regionsServiceTx.retrieve(price.region_id) await this.setRegionPrice(variantId, { currency_code: region.currency_code,