Skip to content

Instantly share code, notes, and snippets.

@sudo-suhas
Last active August 13, 2017 10:30
Show Gist options
  • Select an option

  • Save sudo-suhas/3d84fa23ae4cba5700c916868fe7fa38 to your computer and use it in GitHub Desktop.

Select an option

Save sudo-suhas/3d84fa23ae4cba5700c916868fe7fa38 to your computer and use it in GitHub Desktop.

Revisions

  1. sudo-suhas revised this gist Aug 13, 2017. 3 changed files with 113 additions and 105 deletions.
    30 changes: 16 additions & 14 deletions jest-report.md
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,26 @@
    ```
    PASS test\successful-files.test.js
    cosmiconfig.load
    √ loads defined JSON config path in sync (6ms)
    √ loads defined JSON config path in async (8ms)
    √ loads defined YAML config path in sync (2ms)
    √ loads defined YAML config path in async (8ms)
    √ loads defined JS config path in sync (1ms)
    √ loads defined JS config path in async (4ms)
    √ loads modularized JS file in sync (1ms)
    √ loads modularized JS file in async (9ms)
    √ runs transform in sync (1ms)
    √ runs transform in async (5ms)
    √ transform errors not swallowed in sync (1ms)
    √ transform errors not swallowed in async (5ms)
    sync
    √ loads defined JSON config path (4ms)
    √ loads defined YAML config path (1ms)
    √ loads defined JS config path (2ms)
    √ loads modularized JS file (2ms)
    √ runs transform (5ms)
    √ does not swallow transform errors (1ms)
    async
    √ loads defined JSON config path (4ms)
    √ loads defined YAML config path (2ms)
    √ loads defined JS config path (2ms)
    √ loads modularized JS file (3ms)
    √ runs transform (2ms)
    √ does not swallow transform errors (1ms)
    Test Suites: 1 passed, 1 total
    Tests: 12 passed, 12 total
    Snapshots: 0 total
    Time: 0.276s, estimated 1s
    Ran all test suites.
    Time: 0.27s, estimated 1s
    Ran all test suites matching /success/.
    Watch Usage: Press w to show more.
    ```
    137 changes: 46 additions & 91 deletions test\successful-files.test.js
    Original file line number Diff line number Diff line change
    @@ -1,110 +1,65 @@
    'use strict';

    const isPromise = require('is-promise');

    const cosmiconfig = require('..');
    const util = require('./util');

    const absolutePath = util.absolutePath;
    const configFileLoader = util.configFileLoader;
    const testResolves = util.testResolves;
    const testRejects = util.testRejects;

    function testSyncAndAsync(name, testFn, args) {
    it(`${name} in sync`, testFn.apply(null, [true].concat(args)));

    it(`${name} in async`, testFn.apply(null, [false].concat(args)));
    }

    function testThrows(name, throwFn, errMsg) {
    it(`${name} in sync`, () => {
    expect(() => throwFn(true)).toThrow(errMsg);
    });

    it(`${name} in async`, () => {
    expect.assertions(1);
    return throwFn(false).catch(err => {
    expect(err.message).toMatch(errMsg);
    describe('cosmiconfig.load', () => {
    const expectRes = file => result => {
    expect(result.config).toEqual({
    foo: true,
    });
    });
    }

    function makeFileTest(sync, file) {
    const filePath = absolutePath(file);
    return function fileTest() {
    const loadConfig = cosmiconfig(null, { sync }).load;

    return execAndCheck(
    () => loadConfig(null, filePath),
    result => {
    expect(result.config).toEqual({
    foo: true,
    });
    expect(result.filepath).toBe(filePath);
    }
    );
    expect(result.filepath).toBe(absolutePath(file));
    };
    }

    function execAndCheck(execFn, checkFn) {
    const result = execFn();

    return isPromise(result) ? result.then(checkFn) : checkFn(result);
    }

    describe('cosmiconfig.load', () => {
    testSyncAndAsync(
    'loads defined JSON config path',
    makeFileTest,
    'fixtures/foo.json'
    );

    testSyncAndAsync(
    'loads defined YAML config path',
    makeFileTest,
    'fixtures/foo.yaml'
    );

    testSyncAndAsync(
    'loads defined JS config path',
    makeFileTest,
    'fixtures/foo.js'
    );
    ['json', 'yaml', 'js'].forEach(format => {
    const file = `fixtures/foo.${format}`;
    testResolves(
    `loads defined ${format.toUpperCase()} config path`,
    configFileLoader,
    [file],
    expectRes(file)
    );
    });

    testSyncAndAsync(
    testResolves(
    'loads modularized JS file',
    makeFileTest,
    'fixtures/foo-module.js'
    configFileLoader,
    ['fixtures/foo-module.js'],
    expectRes('fixtures/foo-module.js')
    );

    testSyncAndAsync('runs transform', sync => () => {
    // for testing transform, it should be enough to check for any 1 file type
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    transform(result) {
    result.config.foo = [result.config.foo];
    return result;
    // for testing transform, it should be enough to check for any 1 file type
    testResolves(
    'runs transform',
    configFileLoader,
    [
    'fixtures/foo.json',
    {
    transform(result) {
    result.config.foo = [result.config.foo];
    return result;
    },
    },
    }).load;

    return execAndCheck(
    () => loadConfig(null, filePath),
    result => expect(result.config).toEqual({ foo: [true] })
    );
    });
    ],
    result => expect(result.config).toEqual({ foo: [true] })
    );

    testThrows(
    testRejects(
    'does not swallow transform errors',
    sync => {
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    // eslint-disable-next-line no-unused-vars
    transform(result) {
    configFileLoader,
    [
    'fixtures/foo.json',
    {
    transform() {
    throw new Error('These pretzels are making me thirsty!');
    },
    }).load;

    return loadConfig(null, filePath);
    },
    'These pretzels are making me thirsty!'
    },
    ],
    err => {
    expect(err.message).toBe('These pretzels are making me thirsty!');
    }
    );
    });
    51 changes: 51 additions & 0 deletions test\util.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    'use strict';

    const path = require('path');
    const isPromise = require('is-promise');
    const cosmiconfig = require('..');

    function absolutePath(str) {
    return path.join(__dirname, str);
    }

    exports.absolutePath = absolutePath;

    exports.configFileLoader = function configFileLoader(sync, file, options) {
    const mergedOptions = Object.assign({ sync }, options);
    const loadConfig = cosmiconfig(null, mergedOptions).load;
    return loadConfig(null, absolutePath(file));
    };

    exports.testResolves = function testResolves(name, testFn, args, checkFn) {
    describe('sync', () => {
    it(name, () => {
    checkFn(testFn.apply(null, [true].concat(args)));
    });
    });

    describe('async', () => {
    it(name, () => {
    return testFn.apply(null, [false].concat(args)).then(checkFn);
    });
    });
    };

    exports.testRejects = function testRejects(name, throwFn, args, checkFn) {
    describe('sync', () => {
    it(name, () => {
    expect.hasAssertions();
    try {
    throwFn.apply(null, [true].concat(args));
    } catch (err) {
    checkFn(err);
    }
    });
    });

    describe('async', () => {
    it(name, () => {
    expect.hasAssertions();
    return throwFn.apply(null, [false].concat(args)).catch(checkFn);
    });
    });
    };
  2. sudo-suhas revised this gist Aug 13, 2017. 1 changed file with 9 additions and 13 deletions.
    22 changes: 9 additions & 13 deletions test\successful-files.test.js
    Original file line number Diff line number Diff line change
    @@ -43,16 +43,6 @@ function makeFileTest(sync, file) {
    };
    }

    function transform(result) {
    result.config.foo = [result.config.foo];
    return result;
    }

    // eslint-disable-next-line no-unused-vars
    function transformWithError(result) {
    throw new Error('These pretzels are making me thirsty!');
    }

    function execAndCheck(execFn, checkFn) {
    const result = execFn();

    @@ -89,7 +79,10 @@ describe('cosmiconfig.load', () => {
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    transform,
    transform(result) {
    result.config.foo = [result.config.foo];
    return result;
    },
    }).load;

    return execAndCheck(
    @@ -99,12 +92,15 @@ describe('cosmiconfig.load', () => {
    });

    testThrows(
    'transform errors not swallowed',
    'does not swallow transform errors',
    sync => {
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    transform: transformWithError,
    // eslint-disable-next-line no-unused-vars
    transform(result) {
    throw new Error('These pretzels are making me thirsty!');
    },
    }).load;

    return loadConfig(null, filePath);
  3. sudo-suhas created this gist Aug 12, 2017.
    24 changes: 24 additions & 0 deletions jest-report.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    ```
    PASS test\successful-files.test.js
    cosmiconfig.load
    √ loads defined JSON config path in sync (6ms)
    √ loads defined JSON config path in async (8ms)
    √ loads defined YAML config path in sync (2ms)
    √ loads defined YAML config path in async (8ms)
    √ loads defined JS config path in sync (1ms)
    √ loads defined JS config path in async (4ms)
    √ loads modularized JS file in sync (1ms)
    √ loads modularized JS file in async (9ms)
    √ runs transform in sync (1ms)
    √ runs transform in async (5ms)
    √ transform errors not swallowed in sync (1ms)
    √ transform errors not swallowed in async (5ms)
    Test Suites: 1 passed, 1 total
    Tests: 12 passed, 12 total
    Snapshots: 0 total
    Time: 0.276s, estimated 1s
    Ran all test suites.
    Watch Usage: Press w to show more.
    ```
    114 changes: 114 additions & 0 deletions test\successful-files.test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    'use strict';

    const isPromise = require('is-promise');

    const cosmiconfig = require('..');
    const util = require('./util');

    const absolutePath = util.absolutePath;

    function testSyncAndAsync(name, testFn, args) {
    it(`${name} in sync`, testFn.apply(null, [true].concat(args)));

    it(`${name} in async`, testFn.apply(null, [false].concat(args)));
    }

    function testThrows(name, throwFn, errMsg) {
    it(`${name} in sync`, () => {
    expect(() => throwFn(true)).toThrow(errMsg);
    });

    it(`${name} in async`, () => {
    expect.assertions(1);
    return throwFn(false).catch(err => {
    expect(err.message).toMatch(errMsg);
    });
    });
    }

    function makeFileTest(sync, file) {
    const filePath = absolutePath(file);
    return function fileTest() {
    const loadConfig = cosmiconfig(null, { sync }).load;

    return execAndCheck(
    () => loadConfig(null, filePath),
    result => {
    expect(result.config).toEqual({
    foo: true,
    });
    expect(result.filepath).toBe(filePath);
    }
    );
    };
    }

    function transform(result) {
    result.config.foo = [result.config.foo];
    return result;
    }

    // eslint-disable-next-line no-unused-vars
    function transformWithError(result) {
    throw new Error('These pretzels are making me thirsty!');
    }

    function execAndCheck(execFn, checkFn) {
    const result = execFn();

    return isPromise(result) ? result.then(checkFn) : checkFn(result);
    }

    describe('cosmiconfig.load', () => {
    testSyncAndAsync(
    'loads defined JSON config path',
    makeFileTest,
    'fixtures/foo.json'
    );

    testSyncAndAsync(
    'loads defined YAML config path',
    makeFileTest,
    'fixtures/foo.yaml'
    );

    testSyncAndAsync(
    'loads defined JS config path',
    makeFileTest,
    'fixtures/foo.js'
    );

    testSyncAndAsync(
    'loads modularized JS file',
    makeFileTest,
    'fixtures/foo-module.js'
    );

    testSyncAndAsync('runs transform', sync => () => {
    // for testing transform, it should be enough to check for any 1 file type
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    transform,
    }).load;

    return execAndCheck(
    () => loadConfig(null, filePath),
    result => expect(result.config).toEqual({ foo: [true] })
    );
    });

    testThrows(
    'transform errors not swallowed',
    sync => {
    const filePath = absolutePath('fixtures/foo.json');
    const loadConfig = cosmiconfig(null, {
    sync,
    transform: transformWithError,
    }).load;

    return loadConfig(null, filePath);
    },
    'These pretzels are making me thirsty!'
    );
    });