我正在使用玩笑来创建一些集成测试来测试AWS Appsync订阅并获得回报
测试运行一秒钟后,Jest没有退出。
这通常意味着您的测试中没有停止异步操作。 >考虑使用
--detectOpenHandles
运行Jest来解决此问题`
我认为这是由于对Observable的回调操作之一使用了async
函数。
如何确保等待next
的可观察操作并且已清理所有内容?
这是订阅设置
this._onCreateEventSubscriptionManager.watcher?.subscribe(
{
complete: () => {
console.log('Completed onCreateEvent subscription')
// Subscription was terminated. Notify the subscribers.
this._onCreateEventSubscriptionManager.connectionStatusChanged(
ConnectionState.Disconnected,
)
},
error: (error) => {
console.log('Failed to update a subscription', error)
//Notify the subscribers.
this._onCreateEventSubscriptionManager.connectionStatusChanged(
ConnectionState.Disconnected,
)
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
next: async (result: any): Promise<void> => {
console.log('executing onCreateEvent subscription', result)
const data = (result.data as OnCreateEventSubscription)?.onCreateEvent
if (!data) {
throw new FatalError(
'onCreateEvent subscription response contained error',
)
} else {
console.log('onCreateEvent subscription worked', data)
const cachedItems = await this.apiClient.getCachedQueryEvents()
...
}
return Promise.resolve()
},
},
)
这是开玩笑的测试
function delay(ms: number) {
console.log(`Waiting ${ms} milleseconds...`)
return new Promise((resolve) => setTimeout(resolve, ms))
}
describe('createEvent(), () => {
it('should subscribe to create event', async () => {
// Setup subscriber
const subscriber = new MySubscriber()
client.subscribe('1', ChangeType.Create, subscriber)
await delay(5000)
expect(subscriber.connectionState).toBe(ConnectionState.Connected)
//Create new item
const id = uuid.v4()
const newItem = new Item()
newItem.title = `dummy_title_${id}`
newItem.firstName = `dummy_first_name_${id}`
newItem.lastName = `dummy_last_name_${id}`
const createdItem = await client.createItem(newItem)
await delay(5000)
expect(createdItem.title).toBe(`dummy_title_${id}`)
expect(createdItem.firstName).toBe(`dummy_first_name_${id}`)
expect(createdItem.lastName).toBe(`dummy_last_name_${id}`)
expect(subscriber.changeType).toBe(ChangeType.Create)
expect(subscriber.item?.id).toBeTruthy()
client.unsubscribeAll()
await delay(5000)
}, 120000)
})
})
此客户端unsubscribeAll()执行以下操作
this._onCreateEventSubscriptionManager.subscription?.unsubscribe()
this._onCreateEventSubscriptionManager.watcher = undefined
this._onCreateEventSubscriptionManager.subscription = undefined