Skip to content

Instantly share code, notes, and snippets.

@es128
Created October 10, 2013 21:05
Show Gist options
  • Save es128/6925648 to your computer and use it in GitHub Desktop.
Save es128/6925648 to your computer and use it in GitHub Desktop.
Istanbul + process.nextTick problem
var databaseCerts, databaseCertsSpy, overrideConfig, rewire;
rewire = require('rewire');
databaseCerts = function() {};
databaseCertsSpy = {
databaseCerts: databaseCerts
};
overrideConfig = function(GS, property, newVal) {
var origConfig, testConfig;
origConfig = GS.__get__('config');
testConfig = {};
Object.keys(origConfig).forEach(function(key) {
return testConfig[key] = origConfig[key];
});
if (newVal != null) {
testConfig[property] = newVal;
} else {
delete testConfig[property];
}
GS.__set__('config', testConfig);
return GS;
};
describe('example', function() {
var dbAuthority, dbCert, localAuthority, localCert;
beforeEach(function() {
this.Example = rewire("../lib/example");
return this.Example.__set__('databaseCerts', databaseCerts);
});
localCert = function(GS) {
return overrideConfig(GS, 'ssl', {
certPath: 'test/fixtures/certs/servercert.pem',
keyPath: 'test/fixtures/certs/serverkey.pem',
passphrase: null
});
};
localAuthority = function(GS) {
return overrideConfig(GS, 'certAuthority', {
localOnly: true,
certPath: 'test/fixtures/certs/cacert.pem',
keyPath: 'test/fixtures/certs/cakey.pem',
passphrase: null
});
};
dbCert = function(GS) {
delete GS.options.cert;
delete GS.options.key;
return overrideConfig(GS, 'ssl', false);
};
dbAuthority = function(GS) {
delete GS.options.ca;
return overrideConfig(GS, 'certAuthority', {
certPath: 'test/fixtures/certs/cacert.pem',
keyPath: 'test/fixtures/certs/cakey.pem',
passphrase: null
});
};
it('should be able to use local cert and authority', function(done) {
spyOn(databaseCertsSpy, 'databaseCerts').andCallThrough();
this.Example.__set__('databaseCerts', databaseCertsSpy.databaseCerts);
process.nextTick(function() {
expect(databaseCertsSpy.databaseCerts).not.toHaveBeenCalled();
done();
});
localCert(localAuthority(this.Example));
});
it('should be able to use database cert and authority', function(done) {
spyOn(databaseCertsSpy, 'databaseCerts').andCallFake(function(args, cb) {
expect(function() {
cb(new Error('error'));
}).toThrow();
expect(args.config).toEqual(jasmine.any(Object));
expect(args.options).toEqual(jasmine.any(Object));
done();
});
this.Example.__set__('databaseCerts', databaseCertsSpy.databaseCerts);
dbCert(dbAuthority(this.Example));
});
it('should be able to use database cert and local authority', function(done) {
spyOn(databaseCertsSpy, 'databaseCerts').andCallFake(function(args, cb) {
expect(args.config).toEqual(jasmine.any(Object));
expect(args.options).toEqual(jasmine.any(Object));
done();
});
this.Example.__set__('databaseCerts', databaseCertsSpy.databaseCerts);
dbCert(localAuthority(this.Example));
});
it('should be able to use local cert and database authority', function(done) {
spyOn(databaseCertsSpy, 'databaseCerts').andCallFake(function(args, cb) {
expect(args.config).toEqual(jasmine.any(Object));
expect(args.options).toEqual(jasmine.any(Object));
done();
});
this.Example.__set__('databaseCerts', databaseCertsSpy.databaseCerts);
localCert(dbAuthority(this.Example));
});
});
'use strict';
var Example, config, databaseCerts;
config = {
ssl: false,
certAuthority: {
localOnly: true
}
};//require('../config');
databaseCerts = function(){};//require('./utils/databaseCerts');
Example = (function() {
function Example() {
var _this = this;
this.immediate = true;
process.nextTick(function() {
var notFetched, notLocal;
notFetched = !_this.options.key || !_this.options.cert || !_this.options.ca;
notLocal = !config.ssl || !config.certAuthority.localOnly;
if (notLocal && notFetched) {
databaseCerts({
config: config,
options: _this.options
}, function(err) {
if (err != null) {
throw err;
}
/*
if (_this.immediate) {
return _this.initialize();
}
*/
});
}
});
}
Example.prototype.options = {
};
return Example;
})();
module.exports = new Example;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment