Skip to content

Commit

Permalink
write out all fail() invocations
Browse files Browse the repository at this point in the history
I believe the fail() pattern came from jasmine down to jest in the first
instance, though I am not sure. In any event it leads to some strange
testing behavior in which you begin to re-invent the framework from the
inside out.

For this reason it seems the vite team has decided not to make a pass at
including it, since it is technically breaking containment even in the
context of jest testing.

I have done my best to take the time it needs to ascertain which of
these fails() should be asserts, and which should be using the standard
expectations api.
  • Loading branch information
CocoisBuggy committed Dec 11, 2024
1 parent 9985ab1 commit e64abd5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 99 deletions.
13 changes: 7 additions & 6 deletions src/__tests__/gradeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Test grade utilities', () => {

it('creates grade object correctly in US context', () => {
const context = gradeContextToGradeScales.US
if (context == null) fail('Bad grade context. Should not happen.')
assert(context != null, 'Bad grade context, should not happen')

let actual = createGradeObject('5.9', sanitizeDisciplines({ sport: true }), context)
expect(actual).toEqual({
Expand Down Expand Up @@ -88,9 +88,9 @@ describe('Test grade utilities', () => {
expect(actual).toBeUndefined()
})

it.failing('can alpine ice grades to climbs with discipline ice', () => {
it.fails('can alpine ice grades to climbs with discipline ice', () => {
const context = gradeContextToGradeScales.US
if (context == null) fail('Bad grade context. Should not happen.')
assert(context != null, 'Bad grade context, should not happen')

const actual = createGradeObject('AI2', sanitizeDisciplines({ ice: true }), context)
expect(actual).toEqual({
Expand All @@ -100,7 +100,7 @@ describe('Test grade utilities', () => {

it('creates grade object correctly in AU context', () => {
const context = gradeContextToGradeScales.AU
if (context == null) fail('Bad grade context. Should not happen.')
assert(context != null, 'Bad grade context, should not happen')

let actual = createGradeObject('5', sanitizeDisciplines({ sport: true }), context)
expect(actual).toEqual({
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('Test grade utilities', () => {

it('creates grade object correctly in FR context', () => {
const context = gradeContextToGradeScales.FR
if (context == null) fail('Bad grade context. Should not happen.')
assert(context != null, 'Bad grade context, should not happen')

let actual = createGradeObject('5a', sanitizeDisciplines({ sport: true }), context)
expect(actual).toEqual({
Expand Down Expand Up @@ -178,7 +178,8 @@ describe('Test grade utilities', () => {

it('creates grade object correctly in BRZ context', () => {
const context = gradeContextToGradeScales.BRZ
if (context == null) { fail('Bad grade context. Should not happen.') }
assert(context != null, 'Bad grade context, should not happen')

let actual = createGradeObject('V', sanitizeDisciplines({ sport: true }), context)
expect(actual).toEqual({
brazilian_crux: 'V'
Expand Down
2 changes: 1 addition & 1 deletion src/db/export/json/async-file.processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
interface TestType { name: string, path?: string[] }

describe('file processor', () => {
const writer = jest.fn(async (_data, _path) => await Promise.resolve())
const writer = vi.fn(async (_data, _path) => await Promise.resolve())
const testData: TestType[] = [{ name: 'test', path: ['one', 'two'] }, { name: 'test2' }]
const testPath = 'testPath'

Expand Down
17 changes: 4 additions & 13 deletions src/model/__tests__/AreaHistoryDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ describe('Area history', () => {
it('should record an Areas.deleteArea() call', async () => {
const greece = await areas.addCountry('grc')
const leonidio = await areas.addArea(testUser, 'Leonidio', greece.metadata.area_id)

if (leonidio == null) fail()
assert(leonidio != null)

await areas.deleteArea(testUser, leonidio.metadata.area_id)

Expand All @@ -141,21 +140,13 @@ describe('Area history', () => {
const spain = await areas.addCountry('esp')
const margalef = await areas.addArea(testUser, 'margalef', spain.metadata.area_id)

if (margalef == null) fail()
assert(margalef != null)

const newChild = await areas.addArea(testUser, 'One', margalef.metadata.area_id)

if (newChild == null) fail()

let deleted = false
try {
await areas.deleteArea(testUser, margalef.metadata.area_id)
fail('Shouldn\'t allow deletion when the area still has subareas')
} catch (e) {
deleted = true
}
assert(newChild != null)

expect(deleted).toBeTruthy()
await expect(async () => await areas.deleteArea(testUser, margalef.metadata.area_id)).rejects.toThrow()

await waitForExpect(() => expect(onChange).toHaveBeenCalledTimes(5))
const history = await ChangeLogDataSource.getInstance().getAreaChangeSets(spain.metadata.area_id)
Expand Down
17 changes: 8 additions & 9 deletions src/model/__tests__/MediaDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ describe('MediaDataSource', () => {
await areas.addCountry('USA')
areaForTagging1 = await areas.addArea(muuid.v4(), 'Yosemite NP', null, 'USA')
areaForTagging2 = await areas.addArea(muuid.v4(), 'Lake Tahoe', null, 'USA')
if (areaForTagging1 == null || areaForTagging2 == null) fail('Fail to pre-seed test areas')

assert(areaForTagging1 != null, 'Fail to pre-seed test areas')
assert(areaForTagging2 != null, 'Fail to pre-seed test areas')

const rs = await climbs.addOrUpdateClimbs(muuid.v4(), areaForTagging1.metadata.area_id, [newSportClimb1])
if (rs == null) fail('Fail to pre-seed test climbs')
assert(rs != null, 'Fail to pre-seed test areas')
climbIdForTagging = muuid.from(rs[0])

const rs2 = await media.addMediaObjects([TEST_MEDIA])
testMediaObject = rs2[0]
if (testMediaObject == null) {
fail('Fail to create test media')
}

assert(testMediaObject != null, 'fail to create test media')

areaTag1 = {
mediaId: testMediaObject._id,
Expand Down Expand Up @@ -116,7 +117,7 @@ describe('MediaDataSource', () => {
})

it('should tag & remove an area tag', async () => {
if (areaForTagging1 == null) fail('Pre-seeded test area not found')
assert(areaForTagging1 != null, 'Pre-seeded test area not found')

// verify the number tags before test
let mediaObjects = await media.getOneUserMedia(TEST_MEDIA.userUuid, 10)
Expand Down Expand Up @@ -227,9 +228,7 @@ describe('MediaDataSource', () => {

const expectedMedia = await media.addMediaObjects(newMediaListInput)

if (expectedMedia == null) {
fail('Seeding test media fail')
}
assert(expectedMedia != null, 'seeding test media fail')

// reverse because getOneUserMediaPagination() returns most recent first
expectedMedia.reverse()
Expand Down
56 changes: 26 additions & 30 deletions src/model/__tests__/MutableClimbDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('Climb CRUD', () => {
await areas.addCountry('usa')

const newDestination = await areas.addArea(testUser, 'California', null, 'usa')
if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const routesArea = await areas.addArea(testUser, 'Sport & Trad', newDestination.metadata.area_id)

Expand Down Expand Up @@ -208,7 +208,7 @@ describe('Climb CRUD', () => {
await areas.addCountry('esp')

const newDestination = await areas.addArea(testUser, 'Valencia', null, 'esp')
if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const boulderingArea = await areas.addArea(testUser, 'Bouldering only', newDestination.metadata.area_id)

Expand All @@ -223,13 +223,13 @@ describe('Climb CRUD', () => {

const newClimb = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))

if (newClimb == null) fail('Expecting new boulder problem to be added, but didn\'t find one')
assert(newClimb != null)
expect(newClimb.name).toBe(newBoulderProblem1.name)
})

it('can delete new boulder problems', async () => {
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'fr')
if (newBoulderingArea == null) fail('Expect new area to be created')
expect(newBoulderingArea).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('Climb CRUD', () => {

// expect one to remain
rs = await climbs.findOneClimbByMUUID(muid.from(newIDs[1]))
if (rs == null) fail('Expect climb 2 to exist')
assert(rs != null)
expect(rs._id.toUUID().toString()).toEqual(newIDs[1])

const areaRs = await areas.findOneAreaByUUID(newBoulderingArea.metadata.area_id)
Expand All @@ -281,7 +281,7 @@ describe('Climb CRUD', () => {
it('handles mixed grades and disciplines correctly', async () => {
await areas.addCountry('can')
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'can')
if (newBoulderingArea == null) fail('Expect new area to be created')
expect(newBoulderingArea).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand All @@ -304,7 +304,8 @@ describe('Climb CRUD', () => {
{
// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area 1', null, 'aus')
if (newClimbingArea == null) fail('Expect new area to be created')
expect(newClimbingArea).toBeTruthy()


const newclimbs = [
{ ...newSportClimb1, grade: '17' }, // good sport grade
Expand Down Expand Up @@ -351,7 +352,7 @@ describe('Climb CRUD', () => {
{
// A bouldering area
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'aus')
if (newBoulderingArea == null) fail('Expect new area to be created')
expect(newBoulderingArea).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand Down Expand Up @@ -379,7 +380,7 @@ describe('Climb CRUD', () => {
{
// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area in Brazil', null, 'bra')
if (newClimbingArea == null) fail('Expect new area to be created in Brazil')
expect(newClimbingArea).toBeTruthy()

const newclimbs = [
{ ...newSportClimb1, grade: 'VIsup' }, // good sport grade
Expand Down Expand Up @@ -426,7 +427,7 @@ describe('Climb CRUD', () => {
{
// A bouldering area
const newBoulderingArea = await areas.addArea(testUser, 'Bouldering area 1', null, 'bra')
if (newBoulderingArea == null) fail('Expect new area to be created')
expect(newBoulderingArea).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand All @@ -453,7 +454,7 @@ describe('Climb CRUD', () => {

// A roped climbing area
const newClimbingArea = await areas.addArea(testUser, 'Climbing area 1', null, 'deu')
if (newClimbingArea == null) fail('Expect new area to be created')
expect(newClimbingArea).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand Down Expand Up @@ -481,7 +482,7 @@ describe('Climb CRUD', () => {
it('can update boulder problems', async () => {
const newDestination = await areas.addArea(testUser, 'Bouldering area A100', null, 'fr')

if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand Down Expand Up @@ -540,7 +541,7 @@ describe('Climb CRUD', () => {
it('can update climb length, boltsCount & fa', async () => {
const newDestination = await areas.addArea(testUser, 'Sport area Z100', null, 'fr')

if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand Down Expand Up @@ -576,7 +577,7 @@ describe('Climb CRUD', () => {
await areas.addCountry('aut')

const newDestination = await areas.addArea(testUser, 'Some Location with Multi-Pitch Climbs', null, 'aut')
if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const routesArea = await areas.addArea(testUser, 'Sport & Trad Multi-Pitches', newDestination.metadata.area_id)

Expand All @@ -602,22 +603,20 @@ describe('Climb CRUD', () => {
},
pitches: newClimbWithPitches.pitches
})
// Validate each pitch
if (climb?.pitches != null) {
climb.pitches.forEach((pitch) => {

assert(climb?.pitches != null)

climb.pitches.forEach((pitch) => {
expect(pitch).toHaveProperty('_id')
expect(pitch).toHaveProperty('parentId')
expect(pitch).toHaveProperty('pitchNumber')
})
} else {
fail('Pitches are missing either of required attributes id, parentId, pitchNumber')
}
})

it('can update multi-pitch problems', async () => {
const newDestination = await areas.addArea(testUser, 'Some Multi-Pitch Area to be Updated', null, 'deu')

if (newDestination == null) fail('Expect new area to be created')
expect(newDestination).toBeTruthy()

const newIDs = await climbs.addOrUpdateClimbs(
testUser,
Expand All @@ -628,11 +627,9 @@ describe('Climb CRUD', () => {
// Fetch the original climb
const original = await climbs.findOneClimbByMUUID(muid.from(newIDs[0]))

// Check if 'original' is not null before accessing its properties
if ((original == null) || (original.pitches == null) || original.pitches.length < 2) {
fail('Original climb is null or does not have at least two pitches (as defined in the test case)')
return
}
assert(original !== null)
assert(original.pitches !== undefined)
expect(original.pitches.length).toBeGreaterThan(2)

// Store original pitch IDs and parent IDs
const originalPitch1ID = original.pitches[0]._id.toUUID().toString()
Expand Down Expand Up @@ -700,12 +697,11 @@ describe('Climb CRUD', () => {
}

// Check that the createdBy and updatedBy fields are not undefined before accessing their properties
if ((updatedClimb.createdBy != null) && (updatedClimb.updatedBy != null)) {
assert(updatedClimb.createdBy != undefined)
assert(updatedClimb.updatedBy != undefined)

expect(updatedClimb.createdBy.toUUID().toString()).toEqual(testUser.toString())
expect(updatedClimb.updatedBy.toUUID().toString()).toEqual(testUser.toString())
} else {
fail('createdBy or updatedBy is undefined')
}
}
})
})
5 changes: 2 additions & 3 deletions src/model/__tests__/MutableOrganizationDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ describe('Organization', () => {
const updatedOrg = await organizations.updateOrganization(testUser, newOrg.orgId, document)

expect(updatedOrg).toBeDefined()
if (updatedOrg == null) {
fail('should not reach here.')
}
assert(updatedOrg != null)

expect(updatedOrg.associatedAreaIds.map(muuidToString).sort())
.toStrictEqual(document?.associatedAreaIds?.map(muuidToString).sort())
expect(updatedOrg.excludedAreaIds.map(muuidToString).sort())
Expand Down
Loading

0 comments on commit e64abd5

Please sign in to comment.