【问题标题】:Import express instance for integration tests为集成测试导入 express 实例
【发布时间】:2020-09-29 18:49:33
【问题描述】:

我确实将我的单元测试和集成测试分开了。

对于单元测试,它非常简单并且可以正常工作,但是我在使用“supertest”进行集成测试时遇到了一些问题。

我在 packege.json 中有测试命令:

"test": "jest -c jest.config.js",
"testIntegration": "jest -c jest.config.integration.js"

jest.config.integration.js 文件如下所示:

const unitConfig = require('./jest.config');

unitConfig.testPathIgnorePatterns = ['_tests_/unit', '_tests_/acceptance'];
unitConfig.maxConcurrency = 1;
unitConfig.globalSetup = '<rootDir>config/jest/globalSetup.js';
unitConfig.globalTeardown = '<rootDir>config/jest/globalTeardown.js';

module.exports = unitConfig;

globalSetup.js 文件有一个初始化数据库连接和初始化表达应用程序的功能。

const startupFunctions = require('../../startupFunctions');
module.exports = (async () => {
  await startupFunctions.startup();
}), 10000;

好的! 但现在,在测试文件中,当我导入 express 应用程序的实例时,是纯新实例! 就像我做的那样 const app = express() 这个应用没有所有的路由和中间件。

如果我将启动功能移到 beforeAll,那么一切正常。

有什么建议吗?

【问题讨论】:

    标签: node.js integration-testing supertest


    【解决方案1】:

    我发现这是不可能的。 https://github.com/facebook/jest/issues/7184

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2019-07-23
      • 2013-11-14
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多