From 1034d0b6fe114459833f62af658e039fdc7c4932 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 25 Sep 2015 16:47:54 -0400 Subject: [PATCH] test for bad export types --- .../export-type-mismatch-b/_config.js | 11 +++ test/function/export-type-mismatch-b/main.js | 1 + .../export-type-mismatch-c/_config.js | 11 +++ test/function/export-type-mismatch-c/main.js | 1 + test/function/export-type-mismatch/_config.js | 11 +++ test/function/export-type-mismatch/main.js | 1 + test/test.js | 71 +++++++++---------- 7 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 test/function/export-type-mismatch-b/_config.js create mode 100644 test/function/export-type-mismatch-b/main.js create mode 100644 test/function/export-type-mismatch-c/_config.js create mode 100644 test/function/export-type-mismatch-c/main.js create mode 100644 test/function/export-type-mismatch/_config.js create mode 100644 test/function/export-type-mismatch/main.js diff --git a/test/function/export-type-mismatch-b/_config.js b/test/function/export-type-mismatch-b/_config.js new file mode 100644 index 0000000..25a7220 --- /dev/null +++ b/test/function/export-type-mismatch-b/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'export type must be auto, default, named or none', + bundleOptions: { + exports: 'blah' + }, + generateError: function ( err ) { + assert.ok( /options\.exports must be 'default', 'named', 'none', 'auto', or left unspecified/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch-b/main.js b/test/function/export-type-mismatch-b/main.js new file mode 100644 index 0000000..7a4e8a7 --- /dev/null +++ b/test/function/export-type-mismatch-b/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/export-type-mismatch-c/_config.js b/test/function/export-type-mismatch-c/_config.js new file mode 100644 index 0000000..8df0d58 --- /dev/null +++ b/test/function/export-type-mismatch-c/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'cannot have named exports if explicit export type is default', + bundleOptions: { + exports: 'none' + }, + generateError: function ( err ) { + assert.ok( /'none' was specified for options\.exports/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch-c/main.js b/test/function/export-type-mismatch-c/main.js new file mode 100644 index 0000000..7a4e8a7 --- /dev/null +++ b/test/function/export-type-mismatch-c/main.js @@ -0,0 +1 @@ +export default 42; diff --git a/test/function/export-type-mismatch/_config.js b/test/function/export-type-mismatch/_config.js new file mode 100644 index 0000000..5b321d6 --- /dev/null +++ b/test/function/export-type-mismatch/_config.js @@ -0,0 +1,11 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'cannot have named exports if explicit export type is default', + bundleOptions: { + exports: 'default' + }, + generateError: function ( err ) { + assert.ok( /'default' was specified for options\.exports/.test( err.message ) ); + } +}; diff --git a/test/function/export-type-mismatch/main.js b/test/function/export-type-mismatch/main.js new file mode 100644 index 0000000..3b8dc9f --- /dev/null +++ b/test/function/export-type-mismatch/main.js @@ -0,0 +1 @@ +export var foo = 42; diff --git a/test/test.js b/test/test.js index 624c4a0..a69d215 100644 --- a/test/test.js +++ b/test/test.js @@ -110,64 +110,57 @@ describe( 'rollup', function () { format: 'cjs' })); - if ( config.error ) { + if ( config.generateError ) { unintendedError = new Error( 'Expected an error while generating output' ); } } catch ( err ) { - if ( config.error ) { - config.error( err ); + if ( config.generateError ) { + config.generateError( err ); } else { unintendedError = err; } } if ( unintendedError ) throw unintendedError; + if ( config.error || config.generateError ) return; var code; - try { - if ( config.babel ) { - code = babel.transform( result.code, { - blacklist: [ 'es6.modules' ], - loose: [ 'es6.classes' ] - }).code; - } else { - code = result.code; - } + if ( config.babel ) { + code = babel.transform( result.code, { + blacklist: [ 'es6.modules' ], + loose: [ 'es6.classes' ] + }).code; + } else { + code = result.code; + } - var module = { - exports: {} - }; + var module = { + exports: {} + }; - var context = extend({ - require: require, - module: module, - exports: module.exports, - assert: assert - }, config.context || {} ); + var context = extend({ + require: require, + module: module, + exports: module.exports, + assert: assert + }, config.context || {} ); - var contextKeys = Object.keys( context ); - var contextValues = contextKeys.map( function ( key ) { - return context[ key ]; - }); + var contextKeys = Object.keys( context ); + var contextValues = contextKeys.map( function ( key ) { + return context[ key ]; + }); - var fn = new Function( contextKeys, code ); - fn.apply( {}, contextValues ); + var fn = new Function( contextKeys, code ); + fn.apply( {}, contextValues ); - if ( config.error ) { - unintendedError = new Error( 'Expected an error while executing output' ); - } - - if ( config.exports ) config.exports( module.exports ); - if ( config.bundle ) config.bundle( bundle ); - } catch ( err ) { - if ( config.error ) { - config.error( err ); - } else { - unintendedError = err; - } + if ( config.error ) { + unintendedError = new Error( 'Expected an error while executing output' ); } + if ( config.exports ) config.exports( module.exports ); + if ( config.bundle ) config.bundle( bundle ); + if ( config.show || unintendedError ) { console.log( code + '\n\n\n' ); }