控制器设置会话数据时,超级测试单元测试超时

时间:2019-06-10 12:01:03

标签: node.js express mocha supertest

我试图在我的应用程序上运行单元测试,但是单元测试超时。当我深入研究它时,我发现它正在超时应用程序尝试设置会话数据的位置。如果删除代码的会话设置行,则单元测试将继续(但失败,因为需要会话数据)。

- (BOOL)fullScreenAppPresentOn:(NSScreen *)screen
{
    // Get all of the visible windows (across all running applications)
    NSArray<NSDictionary*> *windowInfoList = (__bridge_transfer id)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

    // For each window, see if the bounds are the same size as the screen's frame
    for (int windowInfoListIndex = 0; windowInfoListIndex < (int)windowsInfoList.count; windowInfoListIndex++)
    {    
        NSDictionary *windowInfo = windowInfoList[windowInfoListIndex];

        CFDictionaryRef windowInfoRef = (__bridge CFDictionaryRef) windowInfo[(__bridge NSString *)kCGWindowBounds];
        CGRect windowBounds;
        CGRectMakeWithDictionaryRepresentation(windowInfoRef, &windowBounds);

        if (CGRectEqualToRect([screen frame], windowBounds))
        {
            return YES;
        }
    }

    return NO;
}

运行代码时,出现以下错误: 错误:超时超过2000毫秒。对于异步测试和挂钩,请确保调用了“ done()”;如果返回承诺,请确保其解决。 (C:\ Nedbank \ aa-serverui \ application \ tests \ authorise.js)

但是,如果我注释掉会话设置行(// req.session.clientData = clientData;),则测试将继续按预期运行。

1 个答案:

答案 0 :(得分:0)

这是一个最小的工作示例:

app.js

const express = require("express");
const session = require("express-session");
const app = express();

app.use(
  session({
    secret: "keyboard cat",
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true },
  }),
);

function controller(req, res) {
  const clientData = req.query;
  console.log(clientData);
  req.session.clientData = clientData;
  res.sendStatus(302);
}

app.get("/router", controller);

app.get("/api", (req, res) => {
  res.sendStatus(200);
});

module.exports = app;

app.test.js

const supertest = require("supertest");
const app = require("./app");
const agent = supertest(app);
const { expect } = require("chai");

describe("Check if endpoint are reachable", () => {
  const clientData = { name: "supertest" };
  before((done) => {
    agent
      .get("/router")
      .query(clientData)
      .expect(302)
      .end((err, res) => {
        if (!err) {
          done();
        } else {
          done(err);
        }
      });
  });

  it("should pass", async () => {
    const res = await agent.get("/api");
    expect(res.status).to.be.eq(200);
  });
});

具有覆盖率报告的集成测试结果:

  Check if endpoint are reachable
{ name: 'supertest' }
    ✓ should pass


  1 passing (44ms)

-------------|----------|----------|----------|----------|-------------------|
File         |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files    |    96.15 |       50 |      100 |    96.15 |                   |
 app.js      |      100 |      100 |      100 |      100 |                   |
 app.test.js |    92.86 |       50 |      100 |    92.86 |                17 |
-------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/56526009