前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jest Async Best Practise: No Nested Async

Jest Async Best Practise: No Nested Async

作者头像
szhshp
发布2022-09-21 10:30:37
4680
发布2022-09-21 10:30:37
举报

Jest Async Best Practise

关键字: Jest with multiple async, Jest nested async 切记这个地方 不要使用嵌套的 test, 使用 beforeAll() 代替 DO NOT USE nested test !!! Use beforeAll() instead

代码语言:javascript
复制
describe('Delete User', () => {
  let xid;

  // 有时候我们先要从数据库提取一些预备数据
  beforeAll(() => {
    axios.get(`${domain}graphQL?query={
      getUser(username: "${testAccount.username}", password: "123456789") {
        errors
        success
        _id
      }
    }
    `)
      .then(res => res.data.data.getUser)
      .then((res) => {
        xid = res._id; // 将其设置给局部变量, 随后下方就可以使用这个变量了
      });
  });


  test('Get User: Wrong ID', () => axios.post(`${domain}graphQL`, {
    query: `mutation{
      deleteUser(_id: "5d0ef90a36ae0b798cd11111") { # wrong id here
        errors
        success
      }
    }`,
  })
    .then(res => res.data.data.deleteUser)
    .then((res) => {
      expect(res.success).toEqual(false);
      expect(res.errors).toEqual([ERRORS.USER.USER_NOT_FOUND]);
    }));

  test('Get User: Correct Inputs', () => axios.post(`${domain}graphQL`, {
    query: `mutation{
      deleteUser(_id: "${xid}") {
        errors
        success
      }
    }`,
  })
    .then(res => res.data.data.deleteUser)
    .then((res) => {
      expect(res.success).toEqual(true);
      expect(res.errors).toEqual([]);
    }));
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Jest Async Best Practise
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档