diff --git a/test/indexr.js b/test/indexr.js index 11b8621..ad03435 100644 --- a/test/indexr.js +++ b/test/indexr.js @@ -1,515 +1,23 @@ /* eslint-disable max-len */ -import assert from 'assert'; -import defaultOptions from '../lib/modules/args/defaultOptions'; -import fs from 'fs'; -import handleDeprecation from '../lib/modules/args/handleDeprecation'; -import indexr from '../lib'; - -import path from 'path'; -import sinon from 'sinon'; -import extendedHelp from '../lib/modules/cli/extendedHelp'; -import getFileList from '../lib/utils/getFileList'; - -import chokidar from 'chokidar'; -import { resetLog, setLogLevel, logHistory, info } from '../lib/utils/logger'; -import { paths, runCLI, fileExists } from './lib/utils'; - -const { inputFolder, fractalFolder, outputFolder } = paths; - +import checkErrorPath from './modules/checkErrorPath'; +import cli from './modules/cli'; +import extendHelp from './modules/extendHelp'; +import getFileList from './modules/getFileList'; +import handleDeprecation from './modules/handleDeprecation'; +import ignoreNodeModulesByDefault from './modules/ignoreNodeModulesByDefault'; +import logger from './modules/logger'; +import nodeApi from './modules/nodeApi'; +import watch from './modules/watch'; +import { setLogLevel } from '../lib/utils/logger'; setLogLevel('none'); - -describe('indexr program', () => { - - describe('logger', () => { - it('should actually log stuff and print to console', () => { - resetLog(); - setLogLevel('info'); - info('foo'); - const actual = logHistory('info')[0]; - const expected = { level: 'info', message: 'indexr >> foo' }; - assert.deepEqual(expected, actual); - setLogLevel('none'); - }); - }); - - describe('handleDeprecation', () => { - const deprecated = { - include: { - sub: 'submodules', - message: 'This is a message', - }, - }; - - const warnFunc = sinon.spy(); - - it('should handle a deprecated object', () => { - const actual = handleDeprecation(deprecated, { - include: 'foo', - warnFunc, - }); - - const expected = { - submodules: 'foo', - }; - - assert(warnFunc.called); - assert.deepEqual(actual, expected, 'handleDeprecation'); - }); - - it('should not allow a deprecated prop through but instead copy its value to the new prop', () => { - const actual = handleDeprecation(deprecated, { - include: 'foo', - other: 'prop', - submodules: 'bar', - warnFunc, - }); - - const expected = { - other: 'prop', - submodules: 'foo', - }; - assert(warnFunc.called); - assert.deepEqual(actual, expected, 'handleDeprecation'); - }); - - }); - - describe('indexr', () => { - afterEach(() => { - const deletePaths = [ - path.resolve(inputFolder, 'server.js'), - path.resolve(inputFolder, 'module-3/node_modules/modules/index.r.js'), - path.resolve(inputFolder, defaultOptions.outputFilename), - path.resolve(fractalFolder, 'modules', defaultOptions.outputFilename), - path.resolve(fractalFolder, 'modules/foo.js'), - path.resolve(fractalFolder, 'modules/thing.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/foo.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/thing.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/foo.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/thing.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/foo.js'), - path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/thing.js'), - path.resolve(fractalFolder, 'modules/module-2/things/thing.js'), - ]; - - deletePaths.forEach((filePath) => { - if (fileExists(filePath)) { - fs.unlinkSync(filePath); - } - }); - }); - - describe('node API', () => { - - it('should return an es6 file with correct exports', (endTest) => { - resetLog(); - indexr(inputFolder, { modules: undefined }).then(() => { - - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - assert(logHistory().length === 1); - endTest(); - }) - .catch(endTest); - }); - - it('should return an es5 file with correct exports', (endTest) => { - resetLog(); - indexr(inputFolder, { es5: true, modules: undefined }).then(() => { - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es5.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - assert(logHistory('info').length === 1); - endTest(); - }) - .catch(endTest); - }); - - it('should write to a file if the output filename is provided', (endTest) => { - // const warnFunc = sinon.spy(); - - indexr(inputFolder, 'server.js', { modules: undefined }).then(() => { - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); - const actual = fs.readFileSync(path.resolve(inputFolder, 'server.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - - it('should accept outputFilename as an option', (endTest) => { - indexr(inputFolder, { outputFilename: 'server.js', modules: undefined }).then(() => { - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); - const actual = fs.readFileSync(path.resolve(inputFolder, 'server.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should capture submodules filters', (endTest) => { - indexr(inputFolder, { submodules: '*/server.js', modules: undefined }).then(() => { - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - const expected = fs.readFileSync(path.resolve(outputFolder, - 'expected-es6-server.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should capture submodules ignore', (endTest) => { - indexr(inputFolder, { submodulesIgnore: 'module-1/', modules: undefined }).then(() => { - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - const expected = fs.readFileSync(path.resolve(outputFolder, - 'expected-es6-submodules-ignore.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should directImport the files if asked ', (endTest) => { - indexr(inputFolder, { submodules: '*/server.js', directImport: true, modules: undefined }) - .then(() => { - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename)); - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6-server-direct.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should remove exts provided ', (endTest) => { - indexr(inputFolder, { - submodules: '*/server.js', - directImport: true, - modules: undefined, - exts: ['js'], - }).then(() => { - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename)); - const expected = fs.readFileSync(path.resolve(outputFolder, - 'expected-es6-server-direct-exts.js'), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should use named exports', (endTest) => { - indexr(inputFolder, { - modules: undefined, - namedExports: true, - }) - .then(() => { - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6-named-exports.js'), 'utf-8'); - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should use named exports for es5', (endTest) => { - indexr(inputFolder, { - modules: undefined, - es5: true, - namedExports: true, - }) - .then(() => { - const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es5-named-exports.js'), 'utf-8'); - const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); - assert.equal(actual, expected, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should accept globs as folders and run indexr on each returned result', (endTest) => { - // const warnFunc = sinon.spy(); - indexr(fractalFolder, 'foo.js', { - modules: '**/modules/', - submodules: '*/index.js', - // warnFunc, - }).then(() => { - const expected = [ - fs.readFileSync(path.resolve(outputFolder, 'expected-module.js'), 'utf-8'), - fs.readFileSync(path.resolve(outputFolder, 'expected-nested.js'), 'utf-8'), - fs.readFileSync(path.resolve(outputFolder, 'expected-double-nested.js'), 'utf-8'), - ]; - - const actual = [ - fs.readFileSync(path.resolve(fractalFolder, 'modules/foo.js'), 'utf-8'), - fs.readFileSync(path.resolve(fractalFolder, 'modules/module-1/modules/foo.js'), 'utf-8'), - fs.readFileSync(path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/foo.js'), 'utf-8'), - ]; - - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - endTest(); - }) - .catch(endTest); - }); - - it('should accept multiple globs in arrays and run indexr on each returned result', (endTest) => { - resetLog(); - indexr(fractalFolder, 'thing.js', { - submodules: '*/index.js', - modules: ['**/modules/', '**/things/'], - }) - .then(() => { - const expected = [ - fs.readFileSync(path.resolve(outputFolder, 'expected-module.js'), 'utf-8'), - fs.readFileSync(path.resolve(outputFolder, 'expected-nested.js'), 'utf-8'), - fs.readFileSync(path.resolve(outputFolder, 'expected-double-nested.js'), 'utf-8'), - fs.readFileSync(path.resolve(outputFolder, 'expected-thing.js'), 'utf-8'), - ]; - - const actual = [ - fs.readFileSync(path.resolve(fractalFolder, 'modules', 'thing.js'), 'utf-8'), - fs.readFileSync(path.resolve(fractalFolder, 'modules', 'module-1', 'modules', 'thing.js'), 'utf-8'), - fs.readFileSync(path.resolve(fractalFolder, 'modules', 'module-1', 'modules', 'nested-2', 'modules', 'thing.js'), 'utf-8'), - fs.readFileSync(path.resolve(fractalFolder, 'modules', 'module-2', 'things', 'thing.js'), 'utf-8'), - ]; - - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - assert(logHistory().length === 5); - endTest(); - }) - .catch(endTest); - }); - - describe('watch', () => { - it('should run the file watcher', () => { - const onEventSpy = sinon.spy((tag, func) => func()); - sinon.stub(chokidar, 'watch', () => ({ on: onEventSpy })); - - indexr(fractalFolder, 'thing.js', { - watch: '**/foo/*', - }); - - assert(chokidar.watch.withArgs('**/foo/*', { ignored: ['**/modules/thing.js'] }).calledOnce, 'Chockidar was not called with the correct args.'); - assert(onEventSpy.withArgs('all').calledOnce); - chokidar.watch.restore(); - }); - - it('should run the file watcher on **/* with watch: true', () => { - sinon.stub(chokidar, 'watch', () => ({ on: () => {} })); - - indexr(fractalFolder, 'thing.js', { - watch: true, - }); - - assert(chokidar.watch.withArgs('**/*', { ignored: ['**/modules/thing.js'] }).calledOnce, 'Chockidar was not called with the correct args.'); - chokidar.watch.restore(); - }); - - - describe('check error path', () => { - it('should throw an error in a nice way', (endTest) => { - resetLog(); - setLogLevel('none'); - indexr(/blow/) - .catch(() => { - const actual = logHistory()[0]; - const expected = { - level: 'error', - message: 'indexr >> ERROR: TypeError: Path must be a string. Received /blow/', - }; - assert.deepEqual(expected, actual); - endTest(); - }); - - }); - }); - - }); - }); - - describe('Ignore node_modules by default', () => { - it('should ignore node_modules', (endTest) => { - indexr(inputFolder) - .then(() => { - const actual = fileExists(path.resolve(inputFolder, 'module-3/node_modules/modules/index.r.js')); - const expected = false; - assert.equal(expected, actual); - endTest(); - }) - .catch(endTest); - - }); - }); - - describe('CLI', () => { - it('should support --out', () => { - const actual = runCLI('indexr', '.', '--out', 'index.js'); - const expected = { - inputFolder: '.', - options: { - outputFilename: 'index.js', - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --es5', () => { - const actual = runCLI('indexr', '.', '--es5'); - const expected = { - inputFolder: '.', - options: { - es5: true, - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --direct-import', () => { - const actual = runCLI('indexr', '.', '--direct-import'); - const expected = { - inputFolder: '.', - options: { - directImport: true, - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --direct-import', () => { - const actual = runCLI('indexr', '.', '--direct-import'); - const expected = { - inputFolder: '.', - options: { - directImport: true, - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --named-exports', () => { - const actual = runCLI('indexr', '.', '--named-exports'); - const expected = { - inputFolder: '.', - options: { - namedExports: true, - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --modules', () => { - const actual = runCLI('indexr', '.', '--modules', '**/fooo/'); - const expected = { - inputFolder: '.', - options: { - modules: ['**/fooo/'], - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --modules-ignore', () => { - const actual = runCLI('indexr', '.', '--modules-ignore', '**/fooo/'); - const expected = { - inputFolder: '.', - options: { - modulesIgnore: ['**/fooo/'], - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --submodules', () => { - const actual = runCLI('indexr', '.', '--submodules', '**/fooo/'); - const expected = { - inputFolder: '.', - options: { - submodules: ['**/fooo/'], - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --submodules-ignore', () => { - const actual = runCLI('indexr', '.', '--submodules-ignore', '**/fooo/'); - const expected = { - inputFolder: '.', - options: { - submodulesIgnore: ['**/fooo/'], - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should support --watch', () => { - const actual = runCLI('indexr', '.', '--watch', '**/fooo/'); - const expected = { - inputFolder: '.', - options: { - watch: '**/fooo/', - }, - }; - assert.deepEqual(expected, actual, 'Function did not return expected output.'); - }); - - it('should return correct --exts', () => { - const actual = runCLI('indexr', '.', '--ext', 'js', '--ext', 'jsx'); - const expected = { - inputFolder: '.', - options: { - exts: ['js', 'jsx'], - }, - }; - assert.deepEqual(expected, actual); - }); - }); - - describe('Units', () => { - - it('extendHelp', () => { - const flags = '-f, --some-flag'; - const description = 'Some flag'; - const long = 'Some long'; - const coercion = undefined; - const defaults = undefined; - const allmessages = []; - - sinon.stub(console, 'log', (msg) => allmessages.push(msg)); - - // const option = sinon.spy(); - const option = () => {}; - const on = (tag, func) => func(); - - extendedHelp({ option, on }, [{ - flags, - description, - coercion, - defaults, - long, - }]); - console.log.restore(); - - assert.deepEqual(allmessages, ['\n Some flag\n ----------\n -f, --some-flag\n\n Some long\n\n']); - - }); - - describe('getFileList', () => { - it('should blow up if it is given a non string path', (endTest) => { - getFileList(/1234/, ['one']) - .then(() => { - endTest('No error was thrown but it should have.'); - }) - .catch((e) => { - assert(e); - endTest(); - }); - }); - }); - - - }); - }); - +describe('indexr', () => { + describe('checkErrorPath', checkErrorPath); + describe('cli', cli); + describe('extendHelp', extendHelp); + describe('getFileList', getFileList); + describe('handleDeprecation', handleDeprecation); + describe('ignoreNodeModulesByDefault', ignoreNodeModulesByDefault); + describe('logger', logger); + describe('nodeApi', nodeApi); + describe('watch', watch); }); - - diff --git a/test/lib/utils.js b/test/lib/utils.js index 14858ab..cb73326 100644 --- a/test/lib/utils.js +++ b/test/lib/utils.js @@ -23,3 +23,10 @@ export const paths = { outputFolder: path.resolve(__dirname, '..', './fixtures/output'), }; +export const deleteFiles = (...deletePaths) => { + deletePaths.forEach((filePath) => { + if (fileExists(filePath)) { + fs.unlinkSync(filePath); + } + }); +}; diff --git a/test/chainMap.js b/test/modules/chainMap.js similarity index 96% rename from test/chainMap.js rename to test/modules/chainMap.js index e3f28d7..5427f8a 100644 --- a/test/chainMap.js +++ b/test/modules/chainMap.js @@ -1,5 +1,5 @@ import { assert } from 'chai'; -import { chain, map } from '../lib/utils/chainMap'; +import { chain, map } from '../../lib/utils/chainMap'; describe('chain', () => { diff --git a/test/modules/checkErrorPath.js b/test/modules/checkErrorPath.js new file mode 100644 index 0000000..8cc5da7 --- /dev/null +++ b/test/modules/checkErrorPath.js @@ -0,0 +1,20 @@ +import indexr from '../../lib'; +import { assert } from 'chai'; +import { resetLog, setLogLevel, logHistory } from '../../lib/utils/logger'; +export default () => { + it('should throw an error in a nice way', (endTest) => { + resetLog(); + setLogLevel('none'); + indexr(/blow/) + .catch(() => { + const actual = logHistory()[0]; + const expected = { + level: 'error', + message: 'indexr >> ERROR: TypeError: Path must be a string. Received /blow/', + }; + assert.deepEqual(expected, actual); + endTest(); + }); + + }); +}; diff --git a/test/modules/cli.js b/test/modules/cli.js new file mode 100644 index 0000000..0aedceb --- /dev/null +++ b/test/modules/cli.js @@ -0,0 +1,124 @@ +import { runCLI } from '../lib/utils'; +import { assert } from 'chai'; +export default () => { + it('should support --out', () => { + const actual = runCLI('indexr', '.', '--out', 'index.js'); + const expected = { + inputFolder: '.', + options: { + outputFilename: 'index.js', + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --es5', () => { + const actual = runCLI('indexr', '.', '--es5'); + const expected = { + inputFolder: '.', + options: { + es5: true, + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --direct-import', () => { + const actual = runCLI('indexr', '.', '--direct-import'); + const expected = { + inputFolder: '.', + options: { + directImport: true, + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --direct-import', () => { + const actual = runCLI('indexr', '.', '--direct-import'); + const expected = { + inputFolder: '.', + options: { + directImport: true, + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --named-exports', () => { + const actual = runCLI('indexr', '.', '--named-exports'); + const expected = { + inputFolder: '.', + options: { + namedExports: true, + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --modules', () => { + const actual = runCLI('indexr', '.', '--modules', '**/fooo/'); + const expected = { + inputFolder: '.', + options: { + modules: ['**/fooo/'], + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --modules-ignore', () => { + const actual = runCLI('indexr', '.', '--modules-ignore', '**/fooo/'); + const expected = { + inputFolder: '.', + options: { + modulesIgnore: ['**/fooo/'], + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --submodules', () => { + const actual = runCLI('indexr', '.', '--submodules', '**/fooo/'); + const expected = { + inputFolder: '.', + options: { + submodules: ['**/fooo/'], + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --submodules-ignore', () => { + const actual = runCLI('indexr', '.', '--submodules-ignore', '**/fooo/'); + const expected = { + inputFolder: '.', + options: { + submodulesIgnore: ['**/fooo/'], + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should support --watch', () => { + const actual = runCLI('indexr', '.', '--watch', '**/fooo/'); + const expected = { + inputFolder: '.', + options: { + watch: '**/fooo/', + }, + }; + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + }); + + it('should return correct --exts', () => { + const actual = runCLI('indexr', '.', '--ext', 'js', '--ext', 'jsx'); + const expected = { + inputFolder: '.', + options: { + exts: ['js', 'jsx'], + }, + }; + assert.deepEqual(expected, actual); + }); +}; diff --git a/test/modules/extendHelp.js b/test/modules/extendHelp.js new file mode 100644 index 0000000..a5af06c --- /dev/null +++ b/test/modules/extendHelp.js @@ -0,0 +1,33 @@ +import extendedHelp from '../../lib/modules/cli/extendedHelp'; +import { assert } from 'chai'; +import sinon from 'sinon'; + +export default () => { + it('extendHelp', () => { + const flags = '-f, --some-flag'; + const description = 'Some flag'; + const long = 'Some long'; + const coercion = undefined; + const defaults = undefined; + const allmessages = []; + + sinon.stub(console, 'log', (msg) => allmessages.push(msg)); + + // const option = sinon.spy(); + const option = () => {}; + const on = (tag, func) => func(); + + extendedHelp({ option, on }, [{ + flags, + description, + coercion, + defaults, + long, + }]); + console.log.restore(); + assert.deepEqual( + allmessages, + ['\n Some flag\n ----------\n -f, --some-flag\n\n Some long\n\n'] + ); + }); +}; diff --git a/test/modules/getFileList.js b/test/modules/getFileList.js new file mode 100644 index 0000000..1b416b6 --- /dev/null +++ b/test/modules/getFileList.js @@ -0,0 +1,14 @@ +import { assert } from 'chai'; +import getFileList from '../../lib/utils/getFileList'; +export default () => { + it('should blow up if it is given a non string path', (endTest) => { + getFileList(/1234/, ['one']) + .then(() => { + endTest('No error was thrown but it should have.'); + }) + .catch((e) => { + assert(e); + endTest(); + }); + }); +}; diff --git a/test/modules/handleDeprecation.js b/test/modules/handleDeprecation.js new file mode 100644 index 0000000..1b111da --- /dev/null +++ b/test/modules/handleDeprecation.js @@ -0,0 +1,44 @@ +import handleDeprecation from '../../lib/modules/args/handleDeprecation'; +import { assert } from 'chai'; +import sinon from 'sinon'; + +export default () => { + const deprecated = { + include: { + sub: 'submodules', + message: 'This is a message', + }, + }; + + const warnFunc = sinon.spy(); + + it('should handle a deprecated object', () => { + const actual = handleDeprecation(deprecated, { + include: 'foo', + warnFunc, + }); + + const expected = { + submodules: 'foo', + }; + + assert(warnFunc.called); + assert.deepEqual(actual, expected, 'handleDeprecation'); + }); + + it('should not allow a deprecated prop through but instead copy its value to the new prop', () => { + const actual = handleDeprecation(deprecated, { + include: 'foo', + other: 'prop', + submodules: 'bar', + warnFunc, + }); + + const expected = { + other: 'prop', + submodules: 'foo', + }; + assert(warnFunc.called); + assert.deepEqual(actual, expected, 'handleDeprecation'); + }); +}; diff --git a/test/modules/ignoreNodeModulesByDefault.js b/test/modules/ignoreNodeModulesByDefault.js new file mode 100644 index 0000000..0106629 --- /dev/null +++ b/test/modules/ignoreNodeModulesByDefault.js @@ -0,0 +1,21 @@ +import indexr from '../../lib'; +import { fileExists, paths } from '../lib/utils'; +import path from 'path'; +import { assert } from 'chai'; +const { + inputFolder, +} = paths; + +export default () => { + it('should ignore node_modules', (endTest) => { + indexr(inputFolder) + .then(() => { + const actual = fileExists(path.resolve(inputFolder, 'module-3/node_modules/modules/index.r.js')); + const expected = false; + assert.equal(expected, actual); + endTest(); + }) + .catch(endTest); + + }); +}; diff --git a/test/modules/logger.js b/test/modules/logger.js new file mode 100644 index 0000000..4b8c96f --- /dev/null +++ b/test/modules/logger.js @@ -0,0 +1,14 @@ +import { assert } from 'chai'; +import { resetLog, setLogLevel, logHistory, info } from '../../lib/utils/logger'; + +export default () => { + it('should actually log stuff and print to console', () => { + resetLog(); + setLogLevel('none'); + info('foo'); + const actual = logHistory('info')[0]; + const expected = { level: 'info', message: 'indexr >> foo' }; + assert.deepEqual(expected, actual); + setLogLevel('none'); + }); +}; diff --git a/test/modules/nodeApi.js b/test/modules/nodeApi.js new file mode 100644 index 0000000..ad1d7e7 --- /dev/null +++ b/test/modules/nodeApi.js @@ -0,0 +1,227 @@ +/* eslint-disable max-len */ +import defaultOptions from '../../lib/modules/args/defaultOptions'; +import fs from 'fs'; +import path from 'path'; +import indexr from '../../lib'; +import { assert } from 'chai'; +import { deleteFiles, paths } from '../lib/utils'; +import { resetLog, logHistory } from '../../lib/utils/logger'; + +const { + fractalFolder, + inputFolder, + outputFolder, +} = paths; + +export default () => { + it('should return an es6 file with correct exports', (endTest) => { + resetLog(); + indexr(inputFolder, { modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); + const actual = fs.readFileSync(outputPath, 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + assert(logHistory().length === 1); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should return an es5 file with correct exports', (endTest) => { + resetLog(); + indexr(inputFolder, { es5: true, modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const actual = fs.readFileSync(path.resolve(inputFolder, defaultOptions.outputFilename), 'utf-8'); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es5.js'), 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + assert(logHistory('info').length === 1); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should write to a file if the output filename is provided', (endTest) => { + // const warnFunc = sinon.spy(); + + indexr(inputFolder, 'server.js', { modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, 'server.js'); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); + const actual = fs.readFileSync(outputPath, 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + + it('should accept outputFilename as an option', (endTest) => { + indexr(inputFolder, { outputFilename: 'server.js', modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, 'server.js'); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6.js'), 'utf-8'); + const actual = fs.readFileSync(outputPath, 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should capture submodules filters', (endTest) => { + indexr(inputFolder, { submodules: '*/server.js', modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const actual = fs.readFileSync(outputPath, 'utf-8'); + const expected = fs.readFileSync(path.resolve(outputFolder, + 'expected-es6-server.js'), 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should capture submodules ignore', (endTest) => { + indexr(inputFolder, { submodulesIgnore: 'module-1/', modules: undefined }).then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const actual = fs.readFileSync(outputPath, 'utf-8'); + const expected = fs.readFileSync(path.resolve(outputFolder, + 'expected-es6-submodules-ignore.js'), 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should directImport the files if asked ', (endTest) => { + indexr(inputFolder, { submodules: '*/server.js', directImport: true, modules: undefined }) + .then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const actual = fs.readFileSync(outputPath); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6-server-direct.js'), 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should remove exts provided ', (endTest) => { + indexr(inputFolder, { + submodules: '*/server.js', + directImport: true, + modules: undefined, + exts: ['js'], + }).then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const actual = fs.readFileSync(outputPath); + const expected = fs.readFileSync(path.resolve(outputFolder, + 'expected-es6-server-direct-exts.js'), 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should use named exports', (endTest) => { + indexr(inputFolder, { + modules: undefined, + namedExports: true, + }) + .then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es6-named-exports.js'), 'utf-8'); + const actual = fs.readFileSync(outputPath, 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should use named exports for es5', (endTest) => { + indexr(inputFolder, { + modules: undefined, + es5: true, + namedExports: true, + }) + .then(() => { + const outputPath = path.resolve(inputFolder, defaultOptions.outputFilename); + const expected = fs.readFileSync(path.resolve(outputFolder, 'expected-es5-named-exports.js'), 'utf-8'); + const actual = fs.readFileSync(outputPath, 'utf-8'); + assert.equal(actual, expected, 'Function did not return expected output.'); + deleteFiles(outputPath); + endTest(); + }) + .catch(endTest); + }); + + it('should accept globs as folders and run indexr on each returned result', (endTest) => { + indexr(fractalFolder, 'foo.js', { + modules: '**/modules/', + submodules: '*/index.js', + }).then(() => { + + const loadFileSync = (filename) => fs.readFileSync(filename, 'utf-8'); + + const actualPaths = [ + path.resolve(fractalFolder, 'modules/foo.js'), + path.resolve(fractalFolder, 'modules/module-1/modules/foo.js'), + path.resolve(fractalFolder, 'modules/module-1/modules/nested-2/modules/foo.js'), + ]; + + const expectedPaths = [ + path.resolve(outputFolder, 'expected-module.js'), + path.resolve(outputFolder, 'expected-nested.js'), + path.resolve(outputFolder, 'expected-double-nested.js'), + ]; + + const expected = expectedPaths.map(loadFileSync); + const actual = actualPaths.map(loadFileSync); + + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + deleteFiles(...actualPaths); + endTest(); + }) + .catch(endTest); + }); + + it('should accept multiple globs in arrays and run indexr on each returned result', (endTest) => { + resetLog(); + indexr(fractalFolder, 'thing.js', { + submodules: '*/index.js', + modules: ['**/modules/', '**/things/'], + }) + .then(() => { + + const loadFileSync = (filename) => fs.readFileSync(filename, 'utf-8'); + + const expectedPaths = [ + path.resolve(outputFolder, 'expected-module.js'), + path.resolve(outputFolder, 'expected-nested.js'), + path.resolve(outputFolder, 'expected-double-nested.js'), + path.resolve(outputFolder, 'expected-thing.js'), + ]; + + const actualPaths = [ + path.resolve(fractalFolder, 'modules', 'thing.js'), + path.resolve(fractalFolder, 'modules', 'module-1', 'modules', 'thing.js'), + path.resolve(fractalFolder, 'modules', 'module-1', 'modules', 'nested-2', 'modules', 'thing.js'), + path.resolve(fractalFolder, 'modules', 'module-2', 'things', 'thing.js'), + ]; + + const expected = expectedPaths.map(loadFileSync); + const actual = actualPaths.map(loadFileSync); + + assert.deepEqual(expected, actual, 'Function did not return expected output.'); + assert(logHistory().length === 5); + deleteFiles(...actualPaths); + endTest(); + }) + .catch(endTest); + }); + +}; diff --git a/test/modules/watch.js b/test/modules/watch.js new file mode 100644 index 0000000..a530d67 --- /dev/null +++ b/test/modules/watch.js @@ -0,0 +1,33 @@ +import sinon from 'sinon'; +import chokidar from 'chokidar'; +import indexr from '../../lib'; +import { assert } from 'chai'; +import { paths } from '../lib/utils'; +const { fractalFolder } = paths; + +export default () => { + it('should run the file watcher', () => { + const onEventSpy = sinon.spy((tag, func) => func()); + sinon.stub(chokidar, 'watch', () => ({ on: onEventSpy })); + + indexr(fractalFolder, 'thing.js', { + watch: '**/foo/*', + }); + + assert(chokidar.watch.withArgs('**/foo/*', { ignored: ['**/modules/thing.js'] }).calledOnce, 'Chockidar was not called with the correct args.'); + assert(onEventSpy.withArgs('all').calledOnce); + chokidar.watch.restore(); + }); + + it('should run the file watcher on **/* with watch: true', () => { + sinon.stub(chokidar, 'watch', () => ({ on: () => {} })); + + indexr(fractalFolder, 'thing.js', { + watch: true, + }); + + assert(chokidar.watch.withArgs('**/*', { ignored: ['**/modules/thing.js'] }).calledOnce, 'Chockidar was not called with the correct args.'); + chokidar.watch.restore(); + }); + +};