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

Allow tests to tamper with Node.js built-ins without breaking AVA #1359

Closed
sindresorhus opened this issue Apr 16, 2017 · 10 comments
Closed

Allow tests to tamper with Node.js built-ins without breaking AVA #1359

sindresorhus opened this issue Apr 16, 2017 · 10 comments
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt enhancement new functionality help wanted scope:test-environment

Comments

@sindresorhus
Copy link
Member

sindresorhus commented Apr 16, 2017

Issuehunt badges

With latest master (c01ac05) and Node.js 4 and 7.

import fs from 'fs';
import test from 'ava';
import sinon from 'sinon';

test(t => {
	fs.readFileSync = sinon.stub(fs, 'readFileSync');
	// ...
	fs.readFileSync.restore();
	t.pass();
});
  2 exceptions

  Uncaught Exception
  TypeError: Cannot read property 'sections' of undefined
    new SourceMapConsumer (/Users/sindresorhus/dev/ava/node_modules/source-map/lib/source-map-consumer.js:20:19)
    mapSourcePosition (/Users/sindresorhus/dev/ava/node_modules/source-map-support/source-map-support.js:171:14)
    wrapCallSite (/Users/sindresorhus/dev/ava/node_modules/source-map-support/source-map-support.js:338:20)
    /Users/sindresorhus/dev/ava/node_modules/source-map-support/source-map-support.js:373:26
    Function.prepareStackTrace (/Users/sindresorhus/dev/ava/node_modules/source-map-support/source-map-support.js:372:24)

  ✖ Test results were not received from test.js

I think we need to protect all builtins from being overridden, so mocking in the tests doesn't affect our usage or our dependencies' usage of core APIs. Maybe running the test files in a new vm context would help with that?

There is a $60.00 open bounty on this issue. Add more on Issuehunt.

@sindresorhus
Copy link
Member Author

Same without Sinon actually:

import fs from 'fs';
import test from 'ava';

test(t => {
	const _ = fs.readFileSync;
	fs.readFileSync = () => 'foo';
	const isFoo = require('.');
	fs.readFileSync = _;
	t.true(isFoo);
});
  2 exceptions

  Uncaught Exception
  SyntaxError: Unexpected token L in JSON at position 0
    SyntaxError: Unexpected token L in JSON at position 0
        JSON.parse (<anonymous>)
        new SourceMapConsumer (node_modules/source-map/lib/source-map-consumer.js:17:22)
        mapSourcePosition (node_modules/source-map-support/source-map-support.js:171:14)
        wrapCallSite (node_modules/source-map-support/source-map-support.js:338:20)
        node_modules/source-map-support/source-map-support.js:373:26
        Function.prepareStackTrace (node_modules/source-map-support/source-map-support.js:372:24)

  ✖ Test results were not received from test.js

@novemberborn novemberborn changed the title Sinon stubbing causes source map error Allow tests to tamper with Node.js built-ins without breaking AVA Apr 17, 2017
@OmgImAlexis
Copy link

Assuming this is related then.

$ nyc ava 

  1 passed
  1 rejection

  Unhandled Rejection
  SyntaxError
    XMLHttpRequest.open (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:486:15)
    dispatchXhrRequest (node_modules/axios/lib/adapters/xhr.js:45:13)
    xhrAdapter (node_modules/axios/lib/adapters/xhr.js:12:10)
    dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:52:10)

@sindresorhus
Copy link
Member Author

@OmgImAlexis Not necessarily. Doesn't look like that error is coming from AVA or a dependent.

@OmgImAlexis
Copy link

Going off the warning from c01ac05 I had assumed it was related. If not feel free to remove my comments.

@pendar747
Copy link

I seem to get a similar error whenever I change fs.readFileSync!

   Uncaught Exception
  TypeError: Cannot read property 'sections' of undefined
    new SourceMapConsumer (node_modules/source-map/lib/source-map-consumer.js:20:19)
    mapSourcePosition (node_modules/source-map-support/source-map-support.js:175:14)
    wrapCallSite (node_modules/source-map-support/source-map-support.js:343:20)
    node_modules/source-map-support/source-map-support.js:378:26
    Function.prepareStackTrace (node_modules/source-map-support/source-map-support.js:377:24)

  × Test results were not received from dist\test\current\asset.spec.js

@mrozbarry
Copy link

I'm not 100% sure if this is related, but in ava 1.2.x with sinon 7.2.3, when I create a sandbox with mocked timers, I get an error that clearTimeout does not exist. I'm willing to open a new issue if this is unrelated, but it seems like it could be.

Ava 1.1.0 is not affected, so I think this has a specific issue with 1.2.0's clearTimeout methods. I'm not certain on the interaction with sinon's sandbox, though.

@novemberborn
Copy link
Member

in ava 1.2.x with sinon 7.2.3, when I create a sandbox with mocked timers, I get an error that clearTimeout does not exist. I'm willing to open a new issue if this is unrelated, but it seems like it could be.

If you could open a new issue that'd be great. This issue is more about changing the fs module etc.

@novemberborn
Copy link
Member

@mrozbarry this should be fixed with #2057.

@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt This issue has been funded on Issuehunt label May 10, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $60.00 to this issue.


@novemberborn
Copy link
Member

IMHO achieving this kind of isolation will have such an overhead as to not be worthwhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💵 Funded on Issuehunt This issue has been funded on Issuehunt enhancement new functionality help wanted scope:test-environment
Projects
None yet
Development

No branches or pull requests

6 participants