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

feat(config): add support for ES modules #2685

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions docs/config/02-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 5 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/module-types.feature
Original file line number Diff line number Diff line change
@@ -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
"""
15 changes: 15 additions & 0 deletions test/e2e/support/modules/minus.mjs
Original file line number Diff line number Diff line change
@@ -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)
})
})

5 changes: 5 additions & 0 deletions test/e2e/support/modules/plus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable no-unused-vars */
// Some code under test
function plus (a, b) {
return a + b
}
10 changes: 10 additions & 0 deletions test/e2e/support/modules/test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})