最初發佈於 szhshp的第三邊境研究所, 轉載請註明ios
關鍵字: Jest with multiple async, Jest nested async數據庫
切記這個地方 不要使用嵌套的 test, 使用 beforeAll() 代替axios
DO NOT USE nested test !!!dom
Use
beforeAll()
insteadasync
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([]); })); });