question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Issuehunt badges

With latest master (c01ac05ef610c87f5c264b1a3eb80f89b4fc0832) 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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
novemberborncommented, Mar 3, 2019
0reactions
novemberborncommented, May 25, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started with Node.js Unit Testing - KnowledgeHut
Node.js unit testing is a built-in assert module that allows you to test a condition and throw an error if it does not...
Read more >
How To Test a Node.js Module with Mocha and Assert
Running tests consistently after writing new code ensures that new changes don't break pre-existing features. This gives the developer ...
Read more >
Crypto | Node.js v19.3.0 Documentation
setAuthTag() method is used to pass in the received authentication tag. If no tag is provided, or if the cipher text has been...
Read more >
Node.js Unit Testing: Get Started Quickly With Examples
Unit testing improves your code's quality and confidence. Find bugs early on in the software development lifecycle with a Node.js unit test.
Read more >
AVA unit tests fails passed Javascript test specs - Stack Overflow
AVA does not support .mjs out of the box, but it looks like you figured out the configuration. For the test script, just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found