Skip to content

Commit

Permalink
Add simple test for fs-writable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Nov 14, 2018
1 parent e5c1280 commit 4862daa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fs-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class WriteStream extends Writable {

_write(data, cb) {
if (!(data instanceof Buffer)) {
const err = new Error();
const err = new ERR_INVALID_ARG_TYPE('data', 'Buffer', data);
return this.emit('error', err);
}

Expand Down
53 changes: 53 additions & 0 deletions test/fs-writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const fs = require('fs');
const metatests = require('metatests');
const WriteStream = require('../lib/fs-writable');


metatests.testSync('fs-writable / create', test => {
const TEST_FILENAME = './create';

new WriteStream(TEST_FILENAME);

test.throws(() => new WriteStream());
test.throws(() => new WriteStream(1));
test.throws(() => new WriteStream(true));

test.throws(() => new WriteStream(TEST_FILENAME, { start: -1 }));

fs.unlinkSync(TEST_FILENAME);
});

metatests.test('fs-writable / open', test => {
const TEST_FILENAME = './open';

test.plan(2);

const stream = new WriteStream(TEST_FILENAME);
stream.on('open', () => test.pass('open'));
stream.on('ready', () => test.pass('ready'));

test.on('done', () => fs.unlinkSync(TEST_FILENAME));
});

metatests.test('fs-writable / write', test => {
const TEST_FILENAME = './write';

test.plan(1);

const stream = new WriteStream(TEST_FILENAME);

stream.write('123');
stream.write('456');
stream.cork();
stream.write('7');
stream.write('8');
stream.uncork();
stream.end('9', () => {
const writeData = fs.readFileSync(TEST_FILENAME).toString();
fs.unlinkSync(TEST_FILENAME);
test.strictSame(writeData, '123456789');
});
});

0 comments on commit 4862daa

Please sign in to comment.