Skip to content

Instantly share code, notes, and snippets.

@sudo-suhas
Last active August 13, 2017 10:30
Show Gist options
  • Save sudo-suhas/3d84fa23ae4cba5700c916868fe7fa38 to your computer and use it in GitHub Desktop.
Save sudo-suhas/3d84fa23ae4cba5700c916868fe7fa38 to your computer and use it in GitHub Desktop.
cosmiconfig - Sample migration to jest
 PASS  test\successful-files.test.js
  cosmiconfig.load
    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.27s, estimated 1s
Ran all test suites matching /success/.

Watch Usage: Press w to show more.
'use strict';
const util = require('./util');
const absolutePath = util.absolutePath;
const configFileLoader = util.configFileLoader;
const testResolves = util.testResolves;
const testRejects = util.testRejects;
describe('cosmiconfig.load', () => {
const expectRes = file => result => {
expect(result.config).toEqual({
foo: true,
});
expect(result.filepath).toBe(absolutePath(file));
};
['json', 'yaml', 'js'].forEach(format => {
const file = `fixtures/foo.${format}`;
testResolves(
`loads defined ${format.toUpperCase()} config path`,
configFileLoader,
[file],
expectRes(file)
);
});
testResolves(
'loads modularized JS file',
configFileLoader,
['fixtures/foo-module.js'],
expectRes('fixtures/foo-module.js')
);
// 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;
},
},
],
result => expect(result.config).toEqual({ foo: [true] })
);
testRejects(
'does not swallow transform errors',
configFileLoader,
[
'fixtures/foo.json',
{
transform() {
throw new Error('These pretzels are making me thirsty!');
},
},
],
err => {
expect(err.message).toBe('These pretzels are making me thirsty!');
}
);
});
'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);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment