您可以在preestall和Jest和Supertest进行测试之间增加延迟吗?

时间:2019-07-16 00:14:05

标签: javascript asynchronous jestjs supertest

我正在使用supertest + jest来测试cookie iat(在时间戳记上启动)在命中端点时是否已更新。

我首先创建了超级测试代理,然后单击返回set-cookie标头的端点,该标头将cookie时间戳设置为当前时间戳(更新cookie)。

问题在于,iat以秒为单位,而不是以毫秒为单位,并且preall(初始时间戳创建)和supertest请求(cookie更新)发生在彼此之间一秒钟之内。因此,在99%的时间中,新旧时间戳之间的不相等测试将失败。

我已经四处搜寻,并尝试了许多我在网上发现的代码片段,以开玩笑地进行异步超时,但尚未使它工作。我尝试在续约生效前约60秒快进时钟,但是新旧版本保持不变。

注意:我知道更新有效,因为有时我会在正确的时间到达终点,并且它们会在1秒钟后关闭(并因此通过)。

这里是我尝试过的设置,我觉得自己最具潜力,但我仍然想念一些东西。

  /* This is used to keep the cookie alive while the user is active. */
  let agentB;
  let initialToken;
  beforeAll(async () => {
    /* Setup the supertest agent. */
    agentB = createAgent();
    await authenticate(agentB, testUsers.admin);
    const cookie = getTokenCookie(agentB.jar);
    initialToken = jwtDecode(cookie.value).payload;
  });

  it('should take place when a route is hit and the request has a cookie.', async (done) => {
    jest.useFakeTimers();
    setTimeout(async () => {
      try {
        const result = await setCookie(agentB.jar, agentB.get('/api/persons')); // this line returns the new set-cookie header to be parsed in the following line
        /* forgive this line as I haven't yet figured out a better wy to parse the cookie */
        const decodedCookie = jwtDecode(
          result.res.headers['set-cookie'][0]
            .split(' ')[0]
            .split('=')[1]
            .slice(0, -1)
        ).payload;
        expect(initialToken.iat).not.toBe(decodedCookie.iat);
      } catch (e) {
        done.fail(e);
      }
    }, 60000); // 3 mins
    jest.runAllTimers();
    done();
  });
});```

Expected result is that the iat from the initial cookie and the iat from the renewed cookie would be different each time.

Instead I've gotten 
```expect(received).not.toBe(expected) // Object.is equality

    Expected: not 1563234141```

0 个答案:

没有答案