Best JavaScript code snippet using jest
asymmetricMatchers.test.js
Source:asymmetricMatchers.test.js
...18 stringNotContaining,19 stringMatching,20 stringNotMatching,21} = require('../asymmetricMatchers');22test('Any.asymmetricMatch()', () => {23 const Thing = function() {};24 [25 any(String).asymmetricMatch('jest'),26 any(Number).asymmetricMatch(1),27 any(Function).asymmetricMatch(() => {}),28 any(Boolean).asymmetricMatch(true),29 any(Object).asymmetricMatch({}),30 any(Array).asymmetricMatch([]),31 any(Thing).asymmetricMatch(new Thing()),32 ].forEach(test => {33 jestExpect(test).toBe(true);34 });35});36test('Any.toAsymmetricMatcher()', () => {37 jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');38});39test('Any throws when called with empty constructor', () => {40 jestExpect(() => any()).toThrow();41});42test('Anything matches any type', () => {43 [44 anything().asymmetricMatch('jest'),45 anything().asymmetricMatch(1),46 anything().asymmetricMatch(() => {}),47 anything().asymmetricMatch(true),48 anything().asymmetricMatch({}),49 anything().asymmetricMatch([]),50 ].forEach(test => {51 jestExpect(test).toBe(true);52 });53});54test('Anything does not match null and undefined', () => {55 [56 anything().asymmetricMatch(null),57 anything().asymmetricMatch(undefined),58 ].forEach(test => {59 jestExpect(test).toBe(false);60 });61});62test('Anything.toAsymmetricMatcher()', () => {63 jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');64});65test('ArrayContaining matches', () => {66 [67 arrayContaining([]).asymmetricMatch('jest'),68 arrayContaining(['foo']).asymmetricMatch(['foo']),69 arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),70 arrayContaining([]).asymmetricMatch({}),71 ].forEach(test => {72 jestExpect(test).toEqual(true);73 });74});75test('ArrayContaining does not match', () => {76 jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);77});78test('ArrayContaining throws for non-arrays', () => {79 jestExpect(() => {80 arrayContaining('foo').asymmetricMatch([]);81 }).toThrow();82});83test('ArrayNotContaining matches', () => {84 jestExpect(arrayNotContaining(['foo']).asymmetricMatch(['bar'])).toBe(true);85});86test('ArrayNotContaining does not match', () => {87 [88 arrayNotContaining([]).asymmetricMatch('jest'),89 arrayNotContaining(['foo']).asymmetricMatch(['foo']),90 arrayNotContaining(['foo']).asymmetricMatch(['foo', 'bar']),91 arrayNotContaining([]).asymmetricMatch({}),92 ].forEach(test => {93 jestExpect(test).toEqual(false);94 });95});96test('ArrayNotContaining throws for non-arrays', () => {97 jestExpect(() => {98 arrayNotContaining('foo').asymmetricMatch([]);99 }).toThrow();100});101test('ObjectContaining matches', () => {102 [103 objectContaining({}).asymmetricMatch('jest'),104 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),105 objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),106 objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({107 first: {second: {}},108 }),109 ].forEach(test => {110 jestExpect(test).toEqual(true);111 });112});113test('ObjectContaining does not match', () => {114 [115 objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),116 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),117 objectContaining({foo: undefined}).asymmetricMatch({}),118 ].forEach(test => {119 jestExpect(test).toEqual(false);120 });121});122test('ObjectContaining matches defined properties', () => {123 const definedPropertyObject = {};124 Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});125 jestExpect(126 objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),127 ).toBe(true);128});129test('ObjectContaining matches prototype properties', () => {130 const prototypeObject = {foo: 'bar'};131 let obj;132 if (Object.create) {133 obj = Object.create(prototypeObject);134 } else {135 function Foo() {}136 Foo.prototype = prototypeObject;137 Foo.prototype.constructor = Foo;138 obj = new Foo();139 }140 jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);141});142test('ObjectContaining throws for non-objects', () => {143 jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();144});145test('ObjectNotContaining matches', () => {146 [147 objectNotContaining({}).asymmetricMatch('jest'),148 objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),149 objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),150 objectNotContaining({foo: undefined}).asymmetricMatch({}),151 ].forEach(test => {152 jestExpect(test).toEqual(true);153 });154});155test('ObjectNotContaining does not match', () => {156 [157 objectNotContaining({foo: 'foo'}).asymmetricMatch({158 foo: 'foo',159 jest: 'jest',160 }),161 objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),162 objectNotContaining({163 first: objectNotContaining({second: {}}),164 }).asymmetricMatch({first: {second: {}}}),165 ].forEach(test => {166 jestExpect(test).toEqual(false);167 });168});169test('ObjectNotContaining throws for non-objects', () => {170 jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();171});172test('StringContaining matches string against string', () => {173 jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);174 jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);175});176test('StringContaining throws if expected value is not string', () => {177 jestExpect(() => {178 stringContaining([1]).asymmetricMatch('queen');179 }).toThrow();180});181test('StringContaining returns false if received value is not string', () => {182 jestExpect(stringContaining('en*').asymmetricMatch(1)).toBe(false);183});184test('StringNotContaining matches string against string', () => {185 jestExpect(stringNotContaining('en*').asymmetricMatch('queen*')).toBe(false);186 jestExpect(stringNotContaining('en').asymmetricMatch('queue')).toBe(true);187});188test('StringNotContaining throws if expected value is not string', () => {189 jestExpect(() => {190 stringNotContaining([1]).asymmetricMatch('queen');191 }).toThrow();192});193test('StringNotContaining returns true if received value is not string', () => {194 jestExpect(stringNotContaining('en*').asymmetricMatch(1)).toBe(true);195});196test('StringMatching matches string against regexp', () => {197 jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);198 jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);199});200test('StringMatching matches string against string', () => {201 jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);202 jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);203});204test('StringMatching throws if expected value is neither string nor regexp', () => {205 jestExpect(() => {206 stringMatching([1]).asymmetricMatch('queen');207 }).toThrow();208});209test('StringMatching returns false if received value is not string', () => {210 jestExpect(stringMatching('en').asymmetricMatch(1)).toBe(false);211});212test('StringMatching returns false even if coerced non-string received value matches pattern', () => {213 jestExpect(stringMatching('null').asymmetricMatch(null)).toBe(false);214});215test('StringNotMatching matches string against regexp', () => {216 jestExpect(stringNotMatching(/en/).asymmetricMatch('queen')).toBe(false);217 jestExpect(stringNotMatching(/en/).asymmetricMatch('queue')).toBe(true);218});219test('StringNotMatching matches string against string', () => {220 jestExpect(stringNotMatching('en').asymmetricMatch('queen')).toBe(false);221 jestExpect(stringNotMatching('en').asymmetricMatch('queue')).toBe(true);222});223test('StringNotMatching throws if expected value is neither string nor regexp', () => {224 jestExpect(() => {225 stringNotMatching([1]).asymmetricMatch('queen');226 }).toThrow();227});228test('StringNotMatching returns true if received value is not string', () => {229 jestExpect(stringNotMatching('en').asymmetricMatch(1)).toBe(true);...
asymmetric_matchers.test.js
Source:asymmetric_matchers.test.js
...18 stringNotContaining,19 stringMatching,20 stringNotMatching,21} = require('../asymmetric_matchers');22test('Any.asymmetricMatch()', () => {23 const Thing = function() {};24 [25 any(String).asymmetricMatch('jest'),26 any(Number).asymmetricMatch(1),27 any(Function).asymmetricMatch(() => {}),28 any(Boolean).asymmetricMatch(true),29 any(Object).asymmetricMatch({}),30 any(Array).asymmetricMatch([]),31 any(Thing).asymmetricMatch(new Thing()),32 ].forEach(test => {33 jestExpect(test).toBe(true);34 });35});36test('Any.toAsymmetricMatcher()', () => {37 jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');38});39test('Any throws when called with empty constructor', () => {40 jestExpect(() => any()).toThrow();41});42test('Anything matches any type', () => {43 [44 anything().asymmetricMatch('jest'),45 anything().asymmetricMatch(1),46 anything().asymmetricMatch(() => {}),47 anything().asymmetricMatch(true),48 anything().asymmetricMatch({}),49 anything().asymmetricMatch([]),50 ].forEach(test => {51 jestExpect(test).toBe(true);52 });53});54test('Anything does not match null and undefined', () => {55 [56 anything().asymmetricMatch(null),57 anything().asymmetricMatch(undefined),58 ].forEach(test => {59 jestExpect(test).toBe(false);60 });61});62test('Anything.toAsymmetricMatcher()', () => {63 jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');64});65test('ArrayContaining matches', () => {66 [67 arrayContaining([]).asymmetricMatch('jest'),68 arrayContaining(['foo']).asymmetricMatch(['foo']),69 arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),70 arrayContaining([]).asymmetricMatch({}),71 ].forEach(test => {72 jestExpect(test).toEqual(true);73 });74});75test('ArrayContaining does not match', () => {76 jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);77});78test('ArrayContaining throws for non-arrays', () => {79 jestExpect(() => {80 arrayContaining('foo').asymmetricMatch([]);81 }).toThrow();82});83test('ArrayNotContaining matches', () => {84 jestExpect(arrayNotContaining(['foo']).asymmetricMatch(['bar'])).toBe(true);85});86test('ArrayNotContaining does not match', () => {87 [88 arrayNotContaining([]).asymmetricMatch('jest'),89 arrayNotContaining(['foo']).asymmetricMatch(['foo']),90 arrayNotContaining(['foo']).asymmetricMatch(['foo', 'bar']),91 arrayNotContaining([]).asymmetricMatch({}),92 ].forEach(test => {93 jestExpect(test).toEqual(false);94 });95});96test('ArrayNotContaining throws for non-arrays', () => {97 jestExpect(() => {98 arrayNotContaining('foo').asymmetricMatch([]);99 }).toThrow();100});101test('ObjectContaining matches', () => {102 [103 objectContaining({}).asymmetricMatch('jest'),104 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),105 objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),106 objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({107 first: {second: {}},108 }),109 ].forEach(test => {110 jestExpect(test).toEqual(true);111 });112});113test('ObjectContaining does not match', () => {114 [115 objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),116 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),117 objectContaining({foo: undefined}).asymmetricMatch({}),118 ].forEach(test => {119 jestExpect(test).toEqual(false);120 });121});122test('ObjectContaining matches defined properties', () => {123 const definedPropertyObject = {};124 Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});125 jestExpect(126 objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),127 ).toBe(true);128});129test('ObjectContaining matches prototype properties', () => {130 const prototypeObject = {foo: 'bar'};131 let obj;132 if (Object.create) {133 obj = Object.create(prototypeObject);134 } else {135 function Foo() {}136 Foo.prototype = prototypeObject;137 Foo.prototype.constructor = Foo;138 obj = new Foo();139 }140 jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);141});142test('ObjectContaining throws for non-objects', () => {143 jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();144});145test('ObjectNotContaining matches', () => {146 [147 objectNotContaining({}).asymmetricMatch('jest'),148 objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),149 objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),150 objectNotContaining({foo: undefined}).asymmetricMatch({}),151 ].forEach(test => {152 jestExpect(test).toEqual(true);153 });154});155test('ObjectNotContaining does not match', () => {156 [157 objectNotContaining({foo: 'foo'}).asymmetricMatch({158 foo: 'foo',159 jest: 'jest',160 }),161 objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),162 objectNotContaining({163 first: objectNotContaining({second: {}}),164 }).asymmetricMatch({first: {second: {}}}),165 ].forEach(test => {166 jestExpect(test).toEqual(false);167 });168});169test('ObjectNotContaining throws for non-objects', () => {170 jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();171});172test('StringContaining matches string against string', () => {173 jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);174 jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);175});176test('StringContaining throws for non-strings', () => {177 jestExpect(() => {178 stringContaining([1]).asymmetricMatch('queen');179 }).toThrow();180 jestExpect(() => {181 stringContaining('en*').asymmetricMatch(1);182 }).toThrow();183});184test('StringNotContaining matches string against string', () => {185 jestExpect(stringNotContaining('en*').asymmetricMatch('queen*')).toBe(false);186 jestExpect(stringNotContaining('en').asymmetricMatch('queue')).toBe(true);187});188test('StringNotContaining throws for non-strings', () => {189 jestExpect(() => {190 stringNotContaining([1]).asymmetricMatch('queen');191 }).toThrow();192 jestExpect(() => {193 stringNotContaining('en*').asymmetricMatch(1);194 }).toThrow();195});196test('StringMatching matches string against regexp', () => {197 jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);198 jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);199});200test('StringMatching matches string against string', () => {201 jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);202 jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);203});204test('StringMatching throws for non-strings and non-regexps', () => {205 jestExpect(() => {206 stringMatching([1]).asymmetricMatch('queen');207 }).toThrow();208});209test('StringMatching throws for non-string actual values', () => {210 jestExpect(() => {211 stringMatching('en').asymmetricMatch(1);212 }).toThrow();213});214test('StringNotMatching matches string against regexp', () => {215 jestExpect(stringNotMatching(/en/).asymmetricMatch('queen')).toBe(false);216 jestExpect(stringNotMatching(/en/).asymmetricMatch('queue')).toBe(true);217});218test('StringNotMatching matches string against string', () => {219 jestExpect(stringNotMatching('en').asymmetricMatch('queen')).toBe(false);220 jestExpect(stringNotMatching('en').asymmetricMatch('queue')).toBe(true);221});222test('StringNotMatching throws for non-strings and non-regexps', () => {223 jestExpect(() => {224 stringNotMatching([1]).asymmetricMatch('queen');225 }).toThrow();226});227test('StringNotMatching throws for non-string actual values', () => {228 jestExpect(() => {229 stringNotMatching('en').asymmetricMatch(1);230 }).toThrow();...
28asymmetricMatchers.test.js
Source:28asymmetricMatchers.test.js
...18 stringNotContaining,19 stringMatching,20 stringNotMatching,21} = require('../asymmetricMatchers');22test('Any.asymmetricMatch()', () => {23 const Thing = function() {};24 [25 any(String).asymmetricMatch('jest'),26 any(Number).asymmetricMatch(1),27 any(Function).asymmetricMatch(() => {}),28 any(Boolean).asymmetricMatch(true),29 any(Object).asymmetricMatch({}),30 any(Array).asymmetricMatch([]),31 any(Thing).asymmetricMatch(new Thing()),32 ].forEach(test => {33 jestExpect(test).toBe(true);34 });35});36test('Any.toAsymmetricMatcher()', () => {37 jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');38});39test('Any throws when called with empty constructor', () => {40 jestExpect(() => any()).toThrow();41});42test('Anything matches any type', () => {43 [44 anything().asymmetricMatch('jest'),45 anything().asymmetricMatch(1),46 anything().asymmetricMatch(() => {}),47 anything().asymmetricMatch(true),48 anything().asymmetricMatch({}),49 anything().asymmetricMatch([]),50 ].forEach(test => {51 jestExpect(test).toBe(true);52 });53});54test('Anything does not match null and undefined', () => {55 [56 anything().asymmetricMatch(null),57 anything().asymmetricMatch(undefined),58 ].forEach(test => {59 jestExpect(test).toBe(false);60 });61});62test('Anything.toAsymmetricMatcher()', () => {63 jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');64});65test('ArrayContaining matches', () => {66 [67 arrayContaining([]).asymmetricMatch('jest'),68 arrayContaining(['foo']).asymmetricMatch(['foo']),69 arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),70 arrayContaining([]).asymmetricMatch({}),71 ].forEach(test => {72 jestExpect(test).toEqual(true);73 });74});75test('ArrayContaining does not match', () => {76 jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);77});78test('ArrayContaining throws for non-arrays', () => {79 jestExpect(() => {80 arrayContaining('foo').asymmetricMatch([]);81 }).toThrow();82});83test('ArrayNotContaining matches', () => {84 jestExpect(arrayNotContaining(['foo']).asymmetricMatch(['bar'])).toBe(true);85});86test('ArrayNotContaining does not match', () => {87 [88 arrayNotContaining([]).asymmetricMatch('jest'),89 arrayNotContaining(['foo']).asymmetricMatch(['foo']),90 arrayNotContaining(['foo']).asymmetricMatch(['foo', 'bar']),91 arrayNotContaining([]).asymmetricMatch({}),92 ].forEach(test => {93 jestExpect(test).toEqual(false);94 });95});96test('ArrayNotContaining throws for non-arrays', () => {97 jestExpect(() => {98 arrayNotContaining('foo').asymmetricMatch([]);99 }).toThrow();100});101test('ObjectContaining matches', () => {102 [103 objectContaining({}).asymmetricMatch('jest'),104 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),105 objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),106 objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({107 first: {second: {}},108 }),109 ].forEach(test => {110 jestExpect(test).toEqual(true);111 });112});113test('ObjectContaining does not match', () => {114 [115 objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),116 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),117 objectContaining({foo: undefined}).asymmetricMatch({}),118 ].forEach(test => {119 jestExpect(test).toEqual(false);120 });121});122test('ObjectContaining matches defined properties', () => {123 const definedPropertyObject = {};124 Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});125 jestExpect(126 objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),127 ).toBe(true);128});129test('ObjectContaining matches prototype properties', () => {130 const prototypeObject = {foo: 'bar'};131 let obj;132 if (Object.create) {133 obj = Object.create(prototypeObject);134 } else {135 function Foo() {}136 Foo.prototype = prototypeObject;137 Foo.prototype.constructor = Foo;138 obj = new Foo();139 }140 jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);141});142test('ObjectContaining throws for non-objects', () => {143 jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();144});145test('ObjectNotContaining matches', () => {146 [147 objectNotContaining({}).asymmetricMatch('jest'),148 objectNotContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),149 objectNotContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),150 objectNotContaining({foo: undefined}).asymmetricMatch({}),151 ].forEach(test => {152 jestExpect(test).toEqual(true);153 });154});155test('ObjectNotContaining does not match', () => {156 [157 objectNotContaining({foo: 'foo'}).asymmetricMatch({158 foo: 'foo',159 jest: 'jest',160 }),161 objectNotContaining({foo: undefined}).asymmetricMatch({foo: undefined}),162 objectNotContaining({163 first: objectNotContaining({second: {}}),164 }).asymmetricMatch({first: {second: {}}}),165 ].forEach(test => {166 jestExpect(test).toEqual(false);167 });168});169test('ObjectNotContaining throws for non-objects', () => {170 jestExpect(() => objectNotContaining(1337).asymmetricMatch()).toThrow();171});172test('StringContaining matches string against string', () => {173 jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);174 jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);175});176test('StringContaining throws for non-strings', () => {177 jestExpect(() => {178 stringContaining([1]).asymmetricMatch('queen');179 }).toThrow();180 jestExpect(() => {181 stringContaining('en*').asymmetricMatch(1);182 }).toThrow();183});184test('StringNotContaining matches string against string', () => {185 jestExpect(stringNotContaining('en*').asymmetricMatch('queen*')).toBe(false);186 jestExpect(stringNotContaining('en').asymmetricMatch('queue')).toBe(true);187});188test('StringNotContaining throws for non-strings', () => {189 jestExpect(() => {190 stringNotContaining([1]).asymmetricMatch('queen');191 }).toThrow();192 jestExpect(() => {193 stringNotContaining('en*').asymmetricMatch(1);194 }).toThrow();195});196test('StringMatching matches string against regexp', () => {197 jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);198 jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);199});200test('StringMatching matches string against string', () => {201 jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);202 jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);203});204test('StringMatching throws for non-strings and non-regexps', () => {205 jestExpect(() => {206 stringMatching([1]).asymmetricMatch('queen');207 }).toThrow();208});209test('StringMatching throws for non-string actual values', () => {210 jestExpect(() => {211 stringMatching('en').asymmetricMatch(1);212 }).toThrow();213});214test('StringNotMatching matches string against regexp', () => {215 jestExpect(stringNotMatching(/en/).asymmetricMatch('queen')).toBe(false);216 jestExpect(stringNotMatching(/en/).asymmetricMatch('queue')).toBe(true);217});218test('StringNotMatching matches string against string', () => {219 jestExpect(stringNotMatching('en').asymmetricMatch('queen')).toBe(false);220 jestExpect(stringNotMatching('en').asymmetricMatch('queue')).toBe(true);221});222test('StringNotMatching throws for non-strings and non-regexps', () => {223 jestExpect(() => {224 stringNotMatching([1]).asymmetricMatch('queen');225 }).toThrow();226});227test('StringNotMatching throws for non-string actual values', () => {228 jestExpect(() => {229 stringNotMatching('en').asymmetricMatch(1);230 }).toThrow();...
asymmetric-matchers-test.js
Source:asymmetric-matchers-test.js
...17 objectContaining,18 stringContaining,19 stringMatching,20} = require('../asymmetric-matchers');21test('Any.asymmetricMatch()', () => {22 const Thing = function() {};23 [24 any(String).asymmetricMatch('jest'),25 any(Number).asymmetricMatch(1),26 any(Function).asymmetricMatch(() => {}),27 any(Boolean).asymmetricMatch(true),28 any(Object).asymmetricMatch({}),29 any(Array).asymmetricMatch([]),30 any(Thing).asymmetricMatch(new Thing()),31 ].forEach(test => {32 jestExpect(test).toBe(true);33 });34});35test('Any.toAsymmetricMatcher()', () => {36 jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');37});38test('Any throws when called with empty constructor', () => {39 jestExpect(() => any()).toThrow();40});41test('Anything matches any type', () => {42 [43 anything().asymmetricMatch('jest'),44 anything().asymmetricMatch(1),45 anything().asymmetricMatch(() => {}),46 anything().asymmetricMatch(true),47 anything().asymmetricMatch({}),48 anything().asymmetricMatch([]),49 ].forEach(test => {50 jestExpect(test).toBe(true);51 });52});53test('Anything does not match null and undefined', () => {54 [55 anything().asymmetricMatch(null),56 anything().asymmetricMatch(undefined),57 ].forEach(test => {58 jestExpect(test).toBe(false);59 });60});61test('Anything.toAsymmetricMatcher()', () => {62 jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');63});64test('ArrayContaining matches', () => {65 [66 arrayContaining([]).asymmetricMatch('jest'),67 arrayContaining(['foo']).asymmetricMatch(['foo']),68 arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),69 arrayContaining([]).asymmetricMatch({}),70 ].forEach(test => {71 jestExpect(test).toEqual(true);72 });73});74test('ArrayContaining does not match', () => {75 jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);76});77test('ArrayContaining throws for non-arrays', () => {78 jestExpect(() => {79 arrayContaining('foo').asymmetricMatch([]);80 }).toThrow();81});82test('ObjectContaining matches', () => {83 [84 objectContaining({}).asymmetricMatch('jest'),85 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),86 objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),87 objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({88 first: {second: {}},89 }),90 ].forEach(test => {91 jestExpect(test).toEqual(true);92 });93});94test('ObjectContaining does not match', () => {95 [96 objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),97 objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),98 objectContaining({foo: undefined}).asymmetricMatch({}),99 ].forEach(test => {100 jestExpect(test).toEqual(false);101 });102});103test('ObjectContaining matches defined properties', () => {104 const definedPropertyObject = {};105 Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});106 jestExpect(107 objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),108 ).toBe(true);109});110test('ObjectContaining matches prototype properties', () => {111 const prototypeObject = {foo: 'bar'};112 let obj;113 if (Object.create) {114 obj = Object.create(prototypeObject);115 } else {116 function Foo() {}117 Foo.prototype = prototypeObject;118 Foo.prototype.constructor = Foo;119 obj = new Foo();120 }121 jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);122});123test('ObjectContaining throws for non-objects', () => {124 jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();125});126test('StringContaining matches string against string', () => {127 jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);128 jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);129});130test('StringContaining throws for non-strings', () => {131 jestExpect(() => {132 stringContaining([1]).asymmetricMatch('queen');133 }).toThrow();134});135test('StringMatching matches string against regexp', () => {136 jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);137 jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);138});139test('StringMatching matches string against string', () => {140 jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);141 jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);142});143test('StringMatching throws for non-strings and non-regexps', () => {144 jestExpect(() => {145 stringMatching([1]).asymmetricMatch('queen');146 }).toThrow();...
custom-asymmetric-matchers.js
Source:custom-asymmetric-matchers.js
...15 return this.toString();16 }17}18class SpacexComposite extends CustomAsymmetricMatcher {19 asymmetricMatch(any) {20 return any !== undefined && typeof any === 'object';21 }22 getExpectedType() {23 return 'object';24 }25}26/**27 * Expect metric and imperial volume numbers in object.28 */29class SpacexVolume extends SpacexComposite {30 asymmetricMatch(any) {31 if (!super.asymmetricMatch(any)) {32 return false;33 }34 expect(any).toHaveProperty('cubic_meters');35 expect(any).toHaveProperty('cubic_feet');36 return true;37 }38 toString() {39 return 'SpacexVolume';40 }41}42/**43 * Expect metric and imperial length (or dimension) numbers in object.44 */45class SpacexLength extends SpacexComposite {46 asymmetricMatch(any) {47 if (!super.asymmetricMatch(any)) {48 return false;49 }50 expect(any).toHaveProperty('meters');51 expect(any).toHaveProperty('feet');52 return true;53 }54 toString() {55 return 'SpacexLength';56 }57}58/**59 * Expect metric and imperial mass numbers in object.60 */61class SpacexMass extends SpacexComposite {62 asymmetricMatch(any) {63 if (!super.asymmetricMatch(any)) {64 return false;65 }66 expect(any).toHaveProperty('kg');67 expect(any).toHaveProperty('lb');68 return true;69 }70 toString() {71 return 'SpacexMass';72 }73}74/**75 * Expect metric and imperial thrust numbers in object.76 */77class SpacexThrust extends SpacexComposite {78 asymmetricMatch(any) {79 if (!super.asymmetricMatch(any)) {80 return false;81 }82 expect(any).toHaveProperty('kN');83 expect(any).toHaveProperty('lbf');84 return true;85 }86 toString() {87 return 'SpacexThrust';88 }89}90/**91 * Expect composite payload weight object.92 */93class SpacexPayloadWeight extends SpacexComposite {94 asymmetricMatch(any) {95 if (!super.asymmetricMatch(any)) {96 return false;97 }98 expect(any).toHaveProperty('id');99 expect(any).toHaveProperty('name');100 expect(any).toEqual(new SpacexMass());101 return true;102 }103 toString() {104 return 'SpacexPayloadWeight';105 }106}107/**108 * Expect composite stage information object.109 */110class SpacexVehicleStage extends SpacexComposite {111 asymmetricMatch(any) {112 if (!super.asymmetricMatch(any)) {113 return false;114 }115 // expect(any).toHaveProperty('reusable', expect.any(Boolean))116 // expect(any).toHaveProperty('engines', expect.any(String))117 // expect(any).toHaveProperty('fuel_amount_tons', expect.any(Number))118 expect(any).toHaveProperty('burn_time_sec');119 // expect(any).toHaveProperty('thrust_sea_level', new SpacexThrust())120 // expect(any).toHaveProperty('thrust_vacuum', new SpacexThrust())121 // expect(any).toHaveProperty('payloads', expect.any(String)) //122 return true;123 }124 toString() {125 return 'SpacexPayloadWeight';126 }...
ObjectContainingSpec.js
Source:ObjectContainingSpec.js
1describe("ObjectContaining", function() {2 it("matches any actual to an empty object", function() {3 var containing = new j$.ObjectContaining({});4 expect(containing.asymmetricMatch("foo")).toBe(true);5 });6 it("does not match an empty object actual", function() {7 var containing = new j$.ObjectContaining("foo");8 expect(function() {9 containing.asymmetricMatch({})10 }).toThrowError(/not 'foo'/)11 });12 it("matches when the key/value pair is present in the actual", function() {13 var containing = new j$.ObjectContaining({foo: "fooVal"});14 expect(containing.asymmetricMatch({foo: "fooVal", bar: "barVal"})).toBe(true);15 });16 it("does not match when the key/value pair is not present in the actual", function() {17 var containing = new j$.ObjectContaining({foo: "fooVal"});18 expect(containing.asymmetricMatch({bar: "barVal", quux: "quuxVal"})).toBe(false);19 });20 it("does not match when the key is present but the value is different in the actual", function() {21 var containing = new j$.ObjectContaining({foo: "other"});22 expect(containing.asymmetricMatch({foo: "fooVal", bar: "barVal"})).toBe(false);23 });24 it("jasmineToString's itself", function() {25 var containing = new j$.ObjectContaining({});26 expect(containing.jasmineToString()).toMatch("<jasmine.objectContaining");27 });28 it("matches recursively", function() {29 var containing = new j$.ObjectContaining({one: new j$.ObjectContaining({two: {}})});30 expect(containing.asymmetricMatch({one: {two: {}}})).toBe(true);31 });32 it("matches when key is present with undefined value", function() {33 var containing = new j$.ObjectContaining({ one: undefined });34 expect(containing.asymmetricMatch({ one: undefined })).toBe(true);35 });36 it("does not match when key with undefined value is not present", function() {37 var containing = new j$.ObjectContaining({ one: undefined });38 expect(containing.asymmetricMatch({})).toBe(false);39 });40 it("matches defined properties", function(){41 // IE 8 doesn't support `definePropery` on non-DOM nodes42 if (jasmine.getEnv().ieVersion < 9) { return; }43 var containing = new j$.ObjectContaining({ foo: "fooVal" });44 var definedPropertyObject = {};45 Object.defineProperty(definedPropertyObject, "foo", {46 get: function() { return "fooVal" }47 });48 expect(containing.asymmetricMatch(definedPropertyObject)).toBe(true);49 });50 it("matches prototype properties", function(){51 var containing = new j$.ObjectContaining({ foo: "fooVal" });52 var prototypeObject = {foo: "fooVal"};53 var obj;54 if (Object.create) {55 obj = Object.create(prototypeObject);56 } else {57 function Foo() {}58 Foo.prototype = prototypeObject;59 Foo.prototype.constructor = Foo;60 obj = new Foo();61 }62 expect(containing.asymmetricMatch(obj)).toBe(true);63 });...
TruthySpec.js
Source:TruthySpec.js
1describe("Truthy", function () {2 it("is true for a non empty string", function () {3 var truthy = new jasmineUnderTest.Truthy();4 expect(truthy.asymmetricMatch("foo")).toBe(true);5 expect(truthy.asymmetricMatch("")).toBe(false);6 });7 it("is true for a number that is not 0", function () {8 var truthy = new jasmineUnderTest.Truthy();9 expect(truthy.asymmetricMatch(1)).toBe(true);10 expect(truthy.asymmetricMatch(0)).toBe(false);11 expect(truthy.asymmetricMatch(-23)).toBe(true);12 expect(truthy.asymmetricMatch(-3.1)).toBe(true);13 });14 it("is true for a function", function () {15 var truthy = new jasmineUnderTest.Truthy();16 expect(truthy.asymmetricMatch(function () {17 })).toBe(true);18 });19 it("is true for an Object", function () {20 var truthy = new jasmineUnderTest.Truthy();21 expect(truthy.asymmetricMatch({})).toBe(true);22 });23 it("is true for a truthful Boolean", function () {24 var truthy = new jasmineUnderTest.Truthy();25 expect(truthy.asymmetricMatch(true)).toBe(true);26 expect(truthy.asymmetricMatch(false)).toBe(false);27 });28 it("is true for an empty object", function () {29 var truthy = new jasmineUnderTest.Truthy();30 expect(truthy.asymmetricMatch({})).toBe(true);31 });32 it("is true for an empty array", function () {33 var truthy = new jasmineUnderTest.Truthy();34 expect(truthy.asymmetricMatch([])).toBe(true);35 });36 it("is true for a date", function () {37 var truthy = new jasmineUnderTest.Truthy();38 expect(truthy.asymmetricMatch(new Date())).toBe(true);39 });40 it("is true for a infiniti", function () {41 var truthy = new jasmineUnderTest.Truthy();42 expect(truthy.asymmetricMatch(Infinity)).toBe(true);43 expect(truthy.asymmetricMatch(-Infinity)).toBe(true);44 });...
AnythingSpec.js
Source:AnythingSpec.js
1describe("Anything", function() {2 it("matches a string", function() {3 var anything = new j$.Anything();4 expect(anything.asymmetricMatch('foo')).toBe(true);5 });6 it("matches a number", function() {7 var anything = new j$.Anything();8 expect(anything.asymmetricMatch(42)).toBe(true);9 });10 it("matches an object", function() {11 var anything = new j$.Anything();12 expect(anything.asymmetricMatch({ foo: 'bar' })).toBe(true);13 });14 it("matches an array", function() {15 var anything = new j$.Anything();16 expect(anything.asymmetricMatch([1,2,3])).toBe(true);17 });18 it("doesn't match undefined", function() {19 var anything = new j$.Anything();20 expect(anything.asymmetricMatch()).toBe(false);21 expect(anything.asymmetricMatch(undefined)).toBe(false);22 });23 it("doesn't match null", function() {24 var anything = new j$.Anything();25 expect(anything.asymmetricMatch(null)).toBe(false);26 });27 it("jasmineToString's itself", function() {28 var anything = new j$.Anything();29 expect(anything.jasmineToString()).toEqual("<jasmine.anything>");30 });...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!