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

Should be able to delete (fs.unlink) a file inside a Mocha test - test passes but file not deleted. why? #1058

Closed
nelsonic opened this issue Dec 4, 2013 · 6 comments

Comments

@nelsonic
Copy link

nelsonic commented Dec 4, 2013

A simple node filesystem test:

var chai   = require('chai');
var assert = chai.assert; 
var fs     = require('fs');

describe('Node.js Environment Checks', function(){
  describe('Basic IO', function(){
    it('CREATE (temporary) file tests create/write access to FS', function(){
        // setup
        var newFile = new Date().getTime() +".txt";

        fs.writeFile(newFile, "hello!", function (err) {
            if (err) console.log(err);
            // console.log("Created file: "+newFile);
            fs.readdir(__dirname, function(err, list) {
                // console.log(list)
                assert.isTrue(list.indexOf(newFile) > -1)
                fs.unlinkSync(newFile);
                console.log('successfully deleted '+newFile);
                // console.log("Deleted: "+newFile)
                fs.readdir(__dirname, function(err, list) {
                    if (err) throw err;
                    assert.isTrue(list.indexOf(newFile) === -1);
                });
            });
        });
    })
  })
}) // end node env checks

Am I doing something wrong?
It works fine outside of the Mocha test:

var fs = require('fs');

var newFile = new Date().getTime() +".txt";

fs.writeFile(newFile, "hello!", function (err) {
    if (err) console.log(err);
    // console.log("Created file: "+newFile);
    fs.readdir(__dirname, function(err, list) {
        // console.log(list)
        console.log(list.indexOf(newFile) > -1)
        fs.unlinkSync(newFile);
        console.log('successfully deleted '+newFile);
        // console.log("Deleted: "+newFile)
        fs.readdir(__dirname, function(err, list) {
            if (err) throw err;
            console.log(list.indexOf(newFile) === -1);
        });
    });
});

I've googled this extensively...
Any insight would be much appreciated.

@travisjeffery
Copy link
Contributor

you need to invoke the done callback with async calls like this

it('CREATE (temporary) file tests create/write access to FS', function(done){
        // setup
        var newFile = new Date().getTime() +".txt";

        fs.writeFile(newFile, "hello!", function (err) {
            if (err) console.log(err);
            // console.log("Created file: "+newFile);
            fs.readdir(__dirname, function(err, list) {
                // console.log(list)
                assert.isTrue(list.indexOf(newFile) > -1)
                fs.unlinkSync(newFile);
                console.log('successfully deleted '+newFile);
                // console.log("Deleted: "+newFile)
                fs.readdir(__dirname, function(err, list) {
                    if (err) throw err;
                    assert.isTrue(list.indexOf(newFile) === -1);
                    done()
                });
            });
        });
    })

@nelsonic
Copy link
Author

nelsonic commented Dec 4, 2013

@travisjeffery, thanks for your prompt reply.
I tried adding the done() callback to my test.
See: https://github.com/nelsonic/valens/blob/master/test/test.js [ line 76 ]
The file is still not being deleted ... :-(
Ideas?

@travisjeffery
Copy link
Contributor

https://github.com/nelsonic/valens/blob/master/test/test.js#L60 you don't have done as a parameter

@nelsonic
Copy link
Author

nelsonic commented Dec 5, 2013

Thanks for replying.
I've added the done parameter to the test.
https://github.com/nelsonic/valens/blob/master/test/test.js#L82
Now it fails. any idea why?
This works fine outside of mocha...
https://github.com/nelsonic/valens/blob/master/try.js

@tj
Copy link
Contributor

tj commented Dec 5, 2013

@nelsonic just glanced at it but __dirname is ./test, you're maybe expecting it to be ./ but __dirname is relative to the script not the CWD

@nelsonic
Copy link
Author

nelsonic commented Dec 6, 2013

Changed __dirname to process.cwd() and now tests pass (and temporary file is deleted).
https://travis-ci.org/nelsonic/valens/builds/15048090
_Thanks TJ_!

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

3 participants