From eb0d24c0b467d5e72c11b401548b8ccac088ddf3 Mon Sep 17 00:00:00 2001 From: Justin Lau Date: Sat, 31 Oct 2015 19:44:41 +0800 Subject: [PATCH] Added eslint and mocha test. --- .eslintrc | 23 ++++++++++ .gitignore | 3 ++ README.md | 13 ++++++ index.js | 14 +----- package.json | 17 ++++++- test/fixtures/local/foo.js | 2 + test/fixtures/local/index.bundle.js | 4 ++ test/test.js | 70 +++++++++++++++++++++++++++++ 8 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 .eslintrc create mode 100644 test/fixtures/local/foo.js create mode 100644 test/fixtures/local/index.bundle.js create mode 100644 test/test.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..6e5fb1a --- /dev/null +++ b/.eslintrc @@ -0,0 +1,23 @@ +{ + "rules": { + "indent": [ 2, "tab", { "SwitchCase": 1 } ], + "quotes": [ 2, "single" ], + "linebreak-style": [ 2, "unix" ], + "semi": [ 2, "always" ], + "space-after-keywords": [ 2, "always" ], + "space-before-blocks": [ 2, "always" ], + "space-before-function-paren": [ 2, "always" ], + "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], + "no-cond-assign": [ 0 ] + }, + "env": { + "es6": true, + "browser": true, + "mocha": true, + "node": true + }, + "extends": "eslint:recommended", + "ecmaFeatures": { + "modules": true + } +} diff --git a/.gitignore b/.gitignore index 9daa824..c313f3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ +test/.gobble/ +test/dist/ .DS_Store node_modules +npm-debug.log diff --git a/README.md b/README.md index cf9556b..4a464fa 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,19 @@ module.exports = gobble( 'src' ).transform( 'browserify', { }); ``` +## Testing + +To run a single test: + +```bash +npm test +``` + +To run a continuous test that watches the source file and test cases for changes: + +```bash +npm start +``` ## License diff --git a/index.js b/index.js index 87ada06..6500192 100644 --- a/index.js +++ b/index.js @@ -18,18 +18,6 @@ function ensureArray ( thing ) { return thing; } -function concat ( stream, callback ) { - var body = ''; - - stream.on( 'data', function ( chunk ) { - body += chunk.toString(); - }); - - stream.on( 'end', function () { - callback( body ); - }); -} - function cacheDependency ( cache, originalDep, inputdir ) { var dep = {}; Object.keys( originalDep ).forEach( function ( key ) { @@ -154,7 +142,7 @@ module.exports = function browserify ( inputdir, outputdir, options, callback ) // incorrect, as browsers (and other sourcemap tools) will assume that the URL // is for the bundle's own map, whether or not there is one. So we remove them, // and store the value of the last one in case we need to process it - bundle = bundle.replace( SOURCEMAP_COMMENT, function ( match, url, a ) { + bundle = bundle.replace( SOURCEMAP_COMMENT, function ( match, url ) { lastSourceMappingURL = url; return ''; }); diff --git a/package.json b/package.json index e7d587f..af28264 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,11 @@ "author": "Rich Harris", "license": "MIT", "repository": "https://github.com/gobblejs/gobble-browserify", + "scripts": { + "lint": "eslint index.js test/", + "start": "npm run -s test; chokidar 'index.js' 'test/test.js' -i 'test/.gobble' -i 'test/dist' -c 'npm run -s test'", + "test": "mocha test/test.js" + }, "dependencies": { "browserify": "^10.1.0" }, @@ -13,5 +18,15 @@ ], "files": [ "index.js" - ] + ], + "devDependencies": { + "chai": "^3.4.0", + "chokidar-cli": "^1.1.1", + "eslint": "^1.8.0", + "eslint-cli": "^0.2.0", + "gobble": "^0.10.2", + "mocha": "^2.3.3", + "sander": "^0.4.0", + "source-map": "^0.5.3" + } } diff --git a/test/fixtures/local/foo.js b/test/fixtures/local/foo.js new file mode 100644 index 0000000..2b974ca --- /dev/null +++ b/test/fixtures/local/foo.js @@ -0,0 +1,2 @@ +// this is the required script +module.exports = 'foo'; diff --git a/test/fixtures/local/index.bundle.js b/test/fixtures/local/index.bundle.js new file mode 100644 index 0000000..d0614e7 --- /dev/null +++ b/test/fixtures/local/index.bundle.js @@ -0,0 +1,4 @@ +// this is the entry bundle +var foo = require('./foo'); + +module.exports = foo === 'foo'; // should export true diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..825d01f --- /dev/null +++ b/test/test.js @@ -0,0 +1,70 @@ +// var assert = require( 'assert' ); +var gobble = require( 'gobble' ); +var gobbleBrowserify = require( '../' ); +var path = require( 'path' ); +var sander = require( 'sander' ); +var sourceMap = require( 'source-map' ); +require( 'chai' ).should(); + +const buildConfig = { + dest: path.resolve( __dirname, 'dist' ), + gobbledir: path.resolve( __dirname, '.gobble' ), + force: true +}; + +/** + * Returns an absolute path to a fixture directory / file + * + * @param {String} relativePath path relative to `'fixtures/'` + * @param {String} + */ +function fixturePath ( relativePath ) { + return path.join( __dirname, 'fixtures', relativePath ); +} +/** + * Returns an absolute path to a gobble compiled directory / file + * + * @param {String} relativePath path relative to `buildConfig.dest` + * @param {String} + */ +function outputPath ( relativePath ) { + return path.join( buildConfig.dest, relativePath ); +} + +afterEach( function () { + // delete output files + return sander.Promise.all([ + sander.rimraf( buildConfig.dest ), + sander.rimraf( buildConfig.gobbledir ) + ]); +}); + +describe( 'when used with local dependencies', function () { + beforeEach( function () { + return gobble( fixturePath( 'local' ) ) + .transform( gobbleBrowserify, { + entries: 'index.bundle.js', + dest: 'local.js', + standalone: 'local' + } ) + .build( buildConfig ); + }); + + it( 'should resolve local dependencies', function () { + require( outputPath( 'local.js' ) ).should.equal( true ); + }); + + it( 'should generate correct sourcemap', function () { + return sander.readFile( outputPath( 'local.js.map' ) ) + .then( function ( sourcemap ) { + var consumer = new sourceMap.SourceMapConsumer( JSON.parse( sourcemap ) ); + var originalPosition = consumer.originalPositionFor( { + line: 3, + column: 1 + }); + + originalPosition.source.should.match( /foo\.js$/ ); + originalPosition.line.should.equal( 2 ); + }); + }); +});