All files readme_test.ts

100% Statements 61/61
100% Branches 12/12
100% Functions 13/13
100% Lines 54/54

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 1081x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 14x 1x 14x   14x 392x 392x 392x 392x           14x 14x   14x 14x   28x 28x   2x   14x 2x   14x 12x           12x         1x 1x 1x 14x 1x 14x   14x 28x                   14x 392x 392x 392x 392x           14x 14x   14x   28x         14x 2x   14x 12x           12x            
import * as path from 'path';
import * as fs from 'fs';
import * as chai from 'chai';
import {ReadMe} from './readme';
import * as chaiJestSnapshot from 'chai-jest-snapshot';
import * as sinon from 'sinon';
chai.use(chaiJestSnapshot);
const {expect} = chai;
 
describe('ReadMe', () => {
  context('.update()', () => {
    const dir = path.join(__dirname, '../examples');
    const all = fs.readdirSync(dir);
    const examples = all.filter(e => e.match(/^error|^valid/));
    examples.forEach(example => {
      const full = path.join(dir, example);
 
      const prune = () => {
        const dir = fs.readdirSync(full);
        const removeReadme = dir.includes('.gitignore');
        try {
          if (removeReadme) fs.unlinkSync(path.join(full, 'README.md'));
        } catch {
          // noop
        }
      };
 
      beforeEach(prune);
      afterEach(prune);
 
      it(`should use example "${example}"`, async () => {
        let error = undefined;
        try {
          const process = {cwd: () => full};
          await ReadMe.update(process as NodeJS.Process);
        } catch (e) {
          error = e;
        }
        if (example.match(/^error/)) {
          expect(error).to.not.equal(undefined);
        }
        if (example.match(/^valid/)) {
          expect(
            fs.readFileSync(path.join(full, 'README.md'), 'utf-8')
          ).to.matchSnapshot(
            path.join(__dirname, 'snapshots', `${example}.js`),
            'use-example'
          );
          expect(error).to.equal(undefined);
        }
      });
    });
  });
  context('.cli()', () => {
    const dir = path.join(__dirname, '../examples');
    const all = fs.readdirSync(dir);
    const examples = all.filter(e => e.match(/^error|^valid/));
    examples.forEach(example => {
      const full = path.join(dir, example);
 
      const process = {
        cwd: () => full,
        stdout: {
          write: sinon.stub(),
        },
        stderr: {
          write: sinon.stub(),
        },
        exit: sinon.stub(),
      };
 
      const prune = () => {
        const dir = fs.readdirSync(full);
        const removeReadme = dir.includes('.gitignore');
        try {
          if (removeReadme) fs.unlinkSync(path.join(full, 'README.md'));
        } catch {
          // noop
        }
      };
 
      beforeEach(prune);
      afterEach(prune);
 
      it(`should use example "${example}"`, async () => {
        try {
          await ReadMe.cli((process as unknown) as NodeJS.Process);
        } catch (e) {
          // noop
        }
 
        if (example.match(/^error/)) {
          expect(process.exit.args).to.deep.equal([[1]]);
        }
        if (example.match(/^valid/)) {
          expect(
            fs.readFileSync(path.join(full, 'README.md'), 'utf-8')
          ).to.matchSnapshot(
            path.join(__dirname, 'snapshots', `${example}.js`),
            'use-example'
          );
          expect(process.exit.args).to.deep.equal([[0]]);
        }
      });
    });
  });
});