Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to delete things from global. #3152

Closed
gdborton opened this issue Mar 15, 2017 · 6 comments
Closed

Unable to delete things from global. #3152

gdborton opened this issue Mar 15, 2017 · 6 comments

Comments

@gdborton
Copy link
Contributor

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
delete global.someProp doesn't work.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Repl.it - https://repl.it/GXd3

const assert = require('assert');

describe('removing stuff from the global object', () => {
  describe(`via 'delete global.prop'`, () => {
    it('should set the value to undefined', () => {
      global.someValue = 'asdf';
      assert.equal(global.someValue, 'asdf');
      delete global.someValue;
      assert.equal(global.someValue, undefined);
    });
  });

  describe(`via 'global.prop = undefined;'`, () => {
    it('should set the value to undefined', () => {
      global.someValue2 = 'asdf';
      assert.equal(global.someValue2, 'asdf');
      global.someValue2 = undefined;
      assert.equal(global.someValue2, undefined);
    });
  });
});

What is the expected behavior?
delete global.someProp should make global.someProp === undefined true.

This works in mocha, jasmine, plain old node, and I think it works when running via vm. We're investigating a move to Jest, but are running into some subtle issues related to this.

For example this function in enzyme doesn't behave correctly - https://github.com/airbnb/enzyme/blob/master/src/Utils.js#L213

We could attempt to work around this, but I feel like this will repeatedly come up in our codebase and will lead to lots of wasted time.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
node - v4.6.2
npm - 2.15.11
jest - v19.0.0
os - OSX Sierra v10.12.1

@rogeliog
Copy link
Contributor

Hi! Thanks for reporting this

I think it has to do with vm, which is what Jest uses for the environment https://github.com/facebook/jest/blob/master/packages/jest-environment-node/src/index.js

This is what I get when running it in a Node repl
screen shot 2017-03-17 at 4 03 32 pm

@aaronabramov
Copy link
Contributor

looks like it has been fixed here nodejs/node#11266

@aaronabramov
Copy link
Contributor

aaronabramov commented Mar 17, 2017

yeah. i can confirm that it works on node 7.7.3

dabramov-mbp ~ node -v
v7.7.3
dabramov-mbp ~ node
> const sb = {a: 1, b: 2, c: 3}
undefined
> vm.createContext(sb)
{ a: 1, b: 2, c: 3 }
> vm.runInContext('delete this.a;', sb)
true
> sb
{ b: 2, c: 3 }

@gdborton
Copy link
Contributor Author

Ooph, so we'll have to update node to get around this?

@aaronabramov
Copy link
Contributor

@gdborton that would be the easiest!

@cpojer cpojer closed this as completed Mar 18, 2017
gdborton added a commit to gdborton/enzyme that referenced this issue Mar 23, 2017
Except in the most recent versions of node, developers are unable to delete
things from global when running code in a node vm.  This means that enzyme is
failing to clean up after itself when temporarily defining global.document for
setState calls.

This papers over the issue by explicitly setting global.document to undefined
if it failed to be deleted.

Relevant jest issue - jestjs/jest#3152

Relevant node pr - nodejs/node#11266
gdborton added a commit to gdborton/enzyme that referenced this issue Mar 24, 2017
Except in the most recent versions of node, developers are unable to delete
things from global when running code in a node vm.  This means that enzyme is
failing to clean up after itself when temporarily defining global.document for
setState calls.

This papers over the issue by explicitly setting global.document to undefined
before attempting to delete. If the delete succeeds, the undefined value will
be removed.

Relevant jest issue - jestjs/jest#3152

Relevant node pr - nodejs/node#11266
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants