jeudi 30 juillet 2015

Issue with Mock Fs Reading Directory Symlink

The test below runs perfectly fine without mock and when the beforeEach and afterEach calls are added I get the corresponding errors below.

var path = require("path")
var chai = require("chai")
var expect = chai.expect

var mock = require("mock-fs")

var fs = require("fs")

describe("ensure-symlink", function(){
  beforeEach(function(){
    mock({
      "occupied-dir": {
        'foobar.txt': 'foobar\n',
      },
      'file.txt': 'file content\n',
      'empty-dir': {}
    })
  })
  afterEach(function() {
    mock.restore()
  })

  it("should create new directory symlink in root w/ resolve", function(){
    fs.symlinkSync(path.resolve("occupied-dir"), "empty-dir/symlink-dir", "dir")
    expect(fs.readdirSync("occupied-dir")).to.contain("foobar.txt")
    expect(fs.readdirSync("empty-dir")).to.contain("symlink-dir")
    expect(fs.lstatSync("empty-dir/symlink-dir").isSymbolicLink()).to.equal(true)
    expect(fs.readdirSync("empty-dir/symlink-dir")).to.contain("foobar.txt")
    expect(fs.readFileSync("empty-dir/symlink-dir/foobar.txt", "utf8")).to.equal("foobar\n")
  })
})

The line

expect(fs.readdirSync("empty-dir/symlink-dir")).to.contain("foobar.txt")

Throws Error: ENOTDIR, not a directory 'empty-dir/symlink-dir'

And the line

expect(fs.readFileSync("empty-dir/symlink-dir/foobar.txt", "utf8")).to.equal("foobar\n")

Throws this:

  TypeError: item.getItem is not a function
        at FileSystem.getItem (/Users/thomas/Desktop/untitled/node_modules/mock-fs/lib/filesystem.js:85:17)

So you can see the mock passes the first three expectations. It's just unable to open the directory and view the contents.

However without the mock, I can view the file and the contents of the symlink dir in finder and the test passes so also with node.js

What's happening here that's not allowing the mock to view the contents of the newly created symlink directory?

Aucun commentaire:

Enregistrer un commentaire