Skip to content

Commit

Permalink
tests are browser compatible, karma tests with sauceLabs, readme on u…
Browse files Browse the repository at this point in the history
…sing in browser, #21
  • Loading branch information
epoberezkin committed Jul 24, 2015
1 parent 6c93d03 commit 6b9bc9e
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ build/Release
node_modules

.DS_Store

# Browserified browser tests
.browser

# Ajv bundle
ajv.bundle.js
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ if (!valid) console.log(ajv.errorsText());
ajv compiles schemas to functions and caches them in all cases (using stringified schema as a key - using [json-stable-stringify](https://github.com/substack/json-stable-stringify)), so that the next time the same schema is used (not necessarily the same object instance) it won't be compiled again.


## Using in browser

You can require ajv directly from the code you browserify - in this case ajv will be a part of your bundle.

If you need to use ajv in several bundles you probably don't won't to have it included separately in each one. In this case you can create browserify bundle using `bin/create-bundle` script (thanks to [siddo420](https://github.com/siddo420)).


Then you need to load ajv in the browser:
```
<script src="ajv.bundle.js"></script>
```

Then you can use it as shown above - `require` will be global and you can `require('ajv')`.

Ajv passes tests with these browsers:

[![Sauce Test Status](https://saucelabs.com/browser-matrix/epoberezkin.svg)](https://saucelabs.com/u/epoberezkin)


## Formats

The following formats are supported for string validation with "format" keyword:
Expand Down
3 changes: 3 additions & 0 deletions bin/create-bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

browserify -r ./lib/ajv.js:ajv -o ajv.bundle.js
5 changes: 5 additions & 0 deletions bin/prepare-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

find spec -type f -name '*.spec.js' | \
xargs -I {} sh -c \
'export f="{}"; browserify $f -t require-globify -r js-beautify -o ${f/spec/.browser};'
59 changes: 59 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Karma configuration
// Generated on Thu Mar 13 2014 14:12:04 GMT-0700 (PDT)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha'],


// list of files / patterns to load in the browser
files: [
'.browser/*.js'
],

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
137 changes: 137 additions & 0 deletions karma.sauce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
'use strict';

var fs = require('fs');

module.exports = function(config) {

// Use ENV vars on Travis and sauce.json locally to get credentials
if (!process.env.SAUCE_USERNAME) {
if (!fs.existsSync('sauce.json')) {
console.log('Create a sauce.json with your credentials based on the sauce-sample.json file.');
process.exit(1);
} else {
process.env.SAUCE_USERNAME = require('./sauce').username;
process.env.SAUCE_ACCESS_KEY = require('./sauce').accessKey;
}
}

// Browsers to run on Sauce Labs
var customLaunchers = {
'SL_Chrome_27': {
base: 'SauceLabs',
browserName: 'chrome',
version: '27'
},
'SL_Chrome_43': {
base: 'SauceLabs',
browserName: 'chrome',
version: '43'
},
'SL_Chrome_beta': {
base: 'SauceLabs',
browserName: 'chrome',
version: 'beta'
},
'SL_InternetExplorer_9': {
base: 'SauceLabs',
browserName: 'internet explorer',
version: '9'
},
'SL_InternetExplorer_10': {
base: 'SauceLabs',
browserName: 'internet explorer',
version: '10'
},
'SL_InternetExplorer_11': {
base: 'SauceLabs',
browserName: 'internet explorer',
version: '11'
},
'SL_FireFox_4': {
base: 'SauceLabs',
browserName: 'firefox',
version: '4'
},
'SL_FireFox_17': {
base: 'SauceLabs',
browserName: 'firefox',
version: '17'
},
'SL_FireFox_24': {
base: 'SauceLabs',
browserName: 'firefox',
version: '24'
},
'SL_FireFox': {
base: 'SauceLabs',
browserName: 'firefox'
},
'SL_Safari_5': {
base: 'SauceLabs',
browserName: 'safari',
version: '5'
},
'SL_Safari_6': {
base: 'SauceLabs',
browserName: 'safari',
version: '6'
},
'SL_Safari_7': {
base: 'SauceLabs',
browserName: 'safari',
version: '7'
},
'SL_iPhone_8': {
base: 'SauceLabs',
browserName: 'iphone',
version: '8.1'
}
};


config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha'],


// list of files / patterns to load in the browser
files: [
'.browser/*.js'
],


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots', 'saucelabs'],


// web server port
port: 9876,

colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

sauceLabs: {
testName: 'Ajv'
},
captureTimeout: 300000,
browserNoActivityTimeout: 120000,

customLaunchers: customLaunchers,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: Object.keys(customLaunchers),
singleRun: true
});
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.6.7",
"version": "0.6.8",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"scripts": {
Expand Down Expand Up @@ -29,7 +29,11 @@
"dot": "^1.0.3",
"glob": "^5.0.10",
"js-beautify": "^1.5.6",
"json-schema-test": "0.0.3",
"json-schema-test": "0.1.1",
"karma": "^0.13.3",
"karma-chrome-launcher": "^0.2.0",
"karma-mocha": "^0.2.0",
"karma-sauce-launcher": "^0.2.14",
"mocha": "^2.2.5",
"watch": "^0.16.0"
}
Expand Down
4 changes: 4 additions & 0 deletions sauce.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "epoberezkin",
"accessKey": "6ab61c89-e0d3-46bc-8719-aa44d58cea84"
}
2 changes: 1 addition & 1 deletion spec/errors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('Validation errors', function () {

function shouldBeInvalid(validate, data, numErrors) {
validate(data) .should.equal(false);
validate.errors.length .should.equal(numErrors || 1);
should.equal(validate.errors.length, numErrors || 1)
}


Expand Down
31 changes: 26 additions & 5 deletions spec/json-schema.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var jsonSchemaTest = require('json-schema-test');
var jsonSchemaTest = require('json-schema-test')
, path = require('path');


var Ajv = require('../lib/ajv')
Expand All @@ -26,10 +27,7 @@ addRemoteRefs();


jsonSchemaTest([ ajv, fullAjv ], {
suites: {
'JSON-Schema tests draft4': './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json',
'Advanced schema tests': './tests/{**/,}*.json'
},
suites: testSuites(),
only: [
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
// 'maximum', 'minimum', 'multipleOf', 'maxLength', 'minLength', 'pattern',
Expand All @@ -49,6 +47,29 @@ jsonSchemaTest([ ajv, fullAjv ], {
});


function testSuites() {
if (typeof window == 'object') {
var suites = {
'JSON-Schema tests draft4': require('./JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json', {mode: 'hash'}),
'Advanced schema tests': require('./tests/{**/,}*.json', {mode: 'hash'})
};
for (var suiteName in suites) {
var suite = suites[suiteName];
var suiteArr = [];
for (var testSetName in suite)
suiteArr.push({ name: testSetName, test: suite[testSetName] });
suites[suiteName] = suiteArr;
}
} else {
var suites = {
'JSON-Schema tests draft4': './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json',
'Advanced schema tests': './tests/{**/,}*.json'
}
}
return suites;
}


function addRemoteRefs() {
for (var id in remoteRefs) {
ajv.addSchema(remoteRefs[id], id);
Expand Down

0 comments on commit 6b9bc9e

Please sign in to comment.