Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

fix: can't redefine process.env when node<4 #9

Merged
merged 2 commits into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions lib/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ var method = module.exports = function mockMethod(obj, key, method) {
delete obj[key];
}

// set a flag that checks if it is mocked
var flag = cache.get(obj);
if (!flag) {
flag = new Set();
cache.set(obj, flag);
}
flag.add(key);

// Can't not delete the property of process.env before node@4
if (obj === process.env) {
obj[key] = method;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed this condition

Expand All @@ -41,13 +49,6 @@ var method = module.exports = function mockMethod(obj, key, method) {
value: method
});

// set a flag that checks if it is mocked
var flag = cache.get(obj);
if (!flag) {
flag = new Set();
cache.set(obj, flag);
}
flag.add(key);
};

/**
Expand All @@ -60,8 +61,14 @@ method.restore = function restoreMocks() {
// delete the mock key, use key on the prototype
delete m.obj[m.key];
} else {
// redefine the origin key instead of the mock key
Object.defineProperty(m.obj, m.key, m.descriptor);
// can't redefine process.env when node<4
// https://github.com/nodejs/node/pull/2999
if (m.obj === process.env) {
m.obj[m.key] = m.descriptor.value;
} else {
// redefine the origin key instead of the mock key
Object.defineProperty(m.obj, m.key, m.descriptor);
}
}
}
mocks = [];
Expand Down
11 changes: 11 additions & 0 deletions test/method-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('Mock property', function () {
enableCache: true,
delay: 10
};
var home = process.env.HOME;

afterEach(muk.restore);

Expand All @@ -91,21 +92,25 @@ describe('Mock property', function () {
it('Property are new after mocked', function () {
muk(config, 'enableCache', false);
muk(config, 'delay', 0);
muk(process.env, 'HOME', '/mockhome');

assert.equal(config.enableCache, false, 'enableCache is false');
assert.equal(config.delay, 0, 'delay is 0');
assert.equal(process.env.HOME, '/mockhome', 'process.env.HOME is /mockhome');
});

it('Should have original properties after muk.restore()', function () {
muk(config, 'enableCache', false);
muk(config, 'enableCache', false);
muk(config, 'delay', 0);
muk(process.env, 'HOME', '/mockhome');
muk(config, 'notExistProp', 'value');
muk(process.env, 'notExistProp', 0);
muk.restore();

assert.equal(config.enableCache, true, 'enableCache is true');
assert.equal(config.delay, 10, 'delay is 10');
assert.equal(process.env.HOME, home, 'process.env.HOME is ' + home);
assert(!hasOwnProperty(config, 'notExistProp'), 'notExistProp is deleted');
assert(!hasOwnProperty(process.env, 'notExistProp'), 'notExistProp is deleted');
});
Expand Down Expand Up @@ -224,6 +229,12 @@ describe('Mock check', function() {
muk(obj, 'e', 2);
assert.ok(muk.isMocked(obj, 'e'));
});

it('should check process.env', function() {
muk(process.env, 'HOME', '/mockhome');
assert.equal(process.env.HOME, '/mockhome');
assert.ok(muk.isMocked(process.env, 'HOME'));
});
});

function hasOwnProperty(obj, prop) {
Expand Down