Best JavaScript code snippet using ava
test_validator.js
Source:test_validator.js
...19 setUp: function(done) {20 done();21 },22 'nonexistent/null/undefined': function(test) {23 test.throws(function() { return new Validator(); });24 test.throws(function() { return new Validator(null); });25 test.throws(function() { return new Validator(undefined); });26 test.done();27 },28 'unrecognized primitive type name': function(test) {29 test.throws(function() { return new Validator('badtype'); });30 test.done();31 },32 'invalid schema javascript type': function(test) {33 test.throws(function() { return new Validator(123); });34 test.throws(function() { return new Validator(function() { }); });35 test.done();36 },37 // Primitive types38 'null': function(test) {39 test.ok(Validator.validate('null', null));40 test.ok(Validator.validate('null', undefined));41 test.throws(function() { Validator.validate('null', 1); });42 test.throws(function() { Validator.validate('null', 'a'); });43 test.done();44 },45 'boolean': function(test) {46 test.ok(Validator.validate('boolean', true));47 test.ok(Validator.validate('boolean', false));48 test.throws(function() { Validator.validate('boolean', null); });49 test.throws(function() { Validator.validate('boolean', 1); });50 test.throws(function() { Validator.validate('boolean', 'a'); });51 test.done();52 },53 'int': function(test) {54 test.ok(Validator.validate('int', 1));55 test.ok(Validator.validate('long', Math.pow(2, 31) - 1));56 test.throws(function() { Validator.validate('int', 1.5); });57 test.throws(function() { Validator.validate('int', Math.pow(2, 40)); });58 test.throws(function() { Validator.validate('int', null); });59 test.throws(function() { Validator.validate('int', 'a'); });60 test.done();61 },62 'long': function(test) {63 test.ok(Validator.validate('long', 1));64 test.ok(Validator.validate('long', Math.pow(2, 63) - 1));65 test.throws(function() { Validator.validate('long', 1.5); });66 test.throws(function() { Validator.validate('long', Math.pow(2, 70)); });67 test.throws(function() { Validator.validate('long', null); });68 test.throws(function() { Validator.validate('long', 'a'); });69 test.done();70 },71 'float': function(test) {72 test.ok(Validator.validate('float', 1));73 test.ok(Validator.validate('float', 1.5));74 test.throws(function() { Validator.validate('float', 'a'); });75 test.throws(function() { Validator.validate('float', null); });76 test.done();77 },78 'double': function(test) {79 test.ok(Validator.validate('double', 1));80 test.ok(Validator.validate('double', 1.5));81 test.throws(function() { Validator.validate('double', 'a'); });82 test.throws(function() { Validator.validate('double', null); });83 test.done();84 },85 'bytes': function(test) {86 // not implemented yet87 test.throws(function() { Validator.validate('bytes', 1); });88 test.done();89 },90 'string': function(test) {91 test.ok(Validator.validate('string', 'a'));92 test.throws(function() { Validator.validate('string', 1); });93 test.throws(function() { Validator.validate('string', null); });94 test.done();95 },96 // Records97 'empty-record': function(test) {98 var schema = {type: 'record', name: 'EmptyRecord', fields: []};99 test.ok(Validator.validate(schema, {}));100 test.throws(function() { Validator.validate(schema, 1); });101 test.throws(function() { Validator.validate(schema, null); });102 test.throws(function() { Validator.validate(schema, 'a'); });103 test.done();104 },105 'record-with-string': function(test) {106 var schema = {type: 'record', name: 'EmptyRecord', fields: [{name: 'stringField', type: 'string'}]};107 test.ok(Validator.validate(schema, {stringField: 'a'}));108 test.throws(function() { Validator.validate(schema, {}); });109 test.throws(function() { Validator.validate(schema, {stringField: 1}); });110 test.throws(function() { Validator.validate(schema, {stringField: []}); });111 test.throws(function() { Validator.validate(schema, {stringField: {}}); });112 test.throws(function() { Validator.validate(schema, {stringField: null}); });113 test.throws(function() { Validator.validate(schema, {stringField: 'a', unexpectedField: 'a'}); });114 test.done();115 },116 'record-with-string-and-number': function(test) {117 var schema = {type: 'record', name: 'EmptyRecord', fields: [{name: 'stringField', type: 'string'}, {name: 'intField', type: 'int'}]};118 test.ok(Validator.validate(schema, {stringField: 'a', intField: 1}));119 test.throws(function() { Validator.validate(schema, {}); });120 test.throws(function() { Validator.validate(schema, {stringField: 'a'}); });121 test.throws(function() { Validator.validate(schema, {intField: 1}); });122 test.throws(function() { Validator.validate(schema, {stringField: 'a', intField: 1, unexpectedField: 'a'}); });123 test.done();124 },125 'nested-record-with-namespace-relative': function(test) {126 var schema = {type: 'record', namespace: 'x.y.z', name: 'RecordA', fields: [{name: 'recordBField1', type: ['null', {type: 'record', name: 'RecordB', fields: []}]}, {name: 'recordBField2', type: 'RecordB'}]};127 test.ok(Validator.validate(schema, {recordBField1: null, recordBField2: {}}));128 test.ok(Validator.validate(schema, {recordBField1: {'x.y.z.RecordB': {}}, recordBField2: {}}));129 test.throws(function() { Validator.validate(schema, {}); });130 test.throws(function() { Validator.validate(schema, {recordBField1: null}); });131 test.throws(function() { Validator.validate(schema, {recordBField2: {}}); });132 test.throws(function() { Validator.validate(schema, {recordBField1: {'RecordB': {}}, recordBField2: {}}); });133 test.done();134 },135 'nested-record-with-namespace-absolute': function(test) {136 var schema = {type: 'record', namespace: 'x.y.z', name: 'RecordA', fields: [{name: 'recordBField1', type: ['null', {type: 'record', name: 'RecordB', fields: []}]}, {name: 'recordBField2', type: 'x.y.z.RecordB'}]};137 test.ok(Validator.validate(schema, {recordBField1: null, recordBField2: {}}));138 test.ok(Validator.validate(schema, {recordBField1: {'x.y.z.RecordB': {}}, recordBField2: {}}));139 test.throws(function() { Validator.validate(schema, {}); });140 test.throws(function() { Validator.validate(schema, {recordBField1: null}); });141 test.throws(function() { Validator.validate(schema, {recordBField2: {}}); });142 test.throws(function() { Validator.validate(schema, {recordBField1: {'RecordB': {}}, recordBField2: {}}); });143 test.done();144 },145 // Enums146 'enum': function(test) {147 var schema = {type: 'enum', name: 'Colors', symbols: ['Red', 'Blue']};148 test.ok(Validator.validate(schema, 'Red'));149 test.ok(Validator.validate(schema, 'Blue'));150 test.throws(function() { Validator.validate(schema, null); });151 test.throws(function() { Validator.validate(schema, undefined); });152 test.throws(function() { Validator.validate(schema, 'NotAColor'); });153 test.throws(function() { Validator.validate(schema, ''); });154 test.throws(function() { Validator.validate(schema, {}); });155 test.throws(function() { Validator.validate(schema, []); });156 test.throws(function() { Validator.validate(schema, 1); });157 test.done();158 },159 // Unions160 'union': function(test) {161 var schema = ['string', 'int'];162 test.ok(Validator.validate(schema, {string: 'a'}));163 test.ok(Validator.validate(schema, {int: 1}));164 test.throws(function() { Validator.validate(schema, null); });165 test.throws(function() { Validator.validate(schema, undefined); });166 test.throws(function() { Validator.validate(schema, 'a'); });167 test.throws(function() { Validator.validate(schema, 1); });168 test.throws(function() { Validator.validate(schema, {string: 'a', int: 1}); });169 test.throws(function() { Validator.validate(schema, []); });170 test.done();171 },172 'union with null': function(test) {173 var schema = ['string', 'null'];174 test.ok(Validator.validate(schema, {string: 'a'}));175 test.ok(Validator.validate(schema, null));176 test.throws(function() { Validator.validate(schema, undefined); });177 test.done();178 },179 'nested union': function(test) {180 var schema = ['string', {type: 'int'}];181 test.ok(Validator.validate(schema, {string: 'a'}));182 test.ok(Validator.validate(schema, {int: 1}));183 test.throws(function() { Validator.validate(schema, null); });184 test.throws(function() { Validator.validate(schema, undefined); });185 test.throws(function() { Validator.validate(schema, 'a'); });186 test.throws(function() { Validator.validate(schema, 1); });187 test.throws(function() { Validator.validate(schema, {string: 'a', int: 1}); });188 test.throws(function() { Validator.validate(schema, []); });189 test.done();190 },191 // Arrays192 'array': function(test) {193 var schema = {type: "array", items: "string"};194 test.ok(Validator.validate(schema, []));195 test.ok(Validator.validate(schema, ["a"]));196 test.ok(Validator.validate(schema, ["a", "b", "a"]));197 test.throws(function() { Validator.validate(schema, null); });198 test.throws(function() { Validator.validate(schema, undefined); });199 test.throws(function() { Validator.validate(schema, 'a'); });200 test.throws(function() { Validator.validate(schema, 1); });201 test.throws(function() { Validator.validate(schema, {}); });202 test.throws(function() { Validator.validate(schema, {"1": "a"}); });203 test.throws(function() { Validator.validate(schema, {1: "a"}); });204 test.throws(function() { Validator.validate(schema, {1: "a", "b": undefined}); });205 test.throws(function() { var a = {}; a[0] = "a"; Validator.validate(schema, a); });206 test.throws(function() { Validator.validate(schema, [1]); });207 test.throws(function() { Validator.validate(schema, [1, "a"]); });208 test.throws(function() { Validator.validate(schema, ["a", 1]); });209 test.throws(function() { Validator.validate(schema, [null, 1]); });210 test.done();211 },212 // Maps213 'map': function(test) {214 var schema = {type: "map", values: "string"};215 test.ok(Validator.validate(schema, {}));216 test.ok(Validator.validate(schema, {"a": "b"}));217 test.ok(Validator.validate(schema, {"a": "b", "c": "d"}));218 test.throws(function() { Validator.validate(schema, null); });219 test.throws(function() { Validator.validate(schema, undefined); });220 test.throws(function() { Validator.validate(schema, 'a'); });221 test.throws(function() { Validator.validate(schema, 1); });222 test.throws(function() { Validator.validate(schema, [1]); });223 test.throws(function() { Validator.validate(schema, {"a": 1}); });224 test.throws(function() { Validator.validate(schema, {"a": "b", "c": 1}); });225 test.done();226 },227 // Protocols228 'protocol': function(test) {229 var protocol = {protocol: "Protocol1", namespace: "x.y.z", types: [230 {type: "record", name: "RecordA", fields: []},231 {type: "record", name: "RecordB", fields: [{name: "recordAField", type: "RecordA"}]}232 ]};233 test.ok(ProtocolValidator.validate(protocol, 'RecordA', {}));234 test.ok(ProtocolValidator.validate(protocol, 'x.y.z.RecordA', {}));235 test.ok(ProtocolValidator.validate(protocol, 'RecordB', {recordAField: {}}));236 test.ok(ProtocolValidator.validate(protocol, 'x.y.z.RecordB', {recordAField: {}}));237 test.throws(function() { ProtocolValidator.validate(protocol, 'RecordDoesNotExist', {}); });238 test.throws(function() { ProtocolValidator.validate(protocol, 'RecordDoesNotExist', null); });239 test.throws(function() { ProtocolValidator.validate(protocol, 'RecordB', {}); });240 test.throws(function() { ProtocolValidator.validate(protocol, null, {}); });241 test.throws(function() { ProtocolValidator.validate(protocol, '', {}); });242 test.throws(function() { ProtocolValidator.validate(protocol, {}, {}); });243 test.done(); 244 },245 // Samples246 'link': function(test) {247 var schema = {248 "type" : "record",249 "name" : "Bundle",250 "namespace" : "aa.bb.cc",251 "fields" : [ {252 "name" : "id",253 "type" : "string"254 }, {255 "name" : "type",256 "type" : "string"257 }, {258 "name" : "data_",259 "type" : [ "null", {260 "type" : "record",261 "name" : "LinkData",262 "fields" : [ {263 "name" : "address",264 "type" : "string"265 }, {266 "name" : "title",267 "type" : [ "null", "string" ],268 "default" : null269 }, {270 "name" : "excerpt",271 "type" : [ "null", "string" ],272 "default" : null273 }, {274 "name" : "image",275 "type" : [ "null", {276 "type" : "record",277 "name" : "Image",278 "fields" : [ {279 "name" : "url",280 "type" : "string"281 }, {282 "name" : "width",283 "type" : "int"284 }, {285 "name" : "height",286 "type" : "int"287 } ]288 } ],289 "default" : null290 }, {291 "name" : "meta",292 "type" : {293 "type" : "map",294 "values" : "string"295 },296 "default" : {297 }298 } ]299 } ],300 "default" : null301 }, {302 "name" : "atoms_",303 "type" : {304 "type" : "map",305 "values" : {306 "type" : "map",307 "values" : {308 "type" : "record",309 "name" : "Atom",310 "fields" : [ {311 "name" : "index_",312 "type" : {313 "type" : "record",314 "name" : "AtomIndex",315 "fields" : [ {316 "name" : "type_",317 "type" : "string"318 }, {319 "name" : "id",320 "type" : "string"321 } ]322 }323 }, {324 "name" : "data_",325 "type" : [ "LinkData" ]326 } ]327 }328 }329 },330 "default" : {331 }332 }, {333 "name" : "meta_",334 "type" : {335 "type" : "record",336 "name" : "BundleMetadata",337 "fields" : [ {338 "name" : "date",339 "type" : "long",340 "default" : 0341 }, {342 "name" : "members",343 "type" : {344 "type" : "map",345 "values" : "string"346 },347 "default" : {348 }349 }, {350 "name" : "tags",351 "type" : {352 "type" : "map",353 "values" : "string"354 },355 "default" : {356 }357 }, {358 "name" : "meta",359 "type" : {360 "type" : "map",361 "values" : "string"362 },363 "default" : {364 }365 }, {366 "name" : "votes",367 "type" : {368 "type" : "map",369 "values" : {370 "type" : "record",371 "name" : "VoteData",372 "fields" : [ {373 "name" : "date",374 "type" : "long"375 }, {376 "name" : "userName",377 "type" : [ "null", "string" ],378 "default" : null379 }, {380 "name" : "direction",381 "type" : {382 "type" : "enum",383 "name" : "VoteDirection",384 "symbols" : [ "Up", "Down", "None" ]385 }386 } ]387 }388 },389 "default" : {390 }391 }, {392 "name" : "views",393 "type" : {394 "type" : "map",395 "values" : {396 "type" : "record",397 "name" : "ViewData",398 "fields" : [ {399 "name" : "userName",400 "type" : "string"401 }, {402 "name" : "count",403 "type" : "int"404 } ]405 }406 },407 "default" : {408 }409 }, {410 "name" : "relevance",411 "type" : {412 "type" : "map",413 "values" : "string"414 },415 "default" : {416 }417 }, {418 "name" : "clicks",419 "type" : {420 "type" : "map",421 "values" : "string"422 },423 "default" : {424 }425 } ]426 }427 } ]428 };429 var okObj = {430 "id": "https://github.com/sqs/akka-kryo-serialization/subscription",431 "type": "link",432 "data_": {433 "aa.bb.cc.LinkData": {434 "address": "https://github.com/sqs/akka-kryo-serialization/subscription",435 "title": {436 "string": "Sign in · GitHub"437 },438 "excerpt": {439 "string": "Signup and Pricing Explore GitHub Features Blog Sign in Sign in (Pricing and Signup) Username or Email Password (forgot password) GitHub Links GitHub About Blog Feat"440 },441 "image": {442 "aa.bb.cc.Image": {443 "url": "https://a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x.png?1340659561",444 "width": 280,445 "height": 120446 }447 },448 "meta": {}449 }450 },451 "atoms_": {452 "link": {453 "https://github.com/sqs/akka-kryo-serialization/subscription": {454 "index_": {455 "type_": "link",456 "id": "https://github.com/sqs/akka-kryo-serialization/subscription"457 },458 "data_": {459 "aa.bb.cc.LinkData": {460 "address": "https://github.com/sqs/akka-kryo-serialization/subscription",461 "title": {462 "string": "Sign in · GitHub"463 },464 "excerpt": {465 "string": "Signup and Pricing Explore GitHub Features Blog Sign in Sign in (Pricing and Signup) Username or Email Password (forgot password) GitHub Links GitHub About Blog Feat"466 },467 "image": {468 "aa.bb.cc.Image": {469 "url": "https://a248.e.akamai.net/assets.github.com/images/modules/header/logov7@4x.png?1340659561",470 "width": 280,471 "height": 120472 }473 },474 "meta": {}475 }476 }477 }478 }479 },480 "meta_": {481 "date": 1345537530000,482 "members": {483 "a@a.com": "1"484 },485 "tags": {486 "blue": "1"487 },488 "meta": {},489 "votes": {},490 "views": {491 "a@a.com": {492 "userName": "John Smith",493 "count": 100494 }495 },496 "relevance": {497 "a@a.com": "1",498 "b@b.com": "2"499 },500 "clicks": {}501 }502 };503 test.ok(Validator.validate(schema, okObj));504 var badObj = okObj; // no deep copy since we won't reuse okObj505 badObj.meta_.clicks['a@a.com'] = 123;506 test.throws(function() { Validator.validate(schema, badObj); });507 test.done();508 }...
ReactPropTypesProduction-test.js
Source:ReactPropTypesProduction-test.js
1/**2 * Copyright 2013-present, Facebook, Inc.3 * All rights reserved.4 *5 * This source code is licensed under the BSD-style license found in the6 * LICENSE file in the root directory of this source tree. An additional grant7 * of patent rights can be found in the PATENTS file in the same directory.8 *9 * @emails react-core10 */11'use strict';12describe('ReactPropTypesProduction', function() {13 var PropTypes;14 var React;15 var ReactPropTypeLocations;16 var ReactTestUtils;17 var oldProcess;18 beforeEach(function() {19 __DEV__ = false;20 // Mutating process.env.NODE_ENV would cause our babel plugins to do the21 // wrong thing. If you change this, make sure to test with jest --no-cache.22 oldProcess = process;23 global.process = {24 ...process,25 env: {...process.env, NODE_ENV: 'production'},26 };27 jest.resetModules();28 PropTypes = require('ReactPropTypes');29 React = require('React');30 ReactPropTypeLocations = require('ReactPropTypeLocations');31 ReactTestUtils = require('ReactTestUtils');32 });33 afterEach(function() {34 __DEV__ = true;35 global.process = oldProcess;36 });37 function expectThrowsInProduction(declaration, value) {38 var props = {testProp: value};39 expect(() => {40 declaration(41 props,42 'testProp',43 'testComponent',44 ReactPropTypeLocations.prop45 );46 }).toThrowError(47 'React.PropTypes type checking code is stripped in production.'48 );49 }50 describe('Primitive Types', function() {51 it('should be a no-op', function() {52 expectThrowsInProduction(PropTypes.array, /please/);53 expectThrowsInProduction(PropTypes.array.isRequired, /please/);54 expectThrowsInProduction(PropTypes.array.isRequired, null);55 expectThrowsInProduction(PropTypes.array.isRequired, undefined);56 expectThrowsInProduction(PropTypes.bool, []);57 expectThrowsInProduction(PropTypes.bool.isRequired, []);58 expectThrowsInProduction(PropTypes.bool.isRequired, null);59 expectThrowsInProduction(PropTypes.bool.isRequired, undefined);60 expectThrowsInProduction(PropTypes.func, false);61 expectThrowsInProduction(PropTypes.func.isRequired, false);62 expectThrowsInProduction(PropTypes.func.isRequired, null);63 expectThrowsInProduction(PropTypes.func.isRequired, undefined);64 expectThrowsInProduction(PropTypes.number, function() {});65 expectThrowsInProduction(PropTypes.number.isRequired, function() {});66 expectThrowsInProduction(PropTypes.number.isRequired, null);67 expectThrowsInProduction(PropTypes.number.isRequired, undefined);68 expectThrowsInProduction(PropTypes.string, 0);69 expectThrowsInProduction(PropTypes.string.isRequired, 0);70 expectThrowsInProduction(PropTypes.string.isRequired, null);71 expectThrowsInProduction(PropTypes.string.isRequired, undefined);72 expectThrowsInProduction(PropTypes.symbol, 0);73 expectThrowsInProduction(PropTypes.symbol.isRequired, 0);74 expectThrowsInProduction(PropTypes.symbol.isRequired, null);75 expectThrowsInProduction(PropTypes.symbol.isRequired, undefined);76 expectThrowsInProduction(PropTypes.object, '');77 expectThrowsInProduction(PropTypes.object.isRequired, '');78 expectThrowsInProduction(PropTypes.object.isRequired, null);79 expectThrowsInProduction(PropTypes.object.isRequired, undefined);80 });81 });82 describe('Any Type', function() {83 it('should be a no-op', function() {84 expectThrowsInProduction(PropTypes.any, null);85 expectThrowsInProduction(PropTypes.any.isRequired, null);86 expectThrowsInProduction(PropTypes.any.isRequired, undefined);87 });88 });89 describe('ArrayOf Type', function() {90 it('should be a no-op', function() {91 expectThrowsInProduction(92 PropTypes.arrayOf({ foo: PropTypes.string }),93 { foo: 'bar' }94 );95 expectThrowsInProduction(96 PropTypes.arrayOf(PropTypes.number),97 [1, 2, 'b']98 );99 expectThrowsInProduction(100 PropTypes.arrayOf(PropTypes.number),101 {'0': 'maybe-array', length: 1}102 );103 expectThrowsInProduction(PropTypes.arrayOf(PropTypes.number).isRequired, null);104 expectThrowsInProduction(PropTypes.arrayOf(PropTypes.number).isRequired, undefined);105 });106 });107 describe('Component Type', function() {108 it('should be a no-op', function() {109 expectThrowsInProduction(PropTypes.element, [<div />, <div />]);110 expectThrowsInProduction(PropTypes.element, 123);111 expectThrowsInProduction(PropTypes.element, 'foo');112 expectThrowsInProduction(PropTypes.element, false);113 expectThrowsInProduction(PropTypes.element.isRequired, null);114 expectThrowsInProduction(PropTypes.element.isRequired, undefined);115 });116 });117 describe('Instance Types', function() {118 it('should be a no-op', function() {119 expectThrowsInProduction(PropTypes.instanceOf(Date), {});120 expectThrowsInProduction(PropTypes.instanceOf(Date).isRequired, {});121 });122 });123 describe('React Component Types', function() {124 it('should be a no-op', function() {125 expectThrowsInProduction(PropTypes.node, {});126 expectThrowsInProduction(PropTypes.node.isRequired, null);127 expectThrowsInProduction(PropTypes.node.isRequired, undefined);128 });129 });130 describe('ObjectOf Type', function() {131 it('should be a no-op', function() {132 expectThrowsInProduction(133 PropTypes.objectOf({ foo: PropTypes.string }),134 { foo: 'bar' }135 );136 expectThrowsInProduction(137 PropTypes.objectOf(PropTypes.number),138 {a: 1, b: 2, c: 'b'}139 );140 expectThrowsInProduction(PropTypes.objectOf(PropTypes.number), [1, 2]);141 expectThrowsInProduction(PropTypes.objectOf(PropTypes.number), null);142 expectThrowsInProduction(PropTypes.objectOf(PropTypes.number), undefined);143 });144 });145 describe('OneOf Types', function() {146 it('should be a no-op', function() {147 expectThrowsInProduction(PropTypes.oneOf('red', 'blue'), 'red');148 expectThrowsInProduction(PropTypes.oneOf(['red', 'blue']), true);149 expectThrowsInProduction(PropTypes.oneOf(['red', 'blue']), null);150 expectThrowsInProduction(PropTypes.oneOf(['red', 'blue']), undefined);151 });152 });153 describe('Union Types', function() {154 it('should be a no-op', function() {155 expectThrowsInProduction(156 PropTypes.oneOfType(PropTypes.string, PropTypes.number),157 'red'158 );159 expectThrowsInProduction(160 PropTypes.oneOfType([PropTypes.string, PropTypes.number]),161 []162 );163 expectThrowsInProduction(164 PropTypes.oneOfType([PropTypes.string, PropTypes.number]),165 null166 );167 expectThrowsInProduction(168 PropTypes.oneOfType([PropTypes.string, PropTypes.number]),169 undefined170 );171 });172 });173 describe('Shape Types', function() {174 it('should be a no-op', function() {175 expectThrowsInProduction(PropTypes.shape({}), 'some string');176 expectThrowsInProduction(177 PropTypes.shape({key: PropTypes.number}).isRequired,178 null179 );180 expectThrowsInProduction(181 PropTypes.shape({key: PropTypes.number}).isRequired,182 undefined183 );184 });185 });186 describe('Custom validator', function() {187 beforeEach(function() {188 jest.resetModules();189 });190 it('should not have been called', function() {191 var spy = jest.fn();192 var Component = React.createClass({193 propTypes: {num: spy},194 render: function() {195 return <div />;196 },197 });198 var instance = <Component num={5} />;199 ReactTestUtils.renderIntoDocument(instance);200 expect(spy).not.toBeCalled();201 });202 });...
exceptions.js
Source:exceptions.js
1"use strict";2var _helpers = require("./util/helpers");3// Unclosed elements4(0, _helpers.throws)('unclosed string', 'a[href="wow]');5(0, _helpers.throws)('unclosed comment', '/* oops');6(0, _helpers.throws)('unclosed pseudo element', 'button::');7(0, _helpers.throws)('unclosed pseudo class', 'a:');8(0, _helpers.throws)('unclosed attribute selector', '[name="james"][href');9(0, _helpers.throws)('no opening parenthesis', ')');10(0, _helpers.throws)('no opening parenthesis (2)', ':global.foo)');11(0, _helpers.throws)('no opening parenthesis (3)', 'h1:not(h2:not(h3)))');12(0, _helpers.throws)('no opening square bracket', ']');13(0, _helpers.throws)('no opening square bracket (2)', ':global.foo]');14(0, _helpers.throws)('no opening square bracket (3)', '[global]]');15(0, _helpers.throws)('bad pseudo element', 'button::"after"');16(0, _helpers.throws)('missing closing parenthesis in pseudo', ':not([attr="test"]:not([attr="test"])');17(0, _helpers.throws)('bad syntax', '-moz-osx-font-smoothing: grayscale');18(0, _helpers.throws)('bad syntax (2)', '! .body');19(0, _helpers.throws)('missing backslash for semicolon', '.;');20(0, _helpers.throws)('missing backslash for semicolon (2)', '.\;');21(0, _helpers.throws)('unexpected / foo', '-Option\/root', "Unexpected '/'. Escaping special characters with \\ may help.");...
Using AI Code Generation
1import test from 'ava';2import fn from './';3test(t => {4 const err = t.throws(() => fn(100), TypeError);5 t.is(err.message, 'Expected a string, got number');6 t.is(err.name, 'TypeError');7});8export default function (str) {9 if (typeof str !== 'string') {10 throw new TypeError('Expected a string');11 }12 return str;13}14{15 "scripts": {16 },17 "devDependencies": {18 }19}
Using AI Code Generation
1test('my passing test', t => {2 t.pass();3});4test('my failing test', t => {5 t.fail();6});7test('my throwing test', t => {8 t.throws(() => {9 throw new Error('my error');10 });11});12test('my not throwing test', t => {13 t.notThrows(() => {14 throw new Error('my error');15 });16});17test('my passing test', t => {18 t.pass();19});20test('my failing test', t => {21 t.fail();22});23test('my throwing test', t => {24 t.throws(() => {25 throw new Error('my error');26 });27});28test('my not throwing test', t => {29 t.notThrows(() => {30 throw new Error('my error');31 });32});33test('my passing test', t => {34 t.pass();35});36test('my failing test', t => {37 t.fail();38});39test('my throwing test', t => {40 t.throws(() => {41 throw new Error('my error');42 });43});44test('my not throwing test', t => {45 t.notThrows(() => {46 throw new Error('my error');47 });48});49test('my passing test', t => {50 t.pass();51});52test('my failing test', t => {53 t.fail();54});55test('my throwing test', t => {56 t.throws(() => {57 throw new Error('my error');58 });59});60test('my not throwing test', t => {61 t.notThrows(() => {62 throw new Error('my error');63 });64});65test('my passing test', t => {66 t.pass();67});68test('my failing test', t => {69 t.fail();70});71test('my throwing test', t => {72 t.throws(() => {73 throw new Error('my error');74 });75});76test('my not throwing test', t => {77 t.notThrows(() => {78 throw new Error('my error');79 });80});
Using AI Code Generation
1import test from 'ava';2import fn from './';3test('throws', t => {4 t.throws(fn, 'unicorn');5 t.throws(fn, /unicorn/);6 t.throws(fn, err => err.message === 'unicorn');7 t.throws(fn, TypeError);8 t.throws(fn, TypeError, 'message');9});10import test from 'ava';11import fn from './';12test('notThrows', t => {13 t.notThrows(fn);14});15import test from 'ava';16import fn from './';17test('ifError', t => {18 t.ifError(fn);19});20import test from 'ava';21import fn from './';22test('snapshot', t => {23 t.snapshot(fn);24});25import test from 'ava';26import fn from './';27test('regex', t => {28 t.regex(fn, /unicorn/);29});30import test from 'ava';31import fn from './';32test('notRegex', t => {33 t.notRegex(fn, /unicorn/);34});35import test from 'ava';36import fn from './';37test('throwsAsync', async t => {38 await t.throwsAsync(fn, 'unicorn');39 await t.throwsAsync(fn, /unicorn/);40 await t.throwsAsync(fn, err => err.message === 'unicorn');41 await t.throwsAsync(fn, TypeError);42 await t.throwsAsync(fn, TypeError, 'message');43});44import test from 'ava';45import fn from './';46test('notThrowsAsync', async t => {47 await t.notThrowsAsync(fn);48});49import test from 'ava';50import fn from './';51test('regexTest', t => {52 t.regexTest(/unicorn/, fn);53});
Using AI Code Generation
1const test = require('ava');2test('throws', t => {3 t.throws(() => {4 throw new Error('foo');5 });6});7const test = require('ava');8test('throws', t => {9 t.throws(() => {10 throw new TypeError('foo');11 }, TypeError);12});13const test = require('ava');14test('throws', t => {15 t.throws(() => {16 throw new TypeError('foo');17 }, TypeError, 'bar');18});19const test = require('ava');20test('throws', t => {21 t.throws(() => {22 throw new TypeError('foo');23 }, TypeError, 'bar');24});25const test = require('ava');26test('throws', t => {27 t.throws(() => {28 throw new TypeError('foo');29 }, TypeError, /bar/);30});31const test = require('ava');32test('throws', t => {33 t.throws(() => {34 throw new TypeError('foo');35 }, {instanceOf: TypeError});36});37const test = require('ava');38test('throws', t => {39 t.throws(() => {40 throw new TypeError('foo');41 }, {instanceOf: TypeError, message: 'bar'});42});43const test = require('ava');44test('throws', t => {45 t.throws(() => {46 throw new TypeError('foo');47 }, {instanceOf: TypeError, message: /bar/});48});49const test = require('ava');50test('throws', t => {51 t.throws(() => {52 throw new TypeError('foo');53 }, TypeError, {message: 'bar'});54});55const test = require('ava');56test('throws', t => {57 t.throws(() => {58 throw new TypeError('foo');59 }, TypeError, {message: /bar/});60});61const test = require('ava');62test('throws', t => {
Using AI Code Generation
1const test = require('ava');2const myModule = require('myModule');3test('throws', t => {4 const err = t.throws(() => {5 myModule();6 }, Error);7 t.is(err.message, 'hello');8});9function myModule() {10 throw new Error('hello');11}12module.exports = myModule;13 Error {14 }15 test (test.js:5:9)16 process._tickCallback (internal/process/next_tick.js:68:7)17const test = require('ava');18const myModule = require('myModule');19test('notThrows', t => {20 t.notThrows(() => {21 myModule();22 });23});24function myModule() {25 console.log('hello');26}27module.exports = myModule;
Using AI Code Generation
1import test from 'ava';2test('throws', t => {3 t.throws(() => {4 throw new TypeError('I am an error');5 });6});7import test from 'ava';8test('throwsAsync', async t => {9 await t.throwsAsync(Promise.reject(new Error('I am an error')));10});11import test from 'ava';12test('notThrows', t => {13 t.notThrows(() => {14 });15});16import test from 'ava';17test('notThrowsAsync', async t => {18 await t.notThrowsAsync(Promise.resolve());19});20import test from 'ava';21test('regex', t => {22 t.regex('I love unicorns', /unicorn/);23});24import test from 'ava';25test('notRegex', t => {26 t.notRegex('I love unicorns', /rainbow/);27});28import test from 'ava';29test('snapshot', t => {30 t.snapshot({foo: 'bar'});31});32import test from 'ava';33test('log', t => {34 t.log('Hello, world!');35});36import test from 'ava';37test('fail', t => {38 t.fail();39});40import test from 'ava';41test('pass', t => {42 t.pass();43});44import test from 'ava';45test('truthy', t => {46 t.truthy('unicorn');47});48import test from 'ava';49test('falsy', t => {50 t.falsy('');51});
Using AI Code Generation
1const test = require('ava')2const { add } = require('./index')3test('add method', t => {4 t.throws(() => add(1, 2), Error)5})6function add(a, b) {7 if (typeof a !== 'number' || typeof b !== 'number') {8 throw new Error('a and b must be numbers')9 }10}11module.exports = {12}13const test = require('ava')14const { add } = require('./index')15test.cb('add method', t => {16 add(1, 2, (err, result) => {17 if (err) {18 t.end(err)19 }20 t.is(result, 3)21 t.end()22 })23})24function add(a, b, callback) {25 if (typeof a !== 'number' || typeof b !== 'number') {26 return callback(new Error('a and b must be numbers'))27 }28 callback(null, a + b)29}30module.exports = {31}
Using AI Code Generation
1const test = require('ava');2const add = require('./add.js');3test('add', t => {4 t.throws(() => {5 add(1)6 }, 'adds two numbers');7});8function add(a, b) {9 return a + b;10}11module.exports = add;12export function add(a: number, b: number): number {13 return a + b;14}15import test from 'ava';16import {add} from './add';17test('add', t => {18 t.throws(() => {19 add(1);20 }, 'adds two numbers');21});22export function add(a: number, b: number): number {23 return a + b;24}25import test from 'ava';26import {add} from './add';27test('add', t => {28 t.throws(() => {29 add(1);30 }, 'adds two numbers');31});
Using AI Code Generation
1import test from 'ava';2import fn from '.';3test(t => {4 t.throws(() => fn(1), 'foo');5});6### `t.notThrows(function, [error], [message])`7import test from 'ava';8import fn from '.';9test(t => {10 t.notThrows(() => fn(1));11});12### `t.regex(contents, regex, [message])`13import test from 'ava';14import fn from '.';15test(t => {16 t.regex(fn('foo'), /foo/);17});18### `t.notRegex(contents, regex, [message])`19import test from 'ava';20import fn from '.';21test(t => {22 t.notRegex(fn('foo'), /bar/);23});24### `t.snapshot(contents, [message])`25import test from 'ava';26import fn from '.';27test(t => {28 t.snapshot(fn('foo'));29});
Using AI Code Generation
1const { test } = require('ava');2const { sum } = require('./sum');3test('sum', t => {4 t.throws(() => {5 sum(1, 2);6 });7});8exports.sum = (a, b) => {9 if (a && b) {10 return a + b;11 }12 throw new Error('a and b are required');13};14const { test } = require('ava');15const { sum } = require('./sum');16test('sum', t => {17 t.throws(() => {18 sum(1, 2);19 }, 'a and b are required');20});21exports.sum = (a, b) =>
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!