Node.js 节点单元测试获胜';不要等待应用程序加载
我决定在服务器开始侦听之前,通过AWS Secrets Manager加载节点服务器的配置json server.jsNode.js 节点单元测试获胜';不要等待应用程序加载,node.js,unit-testing,async-await,aws-secrets-manager,Node.js,Unit Testing,Async Await,Aws Secrets Manager,我决定在服务器开始侦听之前,通过AWS Secrets Manager加载节点服务器的配置json server.js const express = require('express'); const app = express(); const config = require('./helpers/config'); config.init(function(config){ app.config = config; app.set('port', process.env
const express = require('express');
const app = express();
const config = require('./helpers/config');
config.init(function(config){
app.config = config;
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
....
app.listen(app.get('port'), () => {
console.log('express initialized :)');
});
});
module.exports = app;
var aws = require('aws-sdk');
var secretName = "bla";
class Config {
static init(done) {
let client = new aws.SecretsManager({
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_ID,
region: process.env.REGION
});
client.getSecretValue({SecretId: secretName}, function(err, data) {
if (err) {
throw err;
} else {
let res = JSON.parse(data.SecretString);
Config.config = res;
done(res);
}
});
}
}
module.exports = Config;
class Tester {
static async getApp(){
if(Tester.app){
return Tester.app;
}
Tester.app = await require('./../server');
// adding sleep here solves the problem... why doesn't the await works?!
return Tester.app
}
....
}
describe('API Tests :: User', function() {
before((done) => {
Tester.getApp().then((app1) => {
done();
});
});
describe('# register ', function() {
it('should fail for missing email', function(done) {
request('http://127.0.0.1:9763').post('/api/v1/users').end(function(err, res) {
expect(res.statusCode).to.equal(422);
expect(res.body).to.have.property('errors');
done();
});
});
});
});
helpers/config.js
const express = require('express');
const app = express();
const config = require('./helpers/config');
config.init(function(config){
app.config = config;
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
....
app.listen(app.get('port'), () => {
console.log('express initialized :)');
});
});
module.exports = app;
var aws = require('aws-sdk');
var secretName = "bla";
class Config {
static init(done) {
let client = new aws.SecretsManager({
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_ID,
region: process.env.REGION
});
client.getSecretValue({SecretId: secretName}, function(err, data) {
if (err) {
throw err;
} else {
let res = JSON.parse(data.SecretString);
Config.config = res;
done(res);
}
});
}
}
module.exports = Config;
class Tester {
static async getApp(){
if(Tester.app){
return Tester.app;
}
Tester.app = await require('./../server');
// adding sleep here solves the problem... why doesn't the await works?!
return Tester.app
}
....
}
describe('API Tests :: User', function() {
before((done) => {
Tester.getApp().then((app1) => {
done();
});
});
describe('# register ', function() {
it('should fail for missing email', function(done) {
request('http://127.0.0.1:9763').post('/api/v1/users').end(function(err, res) {
expect(res.statusCode).to.equal(422);
expect(res.body).to.have.property('errors');
done();
});
});
});
});
这似乎很有效。然而,它破坏了我基于摩卡的单元测试
我更新了我的单元测试,以包括一个单例,该单例应该在耐心等待配置加载后加载一次应用程序
helpers/tester.js
const express = require('express');
const app = express();
const config = require('./helpers/config');
config.init(function(config){
app.config = config;
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
....
app.listen(app.get('port'), () => {
console.log('express initialized :)');
});
});
module.exports = app;
var aws = require('aws-sdk');
var secretName = "bla";
class Config {
static init(done) {
let client = new aws.SecretsManager({
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_ID,
region: process.env.REGION
});
client.getSecretValue({SecretId: secretName}, function(err, data) {
if (err) {
throw err;
} else {
let res = JSON.parse(data.SecretString);
Config.config = res;
done(res);
}
});
}
}
module.exports = Config;
class Tester {
static async getApp(){
if(Tester.app){
return Tester.app;
}
Tester.app = await require('./../server');
// adding sleep here solves the problem... why doesn't the await works?!
return Tester.app
}
....
}
describe('API Tests :: User', function() {
before((done) => {
Tester.getApp().then((app1) => {
done();
});
});
describe('# register ', function() {
it('should fail for missing email', function(done) {
request('http://127.0.0.1:9763').post('/api/v1/users').end(function(err, res) {
expect(res.statusCode).to.equal(422);
expect(res.body).to.have.property('errors');
done();
});
});
});
});
test/users.test.js
const express = require('express');
const app = express();
const config = require('./helpers/config');
config.init(function(config){
app.config = config;
app.set('port', process.env.PORT || 3000);
app.set('view engine', 'ejs');
....
app.listen(app.get('port'), () => {
console.log('express initialized :)');
});
});
module.exports = app;
var aws = require('aws-sdk');
var secretName = "bla";
class Config {
static init(done) {
let client = new aws.SecretsManager({
secretAccessKey: process.env.SECRET_ACCESS_KEY,
accessKeyId: process.env.ACCESS_ID,
region: process.env.REGION
});
client.getSecretValue({SecretId: secretName}, function(err, data) {
if (err) {
throw err;
} else {
let res = JSON.parse(data.SecretString);
Config.config = res;
done(res);
}
});
}
}
module.exports = Config;
class Tester {
static async getApp(){
if(Tester.app){
return Tester.app;
}
Tester.app = await require('./../server');
// adding sleep here solves the problem... why doesn't the await works?!
return Tester.app
}
....
}
describe('API Tests :: User', function() {
before((done) => {
Tester.getApp().then((app1) => {
done();
});
});
describe('# register ', function() {
it('should fail for missing email', function(done) {
request('http://127.0.0.1:9763').post('/api/v1/users').end(function(err, res) {
expect(res.statusCode).to.equal(422);
expect(res.body).to.have.property('errors');
done();
});
});
});
});
这似乎不像预期的那样有效。单元测试执行得很快,在配置有机会加载之前就失败了。如果我在singleton中添加一个sleep函数,那么一切都正常