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(init): generate test-main.(js/coffee) for RequireJS projects #897

Closed
wants to merge 5 commits 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
52 changes: 29 additions & 23 deletions docs/plus/01-requirejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,41 @@ asynchronously as dependencies must be fetched before the tests are run.
The `test/main-test.js` file ends up looking like this:

```javascript
var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
var allTestFiles = [];
var TEST_REGEXP = /test\.js$/;

var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
}
});

requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base/src',
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/src',

paths: {
'jquery': '../lib/jquery',
'underscore': '../lib/underscore',
},
// example of using shim, to load non AMD libraries (such as underscore and jquery)
paths: {
'jquery': '../lib/jquery',
'underscore': '../lib/underscore',
},

shim: {
'underscore': {
exports: '_'
}
},
shim: {
'underscore': {
exports: '_'
}
},

// ask Require.js to load these files (all our tests)
deps: tests,
// dynamically load all test files
deps: allTestFiles,

// start test run, once Require.js is done
callback: window.__karma__.start
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
```

Expand Down
24 changes: 21 additions & 3 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ var questions = [{
'Enter empty string to move to the next question.',
multiple: true,
validate: validatePattern
}, {
id: 'generateTestMain',
question: 'Do you wanna generate a bootstrap file for RequireJS?',
hint: 'This will generate test-main.js/coffee that configures RequiseJS and starts the tests.',
options: ['no', 'yes'],
boolean: true,
condition: function(answers) {return answers.requirejs;}
}, {
id: 'includedFiles',
question: 'Which files do you want to include with <script> tag ?',
Expand All @@ -133,7 +140,7 @@ var questions = [{
'Enter empty string to move to the next question.',
multiple: true,
validate: validatePattern,
condition: function(answers) {return answers.requirejs;}
condition: function(answers) {return answers.requirejs && !answers.generateTestMain;}
}, {
id: 'autoWatch',
question: 'Do you want Karma to watch all the files and run the tests on change ?',
Expand Down Expand Up @@ -174,6 +181,7 @@ var processAnswers = function(answers, basePath) {
onlyServedFiles: [],
exclude: answers.exclude,
autoWatch: answers.autoWatch,
generateTestMain: answers.generateTestMain,
browsers: answers.browsers,
frameworks: [],
preprocessors: {}
Expand All @@ -185,7 +193,7 @@ var processAnswers = function(answers, basePath) {

if (answers.requirejs) {
processedAnswers.frameworks.push('requirejs');
processedAnswers.files = answers.includedFiles;
processedAnswers.files = answers.includedFiles || [];
processedAnswers.onlyServedFiles = answers.files;
}

Expand Down Expand Up @@ -239,12 +247,22 @@ exports.init = function(config) {
sm.process(questions, function(answers) {
var cwd = process.cwd();
var configFile = config.configFile || 'karma.conf.js';
var requirejsConfigFile = (/\.coffee$/).test(configFile) ? 'test-main.coffee' : 'test-main.js';
var formatter = formatters.createForPath(configFile);
var processedAnswers = processAnswers(answers, getBasePath(configFile, cwd));
var configFilePath = path.resolve(cwd, configFile);
var requirejsConfigFilePath = path.resolve(cwd, requirejsConfigFile);

formatter.writeConfigFile(configFilePath, processedAnswers);

if (processedAnswers.generateTestMain) {
processedAnswers.files.push(requirejsConfigFile);
formatter.writeRequirejsConfigFile(requirejsConfigFilePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the test-main.js has to be added to files (as included: true), you can do this inside processAnswers(), add it into processedAnswers.files

console.log(colorScheme.success(
'RequireJS bootstrap file generated at "' + requirejsConfigFile + '".\n'
));
}

formatter.writeConfigFile(configFilePath, processedAnswers);
console.log(colorScheme.success('Config file generated at "' + configFilePath + '".\n'));
});
};
13 changes: 13 additions & 0 deletions lib/init/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var util = require('util');

var JS_TEMPLATE_PATH = __dirname + '/../../config.tpl.js';
var COFFEE_TEMPLATE_PATH = __dirname + '/../../config.tpl.coffee';
var JS_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.js';
var COFFEE_REQUIREJS_TEMPLATE_PATH = __dirname + '/../../requirejs.config.tpl.coffee';
var COFFEE_REGEXP = /\.coffee$/;
var LIVE_TEMPLATE_PATH = __dirname + '/../../config.tpl.ls';
var LIVE_REGEXP = /\.ls$/;
Expand Down Expand Up @@ -35,6 +37,7 @@ var JavaScriptFormatter = function() {
};

this.TEMPLATE_FILE_PATH = JS_TEMPLATE_PATH;
this.REQUIREJS_TEMPLATE_FILE = JS_REQUIREJS_TEMPLATE_PATH;

this.formatFiles = function(includedFiles, onlyServedFiles) {
var files = includedFiles.map(quote);
Expand Down Expand Up @@ -84,12 +87,22 @@ var JavaScriptFormatter = function() {
this.writeConfigFile = function(path, answers) {
fs.writeFileSync(path, this.generateConfigFile(answers));
};

this.generateRequirejsConfigFile = function () {
var template = fs.readFileSync(this.REQUIREJS_TEMPLATE_FILE).toString();
return template;
};

this.writeRequirejsConfigFile = function (path) {
fs.writeFileSync(path, this.generateRequirejsConfigFile());
};
};

var CoffeeFormatter = function() {
JavaScriptFormatter.call(this);

this.TEMPLATE_FILE_PATH = COFFEE_TEMPLATE_PATH;
this.REQUIREJS_TEMPLATE_FILE = COFFEE_REQUIREJS_TEMPLATE_PATH;
};

var LiveFormatter = function() {
Expand Down
19 changes: 19 additions & 0 deletions requirejs.config.tpl.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
allTestFiles = []
TEST_REGEXP = /test\.coffee$/
pathToModule = (path) ->
path.replace(/^\/base\//, "").replace /\.coffee$/, ""

Object.keys(window.__karma__.files).forEach (file) ->
# Normalize paths to RequireJS module names.
allTestFiles.push pathToModule(file) if TEST_REGEXP.test(file)
return

require.config
# Karma serves files under /base, which is the basePath from your config file
baseUrl: "/base"

# dynamically load all test files
deps: allTestFiles

# we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
24 changes: 24 additions & 0 deletions requirejs.config.tpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var allTestFiles = [];
var TEST_REGEXP = /test\.js$/;

var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});

require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base',

// dynamically load all test files
deps: allTestFiles,

// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
46 changes: 46 additions & 0 deletions test/unit/init.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ describe 'init', ->

# excludes
machine.onLine ''
machine.onLine ''

# generate test-main
machine.onLine 'no'

# included files
machine.onLine 'test/main.js'
Expand All @@ -192,6 +196,48 @@ describe 'init', ->
# autoWatch
machine.onLine 'yes'

it 'should generate the test-main for requirejs', (done) ->
machine.process m.questions, (answers) ->
basePath = m.getBasePath '../karma.conf.js', '/some/path'
processedAnswers = m.processAnswers answers, basePath
generatedConfigCode = formatter.generateConfigFile processedAnswers
config = evaluateConfigCode generatedConfigCode

# expect correct processedAnswers
expect(processedAnswers.generateTestMain).to.be.ok
expect(processedAnswers.files).to.contain 'test-main.js'
# expect correct configuration
expect(config.frameworks).to.contain 'requirejs'
for pattern in config.files.slice(1)
expect(pattern.included).to.equal false
done()

# frameworks
machine.onLine 'jasmine'
machine.onLine ''

# requirejs
machine.onLine 'yes'

# browsers
machine.onLine 'Chrome'
machine.onLine ''

# files
machine.onLine 'src/**/*.js'
machine.onLine 'test/**/*.js'
machine.onLine ''

# excludes
machine.onLine ''
machine.onLine ''

# generate test-main
machine.onLine 'yes'

# autoWatch
machine.onLine 'yes'


it 'should add coffee preprocessor', (done) ->
machine.process m.questions, (answers) ->
Expand Down