load-config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const path = require('path');
  3. const tap = require('tap');
  4. const loadConfig = require('../lib/load-config');
  5. const {test} = tap;
  6. tap.afterEach(done => {
  7. process.chdir(path.resolve(__dirname, '..'));
  8. done();
  9. });
  10. const changeDir = fixtureDir => {
  11. process.chdir(path.resolve(__dirname, 'fixture', 'load-config', fixtureDir));
  12. };
  13. test('finds config in package.json', t => {
  14. changeDir('package-only');
  15. const conf = loadConfig();
  16. t.is(conf.failFast, true);
  17. t.end();
  18. });
  19. test('throws a warning of both configs are present', t => {
  20. changeDir('package-yes-file-yes');
  21. t.throws(loadConfig);
  22. t.end();
  23. });
  24. test('merges in defaults passed with initial call', t => {
  25. changeDir('package-only');
  26. const defaults = {
  27. files: ['123', '!456']
  28. };
  29. const {files, failFast} = loadConfig(defaults);
  30. t.is(failFast, true, 'preserves original props');
  31. t.is(files, defaults.files, 'merges in extra props');
  32. t.end();
  33. });
  34. test('loads config from file with `export default` syntax', t => {
  35. changeDir('package-no-file-yes');
  36. const conf = loadConfig();
  37. t.is(conf.files, 'config-file-esm-test-value');
  38. t.end();
  39. });
  40. test('loads config from factory function', t => {
  41. changeDir('package-no-file-yes-factory');
  42. const conf = loadConfig();
  43. t.ok(conf.files.startsWith(__dirname));
  44. t.end();
  45. });
  46. test('throws an error if a config factory returns a promise', t => {
  47. changeDir('factory-no-promise-return');
  48. t.throws(loadConfig);
  49. t.end();
  50. });
  51. test('throws an error if a config exports a promise', t => {
  52. changeDir('no-promise-config');
  53. t.throws(loadConfig);
  54. t.end();
  55. });
  56. test('throws an error if a config factory does not return a plain object', t => {
  57. changeDir('factory-no-plain-return');
  58. t.throws(loadConfig);
  59. t.end();
  60. });
  61. test('throws an error if a config does not export a plain object', t => {
  62. changeDir('no-plain-config');
  63. t.throws(loadConfig);
  64. t.end();
  65. });
  66. test('receives a `projectDir` property', t => {
  67. changeDir('package-only');
  68. const conf = loadConfig();
  69. t.ok(conf.projectDir.startsWith(__dirname));
  70. t.end();
  71. });
  72. test('rethrows wrapped module errors', t => {
  73. t.plan(1);
  74. changeDir('throws');
  75. try {
  76. loadConfig();
  77. } catch (err) {
  78. t.is(err.parent.message, 'foo');
  79. }
  80. });
  81. test('throws an error if a config file has no default export', t => {
  82. changeDir('no-default-export');
  83. t.throws(loadConfig);
  84. t.end();
  85. });