serverless/test/unit/lib/utils/fs/read-file.test.js
SleepWithCoffee 5b8766159c
test: Refactor to async functions (#11832)
Co-authored-by: SleepWithCoffee <>
2023-03-20 10:35:06 +01:00

48 lines
1.6 KiB
JavaScript

'use strict';
const chai = require('chai');
const writeFile = require('../../../../../lib/utils/fs/write-file');
const readFile = require('../../../../../lib/utils/fs/read-file');
const { getTmpFilePath } = require('../../../../utils/fs');
// Configure chai
chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));
const expect = require('chai').expect;
describe('#readFile()', () => {
it('should read a file asynchronously', async () => {
const tmpFilePath = getTmpFilePath('anything.json');
return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});
it('should read a filename extension .yml', async () => {
const tmpFilePath = getTmpFilePath('anything.yml');
return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});
it('should read a filename extension .yaml', async () => {
const tmpFilePath = getTmpFilePath('anything.yaml');
return writeFile(tmpFilePath, { foo: 'bar' }).then(() =>
expect(readFile(tmpFilePath)).to.eventually.deep.equal({ foo: 'bar' })
);
});
it('should throw YAMLException with filename if yml file is invalid format', async () => {
const tmpFilePath = getTmpFilePath('invalid.yml');
return writeFile(tmpFilePath, ': a')
.then(() => readFile(tmpFilePath))
.catch((e) => {
expect(e.name).to.equal('YAMLException');
expect(e.message).to.match(new RegExp('.*invalid.yml'));
});
});
});