Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
simplify tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 2, 2016
1 parent 393e66a commit 68d7c56
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 48 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"devDependencies": {
"buble": "^0.12.3",
"eslint": "^2.13.1",
"locate-character": "^2.0.0",
"mocha": "^2.5.3",
"rollup": "^0.33.0",
"rollup-plugin-buble": "^0.12.1",
Expand Down
7 changes: 7 additions & 0 deletions test/function/basic/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require( 'assert' );

module.exports = {
exports: exports => {
assert.equal( exports, 42 );
}
};
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions test/function/exports/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require( 'assert' );

module.exports = {
exports: exports => {
assert.equal( exports, 'BARBAZ' );
}
};
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions test/function/inline/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require( 'assert' );

module.exports = {
exports: exports => {
assert.equal( exports(), 2 );
}
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 28 additions & 48 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require( 'path' );
const fs = require( 'fs' );
const assert = require( 'assert' );
const { SourceMapConsumer } = require( 'source-map' );
const { getLocator } = require( 'locate-character' );
const { rollup } = require( 'rollup' );
const nodeResolve = require( 'rollup-plugin-node-resolve' );
const commonjs = require( '..' );
Expand Down Expand Up @@ -31,52 +33,29 @@ function executeBundle ( bundle ) {
return module.exports;
}

function getLocation ( source, charIndex ) {
var lines = source.split( '\n' );
var len = lines.length;

var lineStart = 0;
var i;

for ( i = 0; i < len; i += 1 ) {
var line = lines[i];
var lineEnd = lineStart + line.length + 1; // +1 for newline

if ( lineEnd > charIndex ) {
return { line: i + 1, column: charIndex - lineStart };
}

lineStart = lineEnd;
}

throw new Error( 'Could not determine location of character' );
}

describe( 'rollup-plugin-commonjs', () => {
it( 'converts a basic CommonJS module', () => {
return rollup({
entry: 'samples/basic/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), 42 );
});
});

it( 'converts a CommonJS module that mutates exports instead of replacing', () => {
return rollup({
entry: 'samples/exports/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), 'BARBAZ' );
});
});

it( 'converts inline require calls', () => {
return rollup({
entry: 'samples/inline/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle )(), 2 );
describe( 'function', () => {
fs.readdirSync( 'function' ).forEach( dir => {
let config;

try {
config = require( `./function/${dir}/_config.js` );
} catch ( err ) {
config = {};
}

( config.solo ? it.only : it )( dir, () => {
return rollup({
entry: `function/${dir}/main.js`,
plugins: [ commonjs() ]
}).then( bundle => {
const exports = executeBundle( bundle );

if ( config.exports ) {
config.exports( exports );
}
});
});
});
});

Expand All @@ -92,14 +71,15 @@ describe( 'rollup-plugin-commonjs', () => {
});

const smc = new SourceMapConsumer( generated.map );
const locator = getLocator( generated.code, { offsetLine: 1 });

let generatedLoc = getLocation( generated.code, generated.code.indexOf( '42' ) );
let generatedLoc = locator( '42' );
let loc = smc.originalPositionFor( generatedLoc ); // 42
assert.equal( loc.source, 'samples/sourcemap/foo.js' );
assert.equal( loc.line, 1 );
assert.equal( loc.column, 15 );

generatedLoc = getLocation( generated.code, generated.code.indexOf( 'log' ) );
generatedLoc = locator( 'log' );
loc = smc.originalPositionFor( generatedLoc ); // log
assert.equal( loc.source, 'samples/sourcemap/main.js' );
assert.equal( loc.line, 2 );
Expand Down Expand Up @@ -357,7 +337,7 @@ describe( 'rollup-plugin-commonjs', () => {

it( 'does not process the entry file when it has a leading "." (issue #63)', () => {
return rollup({
entry: './samples/basic/main.js',
entry: './function/basic/main.js',
plugins: [ commonjs() ]
}).then( executeBundle );
});
Expand Down

0 comments on commit 68d7c56

Please sign in to comment.