kevinbeaty / fs-promise

[DEPRECATED] Use mz or fs-extra^3 with Promise support
https://www.npmjs.com/package/fs-extra
MIT License
170 stars 13 forks source link

async works with readJson but not with readFile #16

Closed kristianmandrup closed 7 years ago

kristianmandrup commented 8 years ago

In the following, the last test fails, using await fs.readFile.

Error: ENOENT: no such file or directory, open './details.html'

However using await fs.readJsonworks just fine. Any idea as to why? If I change the readJson to readFile it also fails there:

Error: ENOENT: no such file or directory, open './map.json'

require('./helper');
const fs = require('fs-promise');
const path = require('path');
const mock = require('mock-fs');

const expect = require('chai').expect;

describe('FileIO', () => {
  before(() => {
    mock({
      'map.json': `
        {
          "ui": {
            "vue": {
              "path": "./ui/vue"
            }            
          }
        }
      `,
      'details.html': `<template><h1>{{message}}</h1></template>`
    });    
  })

  after(() => {
    mock.restore();
  })    

  describe('map.json', () => {            
    it('should contain path to ui files', async () => {   
      let result = await fs.readJson('./map.json');          
      console.log('result', result);             
      expect(result.ui.vue.path).to.eql('./ui/vue');
    });
  });

  describe('ui template file', () => {            
    it('should contain template', () => {   
      let result = fs.readFileSync('./details.html', 'utf8');          
      console.log('result', result);             
      expect(result).to.match(/message/);
    });
  });

  describe('ui template file - async', () => {            
    it('should contain template', async () => {   
      let result = await fs.readFile('./details.html', 'utf8');          
      console.log('result', result);             
      expect(result).to.match(/message/);
    });
  });
});
kristianmandrup commented 8 years ago

I also tried using bluebird and fs-readfile-promise to no avail.

const Promise = require('bluebird');
const fs = require('fs-promise');
const readFile = require('fs-readfile-promise');

I was only able to make it work after many trial and errors:

var Bluebird = require('bluebird');
var fs = require('fs-extra-promise').usePromise(Bluebird);

let result = await fsp.readFileAsync('./details.html', 'utf8');

Kind of sad having to do this in autumn 2016...

RyanZim commented 7 years ago

Works fine for me with the latest version: https://runkit.com/57ebc041d4a27f140028fd78/5895e235e6ac1b00142b9028

If this issue still persists, please post a reduced test case as a runkit or github repo. Closing for now.