karma-electron
Advanced tools
Comparing version 4.1.3 to 5.0.0
# karma-electron changelog | ||
5.0.0 - Corrected `__filename`/`__dirname`/`module` inaccuracies between `karma-electron` and standalone Electron | ||
4.1.3 - Upgraded to `jscs@3.0.7` and `jshint@2.9.3` to resolve lint errors | ||
@@ -3,0 +5,0 @@ |
@@ -8,2 +8,12 @@ // Load our dependencies | ||
// Resolve filepath to Karma's `context.html` and `debug.html` | ||
// https://github.com/karma-runner/karma/blob/v1.3.0/lib/middleware/karma.js#L138-L157 | ||
// https://github.com/karma-runner/karma/blob/v1.3.0/lib/web-server.js#L33-L37 | ||
// https://github.com/karma-runner/karma/blob/v1.3.0/lib/middleware/common.js#L30-L70 | ||
var karmaWebServerFilepath = require.resolve('karma/lib/web-server'); | ||
var karmaStaticDirectoryFilepath = path.normalize(path.join(path.dirname(karmaWebServerFilepath), '/../static')); | ||
// Example: /home/todd/github/karma-electron/node_modules/karma/static/context.html | ||
var karmaDefaultContextFile = karmaStaticDirectoryFilepath + '/context.html'; | ||
var karmaDefaultDebugFile = karmaStaticDirectoryFilepath + '/debug.html'; | ||
// Load our template | ||
@@ -16,2 +26,5 @@ // DEV: We minify to remove impact of line numbers | ||
var minifiedTemplateStr = templateStr.replace(/\/\/[^\n]+/g, '\n').replace(/\n/g, ''); | ||
// DEV: Fix up JSCS work arounds | ||
minifiedTemplateStr = minifiedTemplateStr.replace(/{{if_/g, '{{#') | ||
.replace(/{{notif_/g, '{{^').replace(/{{end_/g, '{{/'); | ||
// DEV: We inject a newline after content to prevent `//` comments from breaking our closure | ||
@@ -22,4 +35,6 @@ minifiedTemplateStr = minifiedTemplateStr.replace(/(content}})/, '$1\n'); | ||
// Define our framework to inject our `node-integration` | ||
var $inject = ['config.basePath']; | ||
function createElectronPreprocessor(karmaBasePath) { | ||
var $inject = ['config.basePath', 'config.client.loadScriptsViaRequire', | ||
'config.customContextFile', 'config.customDebugFile']; | ||
function createElectronPreprocessor(karmaBasePath, karmaLoadScriptsViaRequire, | ||
karmaCustomContextFile, karmaCustomDebugFile) { | ||
// Generate our preprocessor function | ||
@@ -34,2 +49,5 @@ function electronPreprocessor(content, file, done) { | ||
karmaBasePath: jsStringEscape(karmaBasePath), | ||
karmaContextFile: jsStringEscape(karmaCustomContextFile || karmaDefaultContextFile), | ||
karmaDebugFile: jsStringEscape(karmaCustomDebugFile || karmaDefaultDebugFile), | ||
loadScriptsViaRequire: !!karmaLoadScriptsViaRequire, | ||
sep: jsStringEscape(path.sep) | ||
@@ -36,0 +54,0 @@ }); |
(function () { | ||
// Set up local filename and dirname | ||
// Set up local filename, dirname, and `module.filename` | ||
// DEV: `__filename` would be a path to Electron since we are loading an HTTP URL | ||
// Once we perform a `require`, it will be using our `require's` context :+1: | ||
// /home/todd/github/karma-electron/node_modules/electron/dist/resources/electron.asar/renderer/init.js | ||
// This overrides to user's expectations of Karma's `file://` filepaths | ||
// DEV: Ignore JSHint so we can override `__filename` in our closure | ||
// DEV: We use `window.require` sniffing to prevent emulating a Node.js environment in a non-Node.js one | ||
var __filename = window.require ? '{{!filename}}' : undefined; // jshint ignore:line | ||
var __dirname = window.require ? '{{!dirname}}' : undefined; | ||
var __filename, __dirname; | ||
if (window.require) { | ||
__filename = window.location.pathname !== '/debug.html' ? '{{!karmaContextFile}}' : '{{!karmaDebugFile}}'; | ||
__dirname = window.require('path').dirname(__filename); | ||
if (window.module) { | ||
module.filename = __filename; | ||
} | ||
} | ||
@@ -41,8 +48,15 @@ // Save original require/require.resolve | ||
// Inject our content | ||
// If we want to require our content, then load it via `require` | ||
// DEV: To work around JSCS errors, we are using `if_`, `else_`, and `end_` | ||
// jscs:disable | ||
// jshint ignore:start | ||
{{!content}} | ||
{{if_loadScriptsViaRequire}} | ||
require('{{!filename}}'); | ||
{{end_loadScriptsViaRequire}} | ||
// Otherwise, inject our content | ||
{{notif_loadScriptsViaRequire}} | ||
{{!content}} | ||
{{end_loadScriptsViaRequire}} | ||
// jshint ignore:end | ||
// jscs:enable | ||
}()); |
{ | ||
"name": "karma-electron", | ||
"description": "Karma launcher and preprocessor for Electron", | ||
"version": "4.1.3", | ||
"version": "5.0.0", | ||
"homepage": "https://github.com/twolfson/karma-electron", | ||
@@ -28,8 +28,11 @@ "author": { | ||
"test": "npm run test-karma-all && npm run lint", | ||
"test-karma-all": "npm run test-karma-single-run && npm run test-karma-failure && npm run test-karma-uncaught-exception && npm run test-karma-karma && npm run test-karma-phantomjs && npm run test-karma-eof-comment && npm run test-karma-source-map", | ||
"test-karma-all": "npm run test-karma-single-run && npm run test-karma-failure && npm run test-karma-uncaught-exception && npm run test-karma-karma && npm run test-karma-phantomjs && npm run test-karma-eof-comment && npm run test-karma-source-map && npm run test-karma-node-require && npm run test-karma-custom-context-file", | ||
"test-karma-continuous": "karma start test/integration-test/karma.conf.js --no-single-run", | ||
"test-karma-custom-context-file-comment": "# DEV: We need to manually test custom debug file support since it's designed for debugging, not testing", | ||
"test-karma-custom-context-file": "cross-env TEST_TYPE=CUSTOM_CONTEXT_FILE karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-eof-comment": "cross-env TEST_TYPE=EOF_COMMENT karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-failure": "node bin/verify-failure.js TEST_TYPE=FAILURE karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-karma-comment": "# DEV: We run `test-karma-karma` twice to verify no user-data-dir is leaking", | ||
"test-karma-karma": "cross-env TEST_TYPE=KARMA karma start test/integration-test/karma.conf.js --single-run && cross-env TEST_TYPE=KARMA karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-failure": "node bin/verify-failure.js TEST_TYPE=FAILURE karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-node-require": "cross-env TEST_TYPE=NODE_REQUIRE karma start test/integration-test/karma.conf.js --single-run", | ||
"test-karma-phantomjs": "cross-env TEST_TYPE=PHANTOMJS karma start test/integration-test/karma.conf.js --single-run --browsers PhantomJS", | ||
@@ -36,0 +39,0 @@ "test-karma-single-run": "karma start test/integration-test/karma.conf.js --single-run", |
@@ -13,3 +13,3 @@ # karma-electron [![Build status](https://travis-ci.org/twolfson/karma-electron.svg?branch=master)](https://travis-ci.org/twolfson/karma-electron) [![Build status](https://ci.appveyor.com/api/projects/status/urgpvcip7kl9q2ih/branch/master?svg=true)](https://ci.appveyor.com/project/twolfson/karma-electron-launcher/branch/master) | ||
- Tested via CI on Linux and Windows | ||
- Support for Node.js integration in the renderer process (e.g. `process`, `require`, `__filename`) | ||
- Support for Node.js integration in the renderer process (e.g. `node_modules`, `__filename`, relative paths for `require`) | ||
- Support for hidden browser windows | ||
@@ -30,2 +30,12 @@ - Support for isolated test runs to prevent cookie/localStorage pollution | ||
## Breaking changes in 5.0.0 | ||
We have corrected inaccuracies with `file://` behavior from Electron. For example: | ||
- `__filename` is now Karma's `context.html` | ||
- Relative paths for `require` resolve from Karma's `context.html` directory | ||
We have transferred support for this to the option `client.loadScriptsViaRequire` which loads scripts via `require` and has the original expected Node.js behavior | ||
For more information, see https://github.com/twolfson/karma-electron/issues/11 | ||
## Getting Started | ||
@@ -45,3 +55,3 @@ On a project that has been set up with `karma init` already, install the module via: | ||
// If you would like Node integration support (e.g. `require`) | ||
// If you would like Node.js integration support (e.g. `require`) | ||
// then, you must include this in `preprocessors` and `client` | ||
@@ -76,3 +86,24 @@ // DEV: preprocessors is for backfilling `__filename` and local `require` paths | ||
### Configuration | ||
### Script configuration | ||
We support the following configurations: | ||
- client `Object` - Container for configuring child windows loaded from Karma | ||
- loadScriptsViaRequire `Boolean` - Load scripts via `require` instead of `<script src=` | ||
- This sets `__filename`, `__dirname`, and `module` to match the script instead of Karma's `context.html` | ||
- By default, this is `false` and we directly load the original scripts content | ||
**Example:** | ||
```js | ||
module.exports = function (config) { | ||
config.set({ | ||
// Use `require` instead of `<script src=` to load scripts | ||
client: { | ||
loadScriptsViaRequire: true | ||
} | ||
}); | ||
}; | ||
``` | ||
### Launcher configuration | ||
We support configuration via Karma's custom launcher inheritance: | ||
@@ -102,2 +133,3 @@ | ||
} | ||
}); | ||
}; | ||
@@ -104,0 +136,0 @@ ``` |
@@ -13,4 +13,10 @@ // Karma configuration | ||
var uncaughtExceptionTest = 'uncaught-exception-test.js'; | ||
var nodeCommonTest = 'node-common-test.js'; | ||
var nodeRequireTest = 'node-require-test.js'; | ||
var nodeScriptSrcTest = 'node-script-src-test.js'; | ||
var customContextFileTest = 'custom-context-file-test.js'; | ||
var testFiles = ['*-test.js']; | ||
var excludeFiles = new Set([failureTest, karmaTest, phantomJsTest, sourceMapTest, uncaughtExceptionTest]); | ||
var excludeFiles = new Set([ | ||
customContextFileTest, failureTest, karmaTest, | ||
nodeRequireTest, phantomJsTest, sourceMapTest, uncaughtExceptionTest]); | ||
@@ -36,2 +42,9 @@ // If we are testing uncaught exceptions, then update our tests | ||
excludeFiles.delete(sourceMapTest); | ||
} else if (process.env.TEST_TYPE === 'CUSTOM_CONTEXT_FILE') { | ||
testFiles = [customContextFileTest]; | ||
excludeFiles.delete(customContextFileTest); | ||
} else if (process.env.TEST_TYPE === 'NODE_REQUIRE') { | ||
testFiles = [nodeCommonTest, nodeRequireTest]; | ||
excludeFiles.add(nodeScriptSrcTest); | ||
excludeFiles.delete(nodeRequireTest); | ||
} else if (process.env.TEST_TYPE) { | ||
@@ -46,2 +59,8 @@ throw new Error('Unrecognized test type "' + process.env.TEST_TYPE + '"'); | ||
// Custom context and debug files | ||
customContextFile: process.env.TEST_TYPE === 'CUSTOM_CONTEXT_FILE' ? | ||
__dirname + '/test-files/custom-context.html' : null, | ||
customDebugFile: process.env.TEST_TYPE === 'CUSTOM_CONTEXT_FILE' ? | ||
__dirname + '/test-files/custom-debug.html' : null, | ||
// frameworks to use | ||
@@ -59,2 +78,5 @@ // available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
client: { | ||
// DEV: We use `client` as these options affect the client side of `karma` | ||
// Based on https://github.com/karma-runner/karma-mocha/tree/v1.1.1#configuration | ||
loadScriptsViaRequire: process.env.TEST_TYPE === 'NODE_REQUIRE', | ||
useIframe: false | ||
@@ -61,0 +83,0 @@ }, |
@@ -5,2 +5,5 @@ // Load in our dependencies | ||
var request = require('request'); | ||
// DEV: This resolve from `context.html` in `karma` so we need a lot of `../` | ||
// DEV: We avoid using `loadScriptsViaRequire` as we want to test trailing source map behavior | ||
var sourceMapTestFilepath = __dirname + '/../../../test/integration-test/source-map-test.js'; | ||
@@ -11,4 +14,2 @@ // Start our tests | ||
// Retrieve karma-electron processed HTTP body | ||
// DEV: As a sanity check, verify we are in the same file | ||
assert(__filename.match(/source-map-test.js$/)); | ||
var that = this; | ||
@@ -27,3 +28,3 @@ request('http://localhost:9876/base/source-map-test.js', function handleRequest (err, res, body) { | ||
// DEV: We might have a trailing new line so we fallback | ||
var fsLines = fs.readFileSync(__filename, 'utf8').split(/\n/g); | ||
var fsLines = fs.readFileSync(sourceMapTestFilepath, 'utf8').split(/\n/g); | ||
var fsLastLine = fsLines[fsLines.length - 1] || fsLines[fsLines.length - 2]; | ||
@@ -30,0 +31,0 @@ |
Sorry, the diff of this file is not supported yet
53509
36
746
150
15