Skip to content

Commit

Permalink
Fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
harryadel committed Nov 4, 2024
1 parent 25c17e9 commit d2f5135
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions tests/find_after_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ Tinytest.addAsync('issue #296 - after insert hook always finds all inserted', as
Tinytest.addAsync('async find hook - after insert hook always finds all inserted', async function (test, next) {
const collection = new Mongo.Collection(null)

collection.before.find(async (userId, selector) => {
await new Promise(resolve => setTimeout(resolve, 10)) // Simulate async operation
collection.before.find((userId, selector) => {
selector.removedAt = { $exists: false }
return true
})

collection.before.findOne((userId, selector) => {
selector.removedAt = { $exists: false }
return true
})
Expand All @@ -74,15 +78,12 @@ Tinytest.addAsync('async find hook - after insert hook always finds all inserted

await collection.insertAsync({ removedAt: new Date() })

// Wait for a short time to ensure async find hook has completed
await new Promise(resolve => setTimeout(resolve, 20))

test.equal(beforeCalled, true)
test.equal(afterCalled, true)
test.equal(beforeCalled, true, 'before insert hook should be called')
test.equal(afterCalled, true, 'after insert hook should be called')

// Verify that the find hook is working
const result = await collection.findOneAsync({})
test.isUndefined(result, 'Document should not be found due to async find hook')
const findResult = await collection.find({}).fetchAsync()
test.equal(findResult.length, 0, 'No documents should be found due to find hook')

next()
const findOneResult = await collection.findOneAsync({})
test.isUndefined(findOneResult, 'Document should not be found due to find hook')
})

0 comments on commit d2f5135

Please sign in to comment.