test: refactor schema tests, use nyc

master
Evgeny Poberezkin 2016-10-04 21:16:18 +01:00
parent 72a3bd0c2a
commit da24fd33e1
12 changed files with 138 additions and 127 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

View File

@ -19,6 +19,7 @@ module.exports = function(config) {
'node_modules/chai/chai.js',
'dist/regenerator.min.js',
'dist/nodent.min.js',
'node_modules/bluebird/js/browser/bluebird.core.min.js',
'.browser/*.spec.js'
],

View File

@ -113,6 +113,7 @@ module.exports = function(config) {
'dist/ajv.min.js',
'node_modules/chai/chai.js',
'dist/nodent.min.js',
'node_modules/bluebird/js/browser/bluebird.core.min.js',
'.browser/*.spec.js'
],

View File

@ -17,7 +17,7 @@
"test-spec": "mocha spec/*.spec.js -R spec",
"test-fast": "AJV_FAST_TEST=true npm run test-spec",
"test-debug": "mocha spec/*.spec.js --debug-brk -R spec",
"test-cov": "istanbul cover -x '**/spec/**' node_modules/mocha/bin/_mocha -- spec/*.spec.js -R spec",
"test-cov": "nyc npm run test-spec",
"test-ts": "tsc --target ES5 --noImplicitAny lib/ajv.d.ts",
"bundle": "./scripts/bundle . Ajv pure_getters",
"bundle-regenerator": "./scripts/bundle regenerator",
@ -30,6 +30,16 @@
"prepublish": "npm run build && npm run bundle-all",
"watch": "watch 'npm run build' ./lib/dot"
},
"nyc": {
"exclude": [
"**/spec/**",
"node_modules"
],
"reporter": [
"lcov",
"text-summary"
]
},
"repository": {
"type": "git",
"url": "https://github.com/epoberezkin/ajv.git"
@ -66,7 +76,6 @@
"gh-pages-generator": "^0.2.0",
"glob": "^7.0.0",
"if-node-version": "^1.0.0",
"istanbul": "^0.4.2",
"js-beautify": "^1.5.6",
"jshint": "^2.8.0",
"json-schema-test": "^1.1.1",
@ -77,6 +86,7 @@
"karma-sauce-launcher": "^0.3.0",
"mocha": "^3.0.0",
"nodent": "^2.5.3",
"nyc": "^8.3.0",
"phantomjs-prebuilt": "^2.1.4",
"pre-commit": "^1.1.1",
"regenerator": "0.8.42",

19
spec/after_test.js Normal file
View File

@ -0,0 +1,19 @@
'use strict';
var should = require('./chai').should();
exports.error = function (res) {
console.log('ajv options:', res.validator._opts);
};
exports.each = function (res) {
// console.log(res.errors);
res.valid .should.be.a('boolean');
if (res.valid === true ) {
should.equal(res.errors, null);
} else {
res.errors .should.be.an('array');
for (var i=0; i<res.errors.length; i++)
res.errors[i] .should.be.an('object');
}
};

View File

@ -3,7 +3,9 @@
var jsonSchemaTest = require('json-schema-test')
, Promise = require('./promise')
, getAjvInstances = require('./ajv_async_instances')
, Ajv = require('./ajv');
, Ajv = require('./ajv')
, suite = require('./brower_test_suite')
, after = require('./after_test');
var instances = getAjvInstances({ v5: true });
@ -14,42 +16,23 @@ instances.forEach(addAsyncFormatsAndKeywords);
jsonSchemaTest(instances, {
description: 'asynchronous schemas tests of ' + instances.length + ' ajv instances with different options',
suites: testSuites(),
suites: {
'async schemas':
typeof window == 'object'
? suite(require('./async/{**/,}*.json', {mode: 'list'}))
: './async/{**/,}*.json'
},
async: true,
assert: require('./chai').assert,
Promise: Promise,
afterError: function (res) {
console.log('ajv options:', res.validator._opts);
},
// afterEach: function (res) {
// console.log(res.errors);
// },
afterError: after.error,
// afterEach: after.each,
cwd: __dirname,
hideFolder: 'async/',
timeout: 90000
});
function testSuites() {
var suites;
if (typeof window == 'object') {
suites = {
'async schemas': require('./async/{**/,}*.json', {mode: 'list'})
};
for (var suiteName in suites) {
suites[suiteName].forEach(function (suite) {
suite.test = suite.module;
});
}
} else {
suites = {
'async schemas': './async/{**/,}*.json'
};
}
return suites;
}
function addAsyncFormatsAndKeywords (ajv) {
ajv.addFormat('english_word', {
async: true,

View File

@ -31,7 +31,7 @@ describe('async schemas, formats and keywords', function() {
maxLength: 3
};
return repeat(function() { return Promise.map(instances, test); });
return repeat(function() { return Promise.all(instances.map(test)); });
function test(_ajv) {
var validate = _ajv.compile(schema);
@ -211,7 +211,7 @@ describe('async schemas, formats and keywords', function() {
}
};
return repeat(function() { return Promise.map(instances, function (_ajv) {
return repeat(function() { return Promise.all(instances.map(function (_ajv) {
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
@ -221,7 +221,7 @@ describe('async schemas, formats and keywords', function() {
shouldBeInvalid( _co(validate({ word: 1 })) ),
shouldThrow( _co(validate({ word: 'today' })), 'unknown word' )
]);
}); });
})); });
});
it('should validate recursive async schema', function() {
@ -330,7 +330,7 @@ describe('async schemas, formats and keywords', function() {
});
function recursiveTest(schema, refSchema) {
return repeat(function() { return Promise.map(instances, function (_ajv) {
return repeat(function() { return Promise.all(instances.map(function (_ajv) {
if (refSchema) try { _ajv.addSchema(refSchema); } catch(e) {}
var validate = _ajv.compile(schema);
var _co = useCo(_ajv);
@ -349,7 +349,7 @@ describe('async schemas, formats and keywords', function() {
shouldBeInvalid( _co(validate({ foo: { foo: { foo: 1 }}})) ),
shouldThrow( _co(validate({ foo: { foo: { foo: 'today' }}})), 'unknown word' )
]);
}); });
})); });
}
});

View File

@ -0,0 +1,8 @@
'use strict';
module.exports = function (suite) {
suite.forEach(function (file) {
file.test = file.module;
});
return suite;
};

View File

@ -3,36 +3,28 @@
var jsonSchemaTest = require('json-schema-test')
, getAjvInstances = require('./ajv_instances')
, options = require('./ajv_options')
, should = require('./chai').should();
, suite = require('./brower_test_suite')
, after = require('./after_test');
var instances = getAjvInstances(options);
var remoteRefs = {
// for JSON-Schema-Test-Suite
'http://localhost:1234/integer.json': require('./JSON-Schema-Test-Suite/remotes/integer.json'),
'http://localhost:1234/subSchemas.json': require('./JSON-Schema-Test-Suite/remotes/subSchemas.json'),
'http://localhost:1234/folder/folderInteger.json': require('./JSON-Schema-Test-Suite/remotes/folder/folderInteger.json'),
// for tests
'http://localhost:1234/name.json': require('./remotes/name.json')
};
var remoteRefsWithIds = [
require('./remotes/bar.json'),
require('./remotes/foo.json'),
require('./remotes/buu.json'),
require('./remotes/tree.json'),
require('./remotes/node.json'),
require('./remotes/second.json'),
require('./remotes/first.json'),
require('./remotes/scope_change.json'),
];
instances.forEach(addRemoteRefs);
jsonSchemaTest(instances, {
description: 'Schema tests of ' + instances.length + ' ajv instances with different options',
suites: testSuites(),
description: 'JSON-Schema Test Suite: ' + instances.length + ' ajv instances with different options',
suites: {
'JSON-Schema tests draft4':
typeof window == 'object'
? suite(require('./JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json', {mode: 'list'}))
: './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json'
},
only: [
// 'type', 'not', 'allOf', 'anyOf', 'oneOf', 'enum',
// 'maximum', 'minimum', 'multipleOf', 'maxLength', 'minLength', 'pattern',
@ -42,55 +34,17 @@ jsonSchemaTest(instances, {
// 'items', 'additionalItems', 'uniqueItems',
// 'optional/format', 'optional/bignum',
// 'ref', 'refRemote', 'definitions',
// 'schemas/complex', 'schemas/basic', 'schemas/advanced',
],
skip: [
'optional/zeroTerminatedFloats'
],
skip: [ 'optional/zeroTerminatedFloats' ],
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator._opts);
},
afterEach: function (res) {
// console.log(res.errors);
res.valid .should.be.a('boolean');
if (res.valid === true ) {
should.equal(res.errors, null);
} else {
res.errors .should.be.an('array');
for (var i=0; i<res.errors.length; i++)
res.errors[i] .should.be.an('object');
}
},
afterError: after.error,
afterEach: after.each,
cwd: __dirname,
hideFolder: 'draft4/',
timeout: 120000
});
function testSuites() {
var suites;
if (typeof window == 'object') {
suites = {
'JSON-Schema tests draft4': require('./JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json', {mode: 'list'}),
'Advanced schema tests': require('./tests/{**/,}*.json', {mode: 'list'})
};
for (var suiteName in suites) {
suites[suiteName].forEach(function (suite) {
suite.test = suite.module;
});
}
} else {
suites = {
'JSON-Schema tests draft4': './JSON-Schema-Test-Suite/tests/draft4/{**/,}*.json',
'Advanced schema tests': './tests/{**/,}*.json'
};
}
return suites;
}
function addRemoteRefs(ajv) {
for (var id in remoteRefs) ajv.addSchema(remoteRefs[id], id);
ajv.addSchema(remoteRefsWithIds);
}

View File

@ -1,12 +1,11 @@
'use strict';
var Promise = require('bluebird');
Promise.config({ warnings: false });
var g = typeof global == 'object' ? global :
typeof window == 'object' ? window : this;
g.Promise = g.Promise || Promise;
if (!g.Promise) {
g.Promise = require('' + 'bluebird');
g.Promise.config({ warnings: false });
}
module.exports = Promise;
module.exports = g.Promise;

53
spec/schema-tests.spec.js Normal file
View File

@ -0,0 +1,53 @@
'use strict';
var jsonSchemaTest = require('json-schema-test')
, getAjvInstances = require('./ajv_instances')
, options = require('./ajv_options')
, suite = require('./brower_test_suite')
, after = require('./after_test');
var instances = getAjvInstances(options);
var remoteRefs = {
'http://localhost:1234/integer.json': require('./JSON-Schema-Test-Suite/remotes/integer.json'),
'http://localhost:1234/folder/folderInteger.json': require('./JSON-Schema-Test-Suite/remotes/folder/folderInteger.json'),
'http://localhost:1234/name.json': require('./remotes/name.json')
};
var remoteRefsWithIds = [
require('./remotes/bar.json'),
require('./remotes/foo.json'),
require('./remotes/buu.json'),
require('./remotes/tree.json'),
require('./remotes/node.json'),
require('./remotes/second.json'),
require('./remotes/first.json'),
require('./remotes/scope_change.json'),
];
instances.forEach(addRemoteRefs);
jsonSchemaTest(instances, {
description: 'Schema tests of ' + instances.length + ' ajv instances with different options',
suites: {
'Advanced schema tests':
typeof window == 'object'
? suite(require('./tests/{**/,}*.json', {mode: 'list'}))
: './tests/{**/,}*.json'
},
only: [
// 'schemas/complex', 'schemas/basic', 'schemas/advanced',
],
assert: require('./chai').assert,
afterError: after.error,
afterEach: after.each,
cwd: __dirname,
timeout: 120000
});
function addRemoteRefs(ajv) {
for (var id in remoteRefs) ajv.addSchema(remoteRefs[id], id);
ajv.addSchema(remoteRefsWithIds);
}

View File

@ -2,42 +2,24 @@
var jsonSchemaTest = require('json-schema-test')
, getAjvInstances = require('./ajv_instances')
, options = require('./ajv_options');
, options = require('./ajv_options')
, suite = require('./brower_test_suite')
, after = require('./after_test');
var instances = getAjvInstances(options, { v5: true });
jsonSchemaTest(instances, {
description: 'v5 schemas tests of ' + instances.length + ' ajv instances with different options',
suites: testSuites(),
assert: require('./chai').assert,
afterError: function (res) {
console.log('ajv options:', res.validator._opts);
suites: {
'v5 proposals': typeof window == 'object'
? suite(require('./v5/{**/,}*.json', {mode: 'list'}))
: './v5/{**/,}*.json'
},
// afterEach: function (res) {
// console.log(res.errors);
// },
assert: require('./chai').assert,
afterError: after.error,
afterEach: after.each,
cwd: __dirname,
hideFolder: 'v5/',
timeout: 90000
});
function testSuites() {
var suites;
if (typeof window == 'object') {
suites = {
'v5 proposals': require('./v5/{**/,}*.json', {mode: 'list'})
};
for (var suiteName in suites) {
suites[suiteName].forEach(function (suite) {
suite.test = suite.module;
});
}
} else {
suites = {
'v5 proposals': './v5/{**/,}*.json'
};
}
return suites;
}