简体   繁体   English

为集成测试导入 express 实例

[英]Import express instance for integration tests

I did separate my unit and integration tests.我确实将单元测试和集成测试分开了。

For unit test it's very simple and it's working, but I have some problem with integration tests using "supertest".对于单元测试,它非常简单并且可以正常工作,但是我在使用“supertest”进行集成测试时遇到了一些问题。

I have test commands in packege.json :我在packege.json中有测试命令:

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

The jest.config.integration.js file looks like this: 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;

The globalSetup.js file have one function that init db connection and init express application. globalSetup.js文件有一个 function 初始化数据库连接和初始化快速应用程序。

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

Ok!行! But now, in test file, when I importing instance of express application, is pure new instance!但是现在,在测试文件中,当我导入 express 应用程序的实例时,是纯新实例! Like I did const app = express() This app have no all routes and middlewares.就像我做的那样 const app = express() 这个应用程序没有所有的路由和中间件。

if I move startup function to beforeAll , then everything is fine and working.如果我将启动 function 移动到beforeAll ,那么一切都很好并且可以正常工作。

Any suggestions?有什么建议么?

I found that is not possible.我发现那是不可能的。 https://github.com/facebook/jest/issues/7184 https://github.com/facebook/jest/issues/7184

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM