chai-fs icon indicating copy to clipboard operation
chai-fs copied to clipboard

.to.not.be.a.file() not working

Open stefanwalther opened this issue 9 years ago • 6 comments

.to.not.be.a.file() does not work, returns true, even if the file is not existing, example:

expect( path.join( __dirname, './tmp/lessReduce/root.css' ) ).to.not.be.a.file();

... but the file is definitely not there ... but throws an asserting exception.

stefanwalther avatar Jun 19 '15 22:06 stefanwalther

Same issue here except using format:

assert.notIsFile(filePath, "Message");

This results in the error:

 value: expected 'file/path/file.txt' to exist
  AssertionError: value: expected 'filePath/file.txtt' to exist
      at Assertion.<anonymous> (node_modules/chai-fs/lib/assertions/file.js:21:53)
      at Assertion.ctx.(anonymous function) [as file] (node_modules/chai/lib/chai/utils/addMethod.js:40:25)
      at Function.assert.notIsFile (node_modules/chai-fs/lib/assertions/file.js:37:39)
      at Context.<anonymous> (test/file.js:68:16)

...I did some digging and everything seems to be in order; however, it appears that somewhere along the line the method is mapped to the isFile method (or some other method that expects it to exists as opposed to not exist).

Cheers!

Kyle

ktyacke avatar Jan 23 '16 00:01 ktyacke

I am also experiencing this issue:

expect('./docs/js/angular.min.js').to.not.be.a.file();

throws error:

Uncaught AssertionError: value: expected './docs/js/angular.min.js' to exist

Any ideas? :(

embpdaniel avatar Apr 22 '16 18:04 embpdaniel

@stefanwalther @ktyacke @embpdaniel

After reviewing the code and associated tests, it appears to me that this behavior was intentional.

expect(<path>).to.not.be.a.file() first asserts that the given path exists. Only if it exists, does it continue on to assert that the given path isn't a file.

That's why you're getting the message "expected path to exist" when you're asserting a non-existent path .to.not.be.a.file().

Therefore, in most cases, what you probably want to do is change your assertion to:

expect(<path>).to.not.be.a.path();

There doesn't appear to be a single assertion that allows you to test that a given path either doesn't exist or it does exist but isn't a file. Personally, I probably would've preferred .to.not.be.a.file() to have this dual effect, but at this point, in order to not break backward compatibility, it seems such a feature would need to be a new assertion as opposed to a modification to the existing one.

meeber avatar Jun 05 '16 02:06 meeber

Holy hell, I've been fighting with this all day What a stupid function that clearly does not behave as most would intend.

At least I can confirm @meeber solution works.

justinireland avatar Jul 21 '16 22:07 justinireland

I hit this also. Maybe docs might want to note this behavior :)

dman777 avatar May 09 '17 20:05 dman777

Goodness. I've also just spent the best part of the last hour trying to figure out why my code was broken -- when it's not. It's this flipping assertion.

Yes, @meeber's fix works, but a path which doesn't exist is not a file (or directory). This function really should not fail for a non-existent path!

fluffynuts avatar Apr 09 '18 11:04 fluffynuts