Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added eslint and mocha test. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
test/.gobble/
test/dist/
.DS_Store
node_modules
npm-debug.log
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 1 addition & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 '';
});
Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
}
}
2 changes: 2 additions & 0 deletions test/fixtures/local/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is the required script
module.exports = 'foo';
4 changes: 4 additions & 0 deletions test/fixtures/local/index.bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this is the entry bundle
var foo = require('./foo');

module.exports = foo === 'foo'; // should export true
70 changes: 70 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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 );
});
});
});