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

Automate client tests with karma #126

Merged
merged 19 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reporting:
dir: ./coverage/server
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
language: node_js
node_js:
- "node"
before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- istanbul cover ./node_modules/mocha/bin/_mocha
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
- karma start
- lcov-result-merger 'coverage/**/lcov.info' 'coverage/fullCoverage.info'
- cat ./coverage/fullCoverage.info | ./node_modules/.bin/coveralls
50 changes: 50 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Karma configuration
Copy link
Member

Choose a reason for hiding this comment

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

any way to suppress this error?

screen shot 2017-04-18 at 12 57 23 am


module.exports = function(config) {
var configuration = {
basePath: '',
frameworks: ['mocha'],
files: [
'teddy.js',
'test/models/*.js',
'node_modules/chai/chai.js',
'node_modules/chai-string/chai-string.js',
{pattern: 'test/templates/**/*', included: false},
'test/*.js',
'test/client.html'
],
reporters: ['progress', 'coverage'],
port: 8000,
proxies: {
'/templates/': '/base/test/templates/',
},
preprocessors: {
'teddy.js': ['coverage']
},
coverageReporter: {
type: 'lcov',
dir: 'coverage/',
subdir: function(browser) {
return browser.toLowerCase().split(/[ /-]/)[0];
}
},
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
singleRun: true,
browsers: ['Chrome'],
customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
concurrency: 1
};

if (process.env.TRAVIS) {
configuration.browsers = ['Chrome_travis_ci'];
}

config.set(configuration);
};
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@
"dependencies": {
},
"devDependencies": {
"eslint": "3.13.1",
"eslint": "3.19.0",
"mocha": "3.2.0",
"chai": "3.5.0",
"chai-string": "1.3.0",
"istanbul": "0.4.5",
"mocha-lcov-reporter": "1.3.0",
"coveralls": "2.12.0",
"cross-env": "4.0.0"
"coveralls": "2.13.0",
"cross-env": "4.0.0",
"karma": "1.6.0",
"karma-cli": "1.0.1",
"karma-mocha": "1.3.0",
"karma-coverage": "1.1.1",
"karma-chrome-launcher": "2.0.0",
"karma-coverage-allsources": "0.0.4",
"lcov-result-merger": "^1.2.0"
},
"eslintConfig": {
"env": {
Expand Down Expand Up @@ -56,7 +63,8 @@
"scripts": {
"test": "cross-env NODE_ENV=test mocha --recursive test",
"eslint": "eslint .",
"cover": "cross-env NODE_ENV=cover istanbul cover _mocha --recursive test"
"cover": "cross-env NODE_ENV=cover istanbul cover _mocha --recursive test",
Copy link
Member

Choose a reason for hiding this comment

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

change cover to coverage

Copy link
Member

Choose a reason for hiding this comment

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

actually no, alias coverage to cover, have both

"karma": "karma start"
Copy link
Member

Choose a reason for hiding this comment

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

  • change this to test-client
  • add another for test-server
  • make npm test run both the server and client tests

},
"pre-commit": [
"test",
Expand Down
26 changes: 15 additions & 11 deletions test/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,21 @@ <h2>Warning: these tests can only be run from a web server.</h2>
<script src='../node_modules/mocha/mocha.js'></script>
<script src='../node_modules/chai/chai.js'></script>
<script src='../node_modules/chai-string/chai-string.js'></script>

<!-- include mocha tests -->
<script>mocha.setup('bdd');</script>
<script src='conditionals.js'></script>
<script src='includes.js'></script>
<script src='looping.js'></script>
<script src='misc.js'></script>

<!-- run tests -->
<script>
var assert = chai.assert,
templateList,
templateLength,
counter = 0,
crossOriginError,
request,
i;

templateList = [
Expand Down Expand Up @@ -417,7 +425,8 @@ <h2>Warning: these tests can only be run from a web server.</h2>
'misc/variableObjectProperty.html',
'misc/emptyModelMarkup.html',
'misc/infiniteIncludeTemplate.html',
'misc/varNotInModel.html'
'misc/varNotInModel.html',
'misc/templateToMinify.html'
];
templateLength = templateList.length;

Expand All @@ -438,9 +447,11 @@ <h2>Warning: these tests can only be run from a web server.</h2>
if (crossOriginError) {
crossOriginError.parentNode.removeChild(crossOriginError);
}
if (template === 'misc/templateToMinify.html') {
teddy.minify(true);
}
teddy.templates[template] = teddy.compile(req.response || req.responseText);
counter++;
if (counter === templateLength) {
if (counter++ === templateLength - 1) {
Copy link
Member

Choose a reason for hiding this comment

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

clever but no

mocha.run();
}
}
Expand All @@ -449,12 +460,5 @@ <h2>Warning: these tests can only be run from a web server.</h2>
})(i);
}
</script>

<!-- run tests -->
<script>mocha.setup('bdd');</script>
<script src='conditionals.js'></script>
<script src='includes.js'></script>
<script src='looping.js'></script>
<script src='misc.js'></script>
</body>
</html>
12 changes: 7 additions & 5 deletions test/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ describe('Conditionals', function() {
before(function() {
teddy.setTemplateRoot('test/templates');
model = makeModel();
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
}
}
});

Expand Down
12 changes: 7 additions & 5 deletions test/includes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ describe('Includes', function() {
before(function() {
teddy.setTemplateRoot('test/templates');
model = makeModel();
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
}
}
});

Expand Down
28 changes: 17 additions & 11 deletions test/looping.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ if (typeof module !== 'undefined') {
assert = chai.assert,
model,
makeModel = require('./models/model'),
loopMs = 50,
teddy = require('../teddy');
chai.use(chaiString);
}
else {
var loopMs = 400;
}

describe('Looping', function() {
before(function() {
teddy.setTemplateRoot('test/templates');
model = makeModel();
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
}
}
});

Expand Down Expand Up @@ -65,7 +71,7 @@ describe('Looping', function() {
done();
});

it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
var start, end, time;
start = new Date().getTime();

Expand All @@ -74,7 +80,7 @@ describe('Looping', function() {
end = new Date().getTime();
time = end - start;

assert.isAtMost(time, 50);
assert.isAtMost(time, loopMs);
done();
});

Expand All @@ -92,7 +98,7 @@ describe('Looping', function() {
done();
});

it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
var start, end, time;
start = new Date().getTime();

Expand All @@ -101,7 +107,7 @@ describe('Looping', function() {
end = new Date().getTime();
time = end - start;

assert.isAtMost(time, 50);
assert.isAtMost(time, loopMs);
done();
});

Expand All @@ -119,7 +125,7 @@ describe('Looping', function() {
done();
});

it('should loop through same array of 5000 elements in < 50ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
it('should loop through same array of 5000 elements in < ' + loopMs + 'ms during second attempt due to caching (looping/largeDataSet.html)', function(done) {
var start, end, time;
start = new Date().getTime();

Expand All @@ -129,7 +135,7 @@ describe('Looping', function() {
time = end - start;

teddy.cacheRenders(false);
assert.isAtMost(time, 50);
assert.isAtMost(time, loopMs);
done();
});

Expand Down
32 changes: 19 additions & 13 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if (typeof module !== 'undefined') {
assert = chai.assert,
model,
makeModel = require('./models/model'),
verbosity = '',
verbosity,
teddy = require('../teddy');
chai.use(chaiString);
}
Expand All @@ -13,11 +13,13 @@ describe('Misc', function() {
before(function() {
teddy.setTemplateRoot('test/templates');
model = makeModel();
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
}
}
});

Expand Down Expand Up @@ -157,6 +159,7 @@ describe('Misc', function() {
});

it('should set each verbosity level', function(done) {
verbosity = '';
teddy.setVerbosity();
verbosity += teddy.params.verbosity + ', ';
teddy.setVerbosity('none');
Expand All @@ -173,18 +176,21 @@ describe('Misc', function() {
verbosity += teddy.params.verbosity;

assert.equal(verbosity, '1, 0, 0, 2, 2, 3, 3');
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
verbosity = '';
if (typeof process === 'object') {
if (process.env.NODE_ENV === 'test') {
teddy.setVerbosity(0);
}
else if (process.env.NODE_ENV === 'cover') {
teddy.setVerbosity(3);
}
}
done();
});

it('should minify template with internal minifier (misc/plainHTML.html)', function(done) {
it('should minify template with internal minifier (misc/templateToMinify.html)', function(done) {
teddy.minify(true);
assert.equal(teddy.compile('misc/plainHTML.html', model), '<!DOCTYPE html><html lang=\'en\'> <head> <meta charset=\'utf-8\'> <meta name=\'viewport\' content=\'width=device-width,initial-scale=1\'> <meta name=\'format-detection\' content=\'telephone=no\'> <title>Plain HTML</title> <link rel=\'stylesheet\' href=\'/css/styles.css\'> </head> <body> <main> <p>This template contains no teddy tags. Just HTML.</p> </main> <script type=\'text/javascript\' src=\'/js/main.js\'></script> </body></html>');
assert.equal(teddy.render('misc/templateToMinify.html', model), '<!DOCTYPE html><html lang=\'en\'> <head> <meta charset=\'utf-8\'> <meta name=\'viewport\' content=\'width=device-width,initial-scale=1\'> <meta name=\'format-detection\' content=\'telephone=no\'> <title>Plain HTML</title> </head> <body> <main> <p>This template contains no teddy tags. Just HTML.</p> </main> </body></html>');
teddy.minify(false);
done();
});
Expand Down
14 changes: 14 additions & 0 deletions test/templates/misc/templateToMinify.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<meta name='format-detection' content='telephone=no'>
<title>Plain HTML</title>
</head>
<body>
<main>
<p>This template contains no teddy tags. Just HTML.</p>
</main>
</body>
</html>