Skip to content

Commit 91f6ed5

Browse files
authored
[Chore][DEVX-547] Fix Flaky Tests Suites (#1005)
* chore(integration-tests): fix flaky tests - fix flaky order search tests - ensure created resources are properly deleted - minor refactor tests * chore(integration-tests): fix flaky tests - fix flaky order search tests - ensure created resources are properly deleted - minor refactor tests * chore(integration-tests): fix flaky tests - fix flaky order search tests - ensure created resources are properly deleted - minor refactor tests * chore(integration-tests): fix flaky tests - fix flaky order search tests - ensure created resources are properly deleted - minor refactor tests * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor * chore(integration-tests): remove remaining duplicates - remove remaining duplicate integration tests - refactor tests and remove unnecessary suites - minor code refactor
1 parent 247da96 commit 91f6ed5

63 files changed

Lines changed: 967 additions & 2110 deletions

File tree

Some content is hidden

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"generate": "manypkg run generate && yarn format",
99
"format": "prettier --write '**/*.{js,ts,json,md}'",
1010
"build": "preconstruct build",
11-
"test": "jest --maxWorkers=2",
11+
"test": "jest --runInBand",
1212
"coverage": "codecov",
1313
"typecheck": "tsc --noEmit",
1414
"changeset": "changeset",

packages/platform-sdk/test/helpers/test-utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ export async function waitUntil(
2626
}
2727
}
2828
}
29+
30+
export const sleep = async (ms: number) =>
31+
new Promise((resolve) => setTimeout(resolve, ms))
32+
33+
export const waitForIndexing = async (
34+
fetchResource: () => Promise<any>,
35+
timeout = 30000,
36+
interval = 10000
37+
) => {
38+
const start = Date.now()
39+
while (Date.now() - start < timeout) {
40+
try {
41+
const result = await fetchResource()
42+
if (result) return result
43+
} catch (e) {
44+
/** noop */
45+
}
46+
47+
// sleep
48+
await sleep(interval)
49+
}
50+
51+
throw new Error('Resource did not indexed within timeout')
52+
}

packages/platform-sdk/test/integration-tests/cart-discount/cart-discount-fixture.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ const cartDiscountDraft: CartDiscountDraft = {
2525
requiresDiscountCode: true,
2626
}
2727

28-
export const createCartDiscount = async (cartDiscountDraftBody?) => {
29-
return await apiRoot
28+
export const createCartDiscount = async (cartDiscountDraftBody?) =>
29+
apiRoot
3030
.cartDiscounts()
3131
.post({ body: cartDiscountDraftBody || cartDiscountDraft })
3232
.execute()
33-
}
34-
export const deleteCartDiscount = async (responseCreatedCartDiscount) => {
35-
return await apiRoot
33+
34+
export const deleteCartDiscount = async (responseCreatedCartDiscount) =>
35+
apiRoot
3636
.cartDiscounts()
3737
.withId({ ID: responseCreatedCartDiscount.body.id })
3838
.delete({
3939
queryArgs: { version: responseCreatedCartDiscount.body.version },
4040
})
4141
.execute()
42-
}

packages/platform-sdk/test/integration-tests/cart-discount/cart-discount.test.ts

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
import { randomUUID } from 'crypto'
99
import { apiRoot, SORT_ORDER } from '../test-utils'
1010

11-
import { createCartDiscount, deleteCartDiscount } from './cart-discount-fixture'
11+
import { deleteCartDiscount } from './cart-discount-fixture'
1212
import { createType, deleteType } from '../type/type-fixture'
1313

1414
describe('testing cart discount API calls', () => {
15-
it('should create and delete a cart discount by ID', async () => {
15+
let cartDiscount, type
16+
it('should create a cart discount', async () => {
1617
const cartDiscountValueDraft: CartDiscountValueRelativeDraft = {
1718
type: 'relative',
1819
permyriad: 10,
@@ -35,53 +36,34 @@ describe('testing cart discount API calls', () => {
3536
.post({ body: cartDiscountDraft })
3637
.execute()
3738

39+
cartDiscount = responseCreatedCartDiscount
40+
expect(responseCreatedCartDiscount.body).toBeDefined()
3841
expect(responseCreatedCartDiscount.statusCode).toEqual(201)
39-
expect(responseCreatedCartDiscount.body).not.toBe(null)
40-
41-
const responseCartDiscountDeleted = await apiRoot
42-
.cartDiscounts()
43-
.withId({ ID: responseCreatedCartDiscount.body.id })
44-
.delete({
45-
queryArgs: { version: responseCreatedCartDiscount.body.version },
46-
})
47-
.execute()
48-
49-
expect(responseCartDiscountDeleted.statusCode).toEqual(200)
5042
})
5143

5244
it('should get a cart discount by Id', async () => {
53-
const cartDiscount = await createCartDiscount()
54-
5545
const getCartDiscount = await apiRoot
5646
.cartDiscounts()
5747
.withId({ ID: cartDiscount.body.id })
5848
.get()
5949
.execute()
6050

61-
expect(getCartDiscount).not.toBe(null)
51+
expect(getCartDiscount).toBeDefined()
6252
expect(getCartDiscount.body.id).toEqual(cartDiscount.body.id)
63-
64-
await deleteCartDiscount(cartDiscount)
6553
})
6654

6755
it('should get a cart discount by key', async () => {
68-
const cartDiscount = await createCartDiscount()
69-
7056
const getCartDiscount = await apiRoot
7157
.cartDiscounts()
7258
.withKey({ key: cartDiscount.body.key })
7359
.get()
7460
.execute()
7561

76-
expect(getCartDiscount).not.toBe(null)
62+
expect(getCartDiscount).toBeDefined()
7763
expect(getCartDiscount.body.key).toEqual(cartDiscount.body.key)
78-
79-
await deleteCartDiscount(cartDiscount)
8064
})
8165

82-
it('should query a cart discount if the name is equal', async () => {
83-
const cartDiscount = await createCartDiscount()
84-
66+
it('should query a cart discount', async () => {
8567
const queryCartDiscount = await apiRoot
8668
.cartDiscounts()
8769
.get({
@@ -92,15 +74,11 @@ describe('testing cart discount API calls', () => {
9274
})
9375
.execute()
9476

95-
expect(queryCartDiscount).not.toBe(null)
77+
expect(queryCartDiscount).toBeDefined()
9678
expect(queryCartDiscount.body.results[0].id).toEqual(cartDiscount.body.id)
97-
98-
await deleteCartDiscount(cartDiscount)
9979
})
10080

10181
it('should update a cart discount by Id', async () => {
102-
const cartDiscount = await createCartDiscount()
103-
10482
const updateCartDiscount = await apiRoot
10583
.cartDiscounts()
10684
.withId({ ID: cartDiscount.body.id })
@@ -117,15 +95,14 @@ describe('testing cart discount API calls', () => {
11795
})
11896
.execute()
11997

120-
expect(updateCartDiscount.body.version).not.toBe(cartDiscount.body.version)
98+
expect(updateCartDiscount.body.version).not.toEqual(
99+
cartDiscount.body.version
100+
)
121101
expect(updateCartDiscount.statusCode).toEqual(200)
122-
123-
await deleteCartDiscount(updateCartDiscount)
102+
cartDiscount = updateCartDiscount
124103
})
125104

126105
it('should update a cart discount by Key', async () => {
127-
const cartDiscount = await createCartDiscount()
128-
129106
const updateCartDiscount = await apiRoot
130107
.cartDiscounts()
131108
.withKey({ key: cartDiscount.body.key })
@@ -142,24 +119,22 @@ describe('testing cart discount API calls', () => {
142119
})
143120
.execute()
144121

145-
expect(updateCartDiscount.body.version).not.toBe(cartDiscount.body.version)
146122
expect(updateCartDiscount.statusCode).toEqual(200)
147-
148-
await deleteCartDiscount(updateCartDiscount)
123+
expect(updateCartDiscount.body.version).not.toEqual(
124+
cartDiscount.body.version
125+
)
126+
cartDiscount = updateCartDiscount
149127
})
150128

151129
it('should set customer type to a cart discount', async () => {
152-
const type = await createType()
153-
const cartDiscount = await createCartDiscount()
154-
130+
type = await createType()
155131
const typeResourceIdentifier: TypeResourceIdentifier = {
156132
typeId: 'type',
157133
id: type.body.id,
158134
}
159135
const fieldName: string = type.body.fieldDefinitions[0].name
160-
const fieldValue = 'fieldValue'
161136
const fieldContainer: FieldContainer = {
162-
[fieldName]: fieldValue,
137+
[fieldName]: 'fieldValue',
163138
}
164139

165140
const updateCartDiscount = await apiRoot
@@ -179,10 +154,16 @@ describe('testing cart discount API calls', () => {
179154
})
180155
.execute()
181156

182-
expect(updateCartDiscount.body.version).not.toBe(cartDiscount.body.version)
183157
expect(updateCartDiscount.statusCode).toEqual(200)
158+
expect(updateCartDiscount.body.version).not.toEqual(
159+
cartDiscount.body.version
160+
)
184161

185-
await deleteCartDiscount(updateCartDiscount)
162+
cartDiscount = updateCartDiscount
163+
})
164+
165+
afterAll(async () => {
166+
await deleteCartDiscount(cartDiscount)
186167
await deleteType(type)
187168
})
188169
})

packages/platform-sdk/test/integration-tests/cart/cart-fixture.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const cartDraft: CartDraft = {
66
key: 'test-cart-key-' + randomUUID(),
77
currency: 'EUR',
88
country: 'DE',
9+
shippingAddress: {
10+
country: 'DE',
11+
state: 'Berlin',
12+
},
913
}
10-
export const createCartDraftWithCustomer = async (customer) => {
14+
export const createCartDraftWithCustomer = (customer) => {
1115
const cartDraft: CartDraft = {
1216
key: 'test-cart-key-' + randomUUID(),
1317
currency: 'EUR',
@@ -18,19 +22,26 @@ export const createCartDraftWithCustomer = async (customer) => {
1822
return cartDraft
1923
}
2024

21-
export const createCart = async (cartDraftBody?) => {
22-
return await apiRoot
25+
export const createCart = async (cartDraftBody?) =>
26+
apiRoot
2327
.carts()
2428
.post({ body: cartDraftBody || cartDraft })
2529
.execute()
26-
}
2730

2831
export const deleteCart = async (cart) => {
29-
return await apiRoot
32+
// get latest cart
33+
const _cart = await apiRoot
34+
.carts()
35+
.withId({ ID: cart.body.id })
36+
.get()
37+
.execute()
38+
39+
// delete cart
40+
await apiRoot
3041
.carts()
3142
.withId({ ID: cart.body.id })
3243
.delete({
33-
queryArgs: { version: cart.body.version },
44+
queryArgs: { version: _cart.body.version },
3445
})
3546
.execute()
3647
}

packages/platform-sdk/test/integration-tests/cart/cart.me.test.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import { randomUUID } from 'crypto'
1212
import { apiRoot } from '../test-utils'
1313
import { createCategory } from '../category/category-fixture'
1414
import { ensureTaxCategory } from '../tax-category/tax-category-fixture'
15+
import { ensureProductType } from '../product-type/product-type-fixture'
1516
import {
16-
ensureProductType,
17-
productTypeDraftForProduct,
18-
} from '../product-type/product-type-fixture'
19-
import { createProduct, createProductDraft } from '../product/product-fixture'
17+
createProduct,
18+
createProductDraft,
19+
deleteProduct,
20+
} from '../product/product-fixture'
2021

2122
const projectKey = requireEnvVar('CTP_PROJECT_KEY')
2223
const clientId = requireEnvVar('CTP_CLIENT_ID')
@@ -35,6 +36,7 @@ describe('testing me endpoint cart', () => {
3536
credentials: {
3637
clientId: clientId,
3738
clientSecret: clientSecret,
39+
anonymousId: 'anonymous-id-' + randomUUID(),
3840
},
3941
scopes: [`manage_project:${projectKey}`],
4042
fetch,
@@ -68,17 +70,17 @@ describe('testing me endpoint cart', () => {
6870
})
6971
.execute()
7072

71-
expect(responseCartCreate.statusCode).toBe(201)
72-
expect(responseCartCreate.body).not.toBeNull()
73+
expect(responseCartCreate.body).toBeDefined()
74+
expect(responseCartCreate.statusCode).toEqual(201)
7375
})
7476

7577
// https://github.com/commercetools/commercetools-sdk-typescript/issues/446
7678
it('should expand active cart using me endpoint in a store', async () => {
7779
const category = await createCategory()
78-
const productType = await ensureProductType(productTypeDraftForProduct)
80+
const productType = await ensureProductType()
7981
const taxCategory = await ensureTaxCategory()
8082

81-
const productDraft = await createProductDraft(
83+
const productDraft = createProductDraft(
8284
category,
8385
taxCategory,
8486
productType,
@@ -97,9 +99,8 @@ describe('testing me endpoint cart', () => {
9799
key: randomUUID(),
98100
}
99101

100-
await apiRoot.stores().post({ body: storeDraft }).execute()
101-
102-
await anonymousApiRoot
102+
const store = await apiRoot.stores().post({ body: storeDraft }).execute()
103+
const cart = await anonymousApiRoot
103104
.inStoreKeyWithStoreKeyValue({ storeKey: storeDraft.key })
104105
.me()
105106
.carts()
@@ -123,5 +124,21 @@ describe('testing me endpoint cart', () => {
123124
expect(responseActiveCarts.body.lineItems[0].productType.obj.name).toEqual(
124125
productType.body.name
125126
)
127+
128+
// clean
129+
await deleteProduct(product)
130+
await anonymousApiRoot
131+
.inStoreKeyWithStoreKeyValue({ storeKey: storeDraft.key })
132+
.me()
133+
.carts()
134+
.withId({ ID: cart.body.id })
135+
.delete({ queryArgs: { version: cart.body.version } })
136+
.execute()
137+
138+
await anonymousApiRoot
139+
.stores()
140+
.withId({ ID: store.body.id })
141+
.delete({ queryArgs: { version: store.body.version } })
142+
.execute()
126143
})
127144
})

0 commit comments

Comments
 (0)