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

test: fix flaky form feedback test again #2241

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/app/modules/feedback/__tests__/feedback.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectId } from 'bson-ext'
import { compareAsc } from 'date-fns'
import { times } from 'lodash'
import { omit, times } from 'lodash'
import moment from 'moment-timezone'
import mongoose from 'mongoose'

Expand Down Expand Up @@ -126,11 +126,14 @@ describe('feedback.service', () => {
rating: 1,
})
const expectedCreatedFbs = await Promise.all(expectedFbPromises)
const expectedFeedbackList = expectedCreatedFbs
// The returned feedback also has an `index` key. However, its value is
// nondeterministic as feedback with identical timestamps can be returned
// in any order. Hence omit the `index` key when checking for the expected
// feedback.
const expectedFeedbackListWithoutIndex = expectedCreatedFbs
// Feedback is returned in date order
.sort((a, b) => compareAsc(a.created!, b.created!))
.map((fb, idx) => ({
index: idx + 1,
.map((fb) => ({
timestamp: moment(fb.created).valueOf(),
rating: fb.rating,
comment: fb.comment,
Expand All @@ -140,25 +143,32 @@ describe('feedback.service', () => {
// Act
const actualResult = await FeedbackService.getFormFeedbacks(mockFormId)
const actual = actualResult._unsafeUnwrap()
const actualFeedbackWithoutIndex = actual.feedback.map((f) =>
omit(f, 'index'),
)

// Assert
// Should only average from the feedbacks for given formId.
const expectedAverage = (
expectedCreatedFbs.reduce((acc, curr) => acc + curr.rating, 0) /
expectedCount
).toFixed(2)
expect(actual).toEqual({
average: expectedAverage,
count: expectedCount,
// Feedback may not be returned in same order, so perform unordered check.
// We cannot simply sort the arrays and expect them to be equal, as the order
// is non-deterministic if the timestamps are identical.
feedback: expect.arrayContaining(expectedFeedbackList),
})
expect(actual.average).toBe(expectedAverage)
expect(actual.count).toBe(expectedCount)
// Feedback may not be returned in same order, so perform unordered check.
// We cannot simply sort the arrays and expect them to be equal, as the order
// is non-deterministic if the timestamps are identical.
expect(actualFeedbackWithoutIndex).toEqual(
expect.arrayContaining(expectedFeedbackListWithoutIndex),
)
// Check that there are no extra elements
expect(actual.feedback.length).toBe(expectedFeedbackList.length)
// Check that feedback is returned in date order.
expect(expectedFeedbackList.map((f) => f.timestamp)).toEqual(
expect(actualFeedbackWithoutIndex.length).toBe(
expectedFeedbackListWithoutIndex.length,
)
// Check that feedback is returned in date order. This works even if there are
// elements with identical timestamps, as we are purely checking for the timestamp order,
// without checking any other keys.
expect(expectedFeedbackListWithoutIndex.map((f) => f.timestamp)).toEqual(
actual.feedback.map((f) => f.timestamp),
)
})
Expand Down