0

I'm testing my NodeJS application with supertest. At the end of my app.js I expose my app, so I can use it within the test.js.

///////////////////
// https options
var options = {
    key: fs.readFileSync("./private/keys/Server.key"),
    cert: fs.readFileSync("./private/certs/Server.crt"),
    ca: fs.readFileSync("./private/ca/CA.crt"),
    requestCert: true,
    rejectUnauthorized: false
};

///////////////////
// start https server
var server = https.createServer(options, app).listen(app.get("port"), function(){
    console.log('Server listening on port ' + app.get('port'));
});

exports = module.exports = server;


When I require my server in my test.js the path of options will cause an error, since my test.js is in the directory ./test/test.js, whereas app.js is in ./app.js.

I'm struggeling to find a clean solution for this problem.

4

1 回答 1

0

如果您将选项放入模块中,您可以直接使用它,也可以使用rewiresandboxed-module之类的东西在测试中存根它

或者,您可以修改导出以接受选项作为参数,以便您需要它,例如 require('server')(config)

于 2013-11-06T14:57:03.760 回答