Я пишу тесты в nodejs, используя mocha framework.Поскольку конечные точки, которые я тестирую, являются асинхронными, я использовал концепцию aync-await.Но тестовый пример не ожидает окончания выполнения before (), т.е.асинхронная функция и, следовательно, показывает неверный результат для listAll () API.
async function fetchContent() {
const [profile, user] = await Promise.all([api.profiles.list(), api.users.list()])
params = {userId: user.items[0].id, label: 'Test', profileId: profile.items[0].id, token: authToken}
testApi = new Api(params)
testApi.profiles.create(params)
}
before(async () => {
await fetchContent()
})
describe('Profiles API', () => {
it('list profiles', done => {
testApi.profiles.listAll().then(response => {
console.log('list=', response)
})
done()
})
})
Также я попробовал это (), как показано ниже, но по-прежнему listAll () не отображает запись профиля, созданную как часть выполнения before ():
describe('Profiles API', () => {
it('list profiles', async () => {
const response = await testApi.profiles.listAll()
console.log('list=', response)
})