From 6ce97e65ef18ec86ced867b7a4223d8200bb5ed2 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Thu, 4 May 2017 08:21:13 -0700 Subject: [PATCH] feat(config): add support for ES modules - Add support for ES modules via `esModule` property in file config object --- docs/config/02-files.md | 5 +++++ lib/config.js | 8 +++++--- lib/file-list.js | 3 ++- lib/file.js | 3 ++- lib/middleware/karma.js | 3 ++- test/e2e/module-types.feature | 25 +++++++++++++++++++++++++ test/e2e/support/modules/minus.mjs | 15 +++++++++++++++ test/e2e/support/modules/plus.js | 5 +++++ test/e2e/support/modules/test.js | 10 ++++++++++ 9 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 test/e2e/module-types.feature create mode 100644 test/e2e/support/modules/minus.mjs create mode 100644 test/e2e/support/modules/plus.js create mode 100644 test/e2e/support/modules/test.js diff --git a/docs/config/02-files.md b/docs/config/02-files.md index d7d267484..fde05c962 100644 --- a/docs/config/02-files.md +++ b/docs/config/02-files.md @@ -52,6 +52,11 @@ Each pattern is either a simple string or an object with four properties: * **Default.** `true` * **Description.** Should the files be served by Karma's webserver? +### `esModule` +* **Type.** Boolean +* **Default.** `false` +* **Description.** Should the files be loaded as ECMAScript modules? + ### `nocache` * **Type.** Boolean * **Default.** `false` diff --git a/lib/config.js b/lib/config.js index deeaf6d7d..540b309eb 100644 --- a/lib/config.js +++ b/lib/config.js @@ -30,11 +30,12 @@ try { TYPE_SCRIPT_AVAILABLE = true } catch (e) {} -var Pattern = function (pattern, served, included, watched, nocache) { +var Pattern = function (pattern, served, included, watched, nocache, esModule) { this.pattern = pattern this.served = helper.isDefined(served) ? served : true this.included = helper.isDefined(included) ? included : true this.watched = helper.isDefined(watched) ? watched : true + this.esModule = helper.isDefined(esModule) ? esModule : false this.nocache = helper.isDefined(nocache) ? nocache : false this.weight = helper.mmPatternWeight(pattern) } @@ -44,7 +45,7 @@ Pattern.prototype.compare = function (other) { } var UrlPattern = function (url) { - Pattern.call(this, url, false, true, false, false) + Pattern.call(this, url, false, true, false, false, false) } var createPatternObject = function (pattern) { @@ -61,7 +62,8 @@ var createPatternObject = function (pattern) { pattern.served, pattern.included, pattern.watched, - pattern.nocache) + pattern.nocache, + pattern.esModule) } log.warn('Invalid pattern %s!\n\tObject is missing "pattern" property.', pattern) diff --git a/lib/file-list.js b/lib/file-list.js index ca9c9e2e2..0e2024461 100644 --- a/lib/file-list.js +++ b/lib/file-list.js @@ -183,7 +183,8 @@ List.prototype._refresh = function () { var mtime = mg.statCache[path].mtime var doNotCache = patternObject.nocache - var file = new File(path, mtime, doNotCache) + var esModule = patternObject.esModule + var file = new File(path, mtime, doNotCache, esModule) if (file.doNotCache) { log.debug('Not preprocessing "%s" due to nocache') diff --git a/lib/file.js b/lib/file.js index 4eabe8e00..2c261eddb 100644 --- a/lib/file.js +++ b/lib/file.js @@ -9,7 +9,7 @@ var _ = require('lodash') // Constructor -var File = function (path, mtime, doNotCache) { +var File = function (path, mtime, doNotCache, esModule) { // used for serving (processed path, eg some/file.coffee -> some/file.coffee.js) this.path = path @@ -23,6 +23,7 @@ var File = function (path, mtime, doNotCache) { this.isUrl = false this.doNotCache = _.isUndefined(doNotCache) ? false : doNotCache + this.esModule = esModule } File.prototype.toString = function () { diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index 04dd47a3f..ed0d31c59 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -178,6 +178,7 @@ var createKarmaMiddleware = function ( var file = files.included[i] var filePath = file.path var fileExt = path.extname(filePath) + var esModule = fileExt.endsWith('js') && file.esModule if (!files.included.hasOwnProperty(i)) { continue @@ -204,7 +205,7 @@ var createKarmaMiddleware = function ( } // The script tag to be placed - var scriptType = (SCRIPT_TYPE[fileExt] || 'text/javascript') + var scriptType = esModule ? 'module' : (SCRIPT_TYPE[fileExt] || 'text/javascript') // In case there is a JavaScript version specified and this is a Firefox browser if (jsVersion && jsVersion > 0 && isFirefox(request)) { diff --git a/test/e2e/module-types.feature b/test/e2e/module-types.feature new file mode 100644 index 000000000..af4d9e926 --- /dev/null +++ b/test/e2e/module-types.feature @@ -0,0 +1,25 @@ +Feature: ES Modules + In order to use Karma + As a person who wants to write great tests + I want to use different script types with Karma. + + Scenario: Simple middleware + Given a configuration with: + """ + files = [ + { pattern: 'modules/plus.js', esModule: false }, + { pattern: 'modules/minus.mjs', esModule: true }, + 'modules/test.js' + ]; + browsers = ['Firefox']; + plugins = [ + 'karma-jasmine', + 'karma-firefox-launcher' + ]; + """ + When I start Karma + Then it passes with: + """ + . + Firefox + """ diff --git a/test/e2e/support/modules/minus.mjs b/test/e2e/support/modules/minus.mjs new file mode 100644 index 000000000..06db0d5a0 --- /dev/null +++ b/test/e2e/support/modules/minus.mjs @@ -0,0 +1,15 @@ +// Some code under test +function minus (a, b) { + return a - b +} + +describe('minus', function () { + it('should pass', function () { + expect(true).toBe(true) + }) + + it('should work', function () { + expect(minus(3, 2)).toBe(1) + }) +}) + diff --git a/test/e2e/support/modules/plus.js b/test/e2e/support/modules/plus.js new file mode 100644 index 000000000..d7fd9e84e --- /dev/null +++ b/test/e2e/support/modules/plus.js @@ -0,0 +1,5 @@ +/* eslint-disable no-unused-vars */ +// Some code under test +function plus (a, b) { + return a + b +} diff --git a/test/e2e/support/modules/test.js b/test/e2e/support/modules/test.js new file mode 100644 index 000000000..e2883be26 --- /dev/null +++ b/test/e2e/support/modules/test.js @@ -0,0 +1,10 @@ +/* globals plus */ +describe('plus', function () { + it('should pass', function () { + expect(true).toBe(true) + }) + + it('should work', function () { + expect(plus(1, 2)).toBe(3) + }) +})