All files require_check_test.ts

100% Statements 61/61
100% Branches 8/8
100% Functions 15/15
100% Lines 50/50

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x 1x 1x   1x                     1x   1x 17x                                   1x 1x 1x       1x 1x 1x             1x 1x 1x       1x 1x 1x             1x 1x 1x 7x 1x 7x 7x 7x   14x   6x   7x 7x         1x 1x 1x 7x 1x 7x 7x   7x                       14x       7x 6x   7x 1x         1x 1x                       2x       1x        
import * as sinon from 'sinon';
import {expect} from 'chai';
import {RequireCheck} from './require_check';
import * as fs from 'fs';
import * as path from 'path';
 
const lodashStdout = [
  '+ lodash@4.17.19\n',
  'updated 1 package and audited 522 packages in 4.735s\n',
  '\n',
  '57 packages are looking for funding\n',
  '  run `npm fund` for details\n',
  '\n',
  'found 0 vulnerabilities\n',
  '\n',
].join('');
 
describe('RequireCheck', () => {
  let execStub: sinon.SinonStub;
  afterEach(() => {
    sinon.restore();
  });
 
  // context('sinon', () => {
  //   it('should properly stub the method', () => {
  //     let value = false;
  //     class Example {
  //       static duck() {
  //         value = true;
  //         return true;
  //       }
  //     }
  //     sinon.stub(Example, 'duck').returns(false);
  //     expect(Example.duck()).to.equal(false);
  //     expect(value).to.equal(false);
  //   });
  // });
 
  context('.npmInstall()', () => {
    it('should fire exec', async () => {
      execStub = sinon.stub(RequireCheck, 'exec').resolves({
        stdout: lodashStdout,
        stderr: '',
      });
      const result = await RequireCheck.npmInstall('lodash', './');
      expect(result).to.deep.equal({stdout: lodashStdout, stderr: ''});
      expect(execStub.args[0]).to.deep.equal([
        'npm install lodash',
        {cwd: './'},
      ]);
    });
  });
 
  context('.requireModule()', () => {
    it('should fire exec', async () => {
      execStub = sinon.stub(RequireCheck, 'exec').resolves({
        stdout: '',
        stderr: '',
      });
      const result = await RequireCheck.requireModule({name: 'url'}, './');
      expect(result).to.deep.equal({stdout: '', stderr: ''});
      expect(execStub.args[0]).to.deep.equal([
        'node -e "require(\'url\')"',
        {cwd: './'},
      ]);
    });
  });
 
  context('.check()', () => {
    const dir = path.join(__dirname, '../examples');
    const all = fs.readdirSync(dir);
    const examples = all.filter(e => e.match(/^error|^valid/));
    examples.forEach(example => {
      it(`should use example "${example}"`, async () => {
        const full = path.join(dir, example);
        let error = undefined;
        try {
          await RequireCheck.check(full);
        } catch (e) {
          error = e;
        }
        if (example.match('^error')) expect(error).to.not.equal(undefined);
        if (example.match('^valid')) 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 => {
      it(`should use example "${example}"`, async () => {
        const full = path.join(dir, example);
 
        const process = {
          argv: ['skip', 'skip', full],
          stdout: {
            write: sinon.stub(),
          },
          stderr: {
            write: sinon.stub(),
          },
          exit: sinon.stub(),
        };
 
        try {
          await RequireCheck.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(process.exit.args).to.deep.equal([[0]]);
        }
      });
    });
 
    it('should throw error if no location is passed in', async () => {
      const process = {
        argv: ['skip', 'skip'],
        stdout: {
          write: sinon.stub(),
        },
        stderr: {
          write: sinon.stub(),
        },
        exit: sinon.stub(),
      };
 
      try {
        await RequireCheck.cli((process as unknown) as NodeJS.Process);
      } catch (e) {
        // noop
      }
      expect(process.exit.args).to.deep.equal([[1]]);
    });
  });
});