All files cerba_test.ts

100% Statements 101/101
100% Branches 2/2
100% Functions 16/16
100% Lines 81/81

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 1341x 1x 1x 1x 1x 1x 1x 76x   1x 1x 1x 10x                               10x 1x 1x 2x 1x 1x 1x 1x             1x 1x 1x 1x   1x 1x 1x 2x     1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x   1x 1x 1x 2x     1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                  
import * as mock from 'mock-fs';
import * as path from 'path';
import * as fs from 'fs-extra';
import {Cerba} from './cerba';
import {deepStrictEqual, rejects, strictEqual} from 'assert';
import {CerbaPackage} from './cerba_package';
const cwd = process.cwd();
const here = (filename: string) => path.join(cwd, filename);
 
const exampleContent = ['import "path"', 'import "lodash"'].join('\n');
describe('Cerba', () => {
  beforeEach(() =>
    mock({
      [here('package.json')]: JSON.stringify({dependencies: {lodash: '1.0.0', url: '1.0.0'}}),
      [here('src/example.ts')]: exampleContent,
      [here('has-config/cerba.json')]: JSON.stringify({
        packages: [
          {
            name: 'cerba',
            description: 'Example description',
            main: './src/example.ts',
          },
        ],
      }),
      [here('has-config/package.json')]: JSON.stringify({dependencies: {lodash: '1.0.0', url: '1.0.0'}}),
      [here('has-config/src/example.ts')]: exampleContent,
    })
  );
  afterEach(() => mock.restore());
  it('should build main package', async () => {
    const cerba = new Cerba({main: './src/example.ts'});
    await cerba.build();
    const file = fs.readFileSync(here('./packages/example/src/example.ts'), 'utf8');
    const pack = JSON.parse(fs.readFileSync(here('./packages/example/package.json'), 'utf8'));
    strictEqual(exampleContent, file);
    deepStrictEqual(pack, {
      name: 'example',
      main: './src/example.js',
      dependencies: {lodash: '1.0.0'},
      version: '1.0.0',
    });
  });
  it('should pull from packages', async () => {
    const cerba = new Cerba({main: './src/example.ts'});
    const pack = await cerba.findPackage('example');
    strictEqual(pack instanceof CerbaPackage, true);
  });
  it('should throw error when cannot pull from packages', async () => {
    const cerba = new Cerba({main: './src/example.ts'});
    rejects(async () => {
      await cerba.findPackage('doug');
    });
  });
  it('should throw error when cannot pull from packages', async () => {
    const cerba = new Cerba({cwd: './has-config'});
    const pkg = await cerba.findPackage('cerba');
    strictEqual(pkg.name, 'cerba');
  });
  it('should get all packages', async () => {
    const cerba = new Cerba({cwd: './has-config'});
    strictEqual((await cerba.packages).length, 1);
    strictEqual((await cerba.packages).length, 1);
  });
  it('should build package', async () => {
    const cerba = new Cerba({cwd: './has-config'});
    await cerba.buildPackage('cerba');
    deepStrictEqual(fs.readdirSync(here('')), ['has-config', 'package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config')), ['cerba.json', 'package.json', 'packages', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/src')), ['example.ts']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages')), ['cerba']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba')), ['package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba/src')), ['example.ts']);
    const file = fs.readFileSync(here('./has-config/packages/cerba/src/example.ts'), 'utf8');
    strictEqual(file, exampleContent);
    const pack = JSON.parse(fs.readFileSync(here('./has-config/packages/cerba/package.json'), 'utf8'));
    deepStrictEqual(pack, {
      name: 'cerba',
      description: 'Example description',
      main: './src/example.js',
      dependencies: {lodash: '1.0.0'},
      version: '1.0.0',
    });
  });
  it('should create with no props', async () => {
    new Cerba();
  });
  it('should not build if no name', async () => {
    const cerba = new Cerba();
    rejects(async () => {
      await cerba.buildPackage();
    });
  });
  it('should build package via prop', async () => {
    const cerba = new Cerba({cwd: './has-config', name: 'cerba'});
    await cerba.buildPackage();
    deepStrictEqual(fs.readdirSync(here('')), ['has-config', 'package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config')), ['cerba.json', 'package.json', 'packages', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/src')), ['example.ts']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages')), ['cerba']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba')), ['package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba/src')), ['example.ts']);
    const file = fs.readFileSync(here('./has-config/packages/cerba/src/example.ts'), 'utf8');
    strictEqual(file, exampleContent);
    const pack = JSON.parse(fs.readFileSync(here('./has-config/packages/cerba/package.json'), 'utf8'));
    deepStrictEqual(pack, {
      name: 'cerba',
      description: 'Example description',
      main: './src/example.js',
      dependencies: {lodash: '1.0.0'},
      version: '1.0.0',
    });
  });
  it('should build name through all', async () => {
    const cerba = new Cerba({cwd: './has-config', name: 'cerba'});
    await cerba.build();
    deepStrictEqual(fs.readdirSync(here('')), ['has-config', 'package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config')), ['cerba.json', 'package.json', 'packages', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/src')), ['example.ts']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages')), ['cerba']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba')), ['package.json', 'src']);
    deepStrictEqual(fs.readdirSync(here('has-config/packages/cerba/src')), ['example.ts']);
    const file = fs.readFileSync(here('./has-config/packages/cerba/src/example.ts'), 'utf8');
    strictEqual(file, exampleContent);
    const pack = JSON.parse(fs.readFileSync(here('./has-config/packages/cerba/package.json'), 'utf8'));
    deepStrictEqual(pack, {
      name: 'cerba',
      description: 'Example description',
      main: './src/example.js',
      dependencies: {lodash: '1.0.0'},
      version: '1.0.0',
    });
  });
});