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

Running with tests that don't use mockfs #103

Open
jamchamb opened this issue Apr 6, 2016 · 9 comments
Open

Running with tests that don't use mockfs #103

jamchamb opened this issue Apr 6, 2016 · 9 comments

Comments

@jamchamb
Copy link

jamchamb commented Apr 6, 2016

I had some issues running unit tests (in mocha) with a mock-fs setup for a module that was also included in other tested modules, whose tests did not make use of mock-fs. This would only happen if the tests that did not use mock-fs ran first - so it seemed there was an issue with non-overridden modules being cached.

My solution was to add this to the top of test files that used mock-fs:

// Clear require cache to make sure mock-fs works
Object.keys(require.cache).forEach(function (key) {delete require.cache[key];}); 

I'm wondering if there's a better way to do this, or if it would be helpful to add this to the documentation?

@goliney
Copy link
Contributor

goliney commented Jun 6, 2016

I have faced the same problem. Would love to see elegant way to get round.

@tschaub
Copy link
Owner

tschaub commented Jun 6, 2016

It would be useful to see a simple example that reproduces the issue.

@goliney
Copy link
Contributor

goliney commented Jun 7, 2016

Here is an example that reproduces the issue: https://github.com/goliney/mock-fs-103
Long story short: it is reproducible with fs-extra. With standard fs everything works fine.

/* A.js - runs first */
const expect = require('chai').expect;
//const mock = require('mock-fs');      //uncommenting this line solves the problem
const fs = require('fs-extra');

describe('Dummy test A', function () {
    it('should pass', function () {
        expect(true).to.be.true;
    });
});
/* B.js - runs after */
const expect = require('chai').expect;
const mock = require('mock-fs');
const fs = require('fs-extra');

describe('Dummy test B', function () {
    before(function () {
        mock({
            folder: {}
        });
    });

    after(function () {
        mock.restore();
    });

    it('should read mocked directory', function () {
        var content = fs.readdirSync('folder');
        expect(content).to.be.an.array;
    });
});

image

@tschaub
Copy link
Owner

tschaub commented Jul 9, 2016

Thanks for the report @jamchamb and for the excellent example setup @goliney.

I've adapted @jamchamb's require cache clearing suggestion in 51bc0fb and pulled in @goliney's example as regression tests in 97c89a5.

I'll cut a release with those changes if tests pass everywhere.

@tschaub
Copy link
Owner

tschaub commented Jul 9, 2016

Looks like fs-extra breaks Node 0.8 support. We could drop Node 0.8 and publish a new major version or write a regression test that doesn't require fs-extra. I'll take a shot at the latter.

@tschaub
Copy link
Owner

tschaub commented Jul 9, 2016

Ok. That got boring. I pushed another change to stop running tests on Node 0.8.

@tschaub tschaub reopened this Nov 1, 2016
@tschaub
Copy link
Owner

tschaub commented Nov 1, 2016

I've reverted #141 since it introduced new problems. Will need to revisit this.

@kevincaradant
Copy link

Hi @tschaub,

I met the same problem, every files can't be found with fs-extra. Do you any news about it ? :)

thank you

@LeoK80
Copy link

LeoK80 commented Jun 26, 2018

testing with jest I've been able to isolate mock-fs to a describe block and simply call mock() in the beforeEach() section and mock.restore() in the afterEach() section for the tests within the describe block using mock-fs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants