All files writable_file_test.ts

100% Statements 55/55
100% Branches 1/1
100% Functions 10/10
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 511x 1x 1x 1x 1x 1x 10x   1x 1x 6x       1x 1x 1x   1x 1x 2x 1x 1x     1x 1x 1x 2x     1x 1x 2x 1x 1x   1x 1x 2x 1x 1x   1x 1x 2x 1x 1x      
import * as path from 'path';
import * as mock from 'mock-fs';
import {strictEqual, rejects} from 'assert';
import {WritableFile} from './writable_file';
import * as fs from 'fs-extra';
const cwd = process.cwd();
const here = (filename: string) => path.join(cwd, filename);
 
describe('WritableFile', () => {
  beforeEach(() =>
    mock({
      [here('hello.txt')]: 'hello world',
    })
  );
  it('should get content', async () => {
    const p = new WritableFile({basename: './hello.txt'});
    strictEqual(await p.content, 'hello world');
  });
  it('should write', async () => {
    const p = new WritableFile({basename: './hello.txt'});
    await p.writeFromString('meow meow');
    strictEqual(fs.readFileSync(here('hello.txt'), 'utf8'), 'meow meow');
    strictEqual(await p.content, 'meow meow');
  });
 
  it('should throw error during create if exists', async () => {
    const p = new WritableFile({basename: './hello.txt'});
    rejects(async () => {
      await p.createFromString('meow meow');
    });
  });
  it('should create', async () => {
    const p = new WritableFile({basename: './kitty.txt'});
    await p.createFromString('meow meow');
    strictEqual(fs.readFileSync(here('kitty.txt'), 'utf8'), 'meow meow');
    strictEqual(await p.content, 'meow meow');
  });
  it('should upsert', async () => {
    const p = new WritableFile({basename: './kitty.txt'});
    await p.upsertFromString('meow meow');
    strictEqual(fs.readFileSync(here('kitty.txt'), 'utf8'), 'meow meow');
    strictEqual(await p.content, 'meow meow');
  });
  it('should throw error during upsert if exists', async () => {
    const p = new WritableFile({basename: './hello.txt'});
    await p.upsertFromString('meow meow');
    strictEqual(fs.readFileSync(here('hello.txt'), 'utf8'), 'hello world');
    strictEqual(await p.content, 'hello world');
  });
});