Best JavaScript code snippet using jest
toThrowError.js
Source:toThrowError.js
...25 if (!(thrown instanceof Error)) {26 return fail("Expected function to throw an Error, but it threw " + thrown + ".");27 }28 if (arguments.length == 1) {29 return pass("Expected function not to throw an Error, but it threw " + fnNameFor(thrown) + ".");30 }31 if (errorType) {32 name = fnNameFor(errorType);33 constructorName = fnNameFor(thrown.constructor);34 }35 if (errorType && message) {36 if (thrown.constructor == errorType && util.equals(thrown.message, message)) {37 return pass("Expected function not to throw " + name + " with message \"" + message + "\".");38 } else {39 return fail("Expected function to throw " + name + " with message \"" + message +40 "\", but it threw " + constructorName + " with message \"" + thrown.message + "\".");41 }42 }43 if (errorType && regexp) {44 if (thrown.constructor == errorType && regexp.test(thrown.message)) {45 return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");46 } else {47 return fail("Expected function to throw " + name + " with message matching " + regexp +48 ", but it threw " + constructorName + " with message \"" + thrown.message + "\".");49 }50 }51 if (errorType) {52 if (thrown.constructor == errorType) {53 return pass("Expected function not to throw " + name + ".");54 } else {55 return fail("Expected function to throw " + name + ", but it threw " + constructorName + ".");56 }57 }58 if (message) {59 if (thrown.message == message) {60 return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");61 } else {62 return fail("Expected function to throw an exception with message " + j$.pp(message) +63 ", but it threw an exception with message " + j$.pp(thrown.message) + ".");64 }65 }66 if (regexp) {67 if (regexp.test(thrown.message)) {68 return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");69 } else {70 return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +71 ", but it threw an exception with message " + j$.pp(thrown.message) + ".");72 }73 }74 function fnNameFor(func) {75 return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];76 }77 function pass(notMessage) {78 return {79 pass: true,80 message: notMessage81 };82 }83 function fail(message) {84 return {85 pass: false,86 message: message87 };88 }...
asymmetric-matchers.js
Source:asymmetric-matchers.js
...58 }59 if (this.sample == Boolean) {60 return 'boolean';61 }62 return fnNameFor(this.sample);63 }64 toAsymmetricMatcher() {65 return 'Any<' + fnNameFor(this.sample) + '>';66 }}67class Anything extends AsymmetricMatcher {68 asymmetricMatch(other) {69 return !isUndefined(other) && other !== null;70 }71 toString() {72 return 'Anything';73 }74 // No getExpectedType method, because it matches either null or undefined.75 toAsymmetricMatcher() {76 return 'Anything';77 }}78class ArrayContaining extends AsymmetricMatcher {79 constructor(sample) {...
AsymmetricMatcher-test.js
Source:AsymmetricMatcher-test.js
...9'use strict';10const prettyFormat = require('../');11const AsymmetricMatcher = require('../plugins/AsymmetricMatcher');12let options;13function fnNameFor(func) {14 if (func.name) {15 return func.name;16 }17 const matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/);18 return matches ? matches[1] : '<anonymous>';19}20beforeEach(() => {21 options = {plugins: [AsymmetricMatcher]};22});23[24 String,25 Function,26 Array,27 Object,28 RegExp,29 Symbol,30 Function,31 () => {},32 function namedFuntction() {},33].forEach(type => {34 test(`supports any(${fnNameFor(type)})`, () => {35 const result = prettyFormat(expect.any(type), options);36 expect(result).toEqual(`Any<${fnNameFor(type)}>`);37 });38 test(`supports nested any(${fnNameFor(type)})`, () => {39 const result = prettyFormat(40 {41 test: {42 nested: expect.any(type),43 },44 },45 options,46 );47 expect(result).toEqual(48 `Object {\n "test": Object {\n "nested": Any<${fnNameFor(type)}>,\n },\n}`,49 );50 });51});52test(`anything()`, () => {53 const result = prettyFormat(expect.anything(), options);54 expect(result).toEqual('Anything');55});56test(`arrayContaining()`, () => {57 const result = prettyFormat(expect.arrayContaining([1, 2]), options);58 expect(result).toEqual(59 `ArrayContaining [60 1,61 2,62]`,...
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!!