Best JavaScript code snippet using ava
assert.js
Source:assert.js
1'use strict';2require('../lib/chalk').set();3require('../lib/worker/options').set({color: false});4const path = require('path');5const stripAnsi = require('strip-ansi');6const React = require('react');7const renderer = require('react-test-renderer');8const test = require('tap').test;9const assert = require('../lib/assert');10const snapshotManager = require('../lib/snapshot-manager');11const Test = require('../lib/test');12const HelloMessage = require('./fixture/hello-message');13let lastFailure = null;14let lastPassed = false;15const assertions = assert.wrapAssertions({16 pass(testObj) {17 if (testObj !== assertions && !(testObj instanceof Test)) {18 throw new Error('Expected testObj');19 }20 lastPassed = true;21 },22 pending(testObj, promise) {23 if (testObj !== assertions && !(testObj instanceof Test)) {24 throw new Error('Expected testObj');25 }26 promise.then(() => {27 lastPassed = true;28 }, err => {29 lastFailure = err;30 });31 },32 fail(testObj, error) {33 if (testObj !== assertions && !(testObj instanceof Test)) {34 throw new Error('Expected testObj');35 }36 lastFailure = error;37 }38});39function assertFailure(t, subset) {40 if (!lastFailure) {41 t.fail('Expected assertion to fail');42 return;43 }44 t.is(lastFailure.assertion, subset.assertion);45 t.is(lastFailure.message, subset.message);46 t.is(lastFailure.name, 'AssertionError');47 t.is(lastFailure.operator, subset.operator);48 if (subset.raw) {49 t.is(lastFailure.raw.expected, subset.raw.expected);50 t.is(lastFailure.raw.actual, subset.raw.actual);51 }52 if (subset.statements) {53 t.is(lastFailure.statements.length, subset.statements.length);54 lastFailure.statements.forEach((s, i) => {55 t.is(s[0], subset.statements[i][0]);56 t.match(s[1], subset.statements[i][1]);57 });58 } else {59 t.same(lastFailure.statements, []);60 }61 if (subset.values) {62 t.is(lastFailure.values.length, subset.values.length);63 lastFailure.values.forEach((s, i) => {64 t.is(s.label, subset.values[i].label);65 t.match(stripAnsi(s.formatted), subset.values[i].formatted);66 });67 } else {68 t.same(lastFailure.values, []);69 }70}71let gathering = false;72let gatheringPromise = Promise.resolve();73function gather(run) {74 return t => {75 if (gathering) {76 throw new Error('Cannot nest gather()');77 }78 gathering = true;79 try {80 run(t);81 return gatheringPromise;82 } finally {83 gathering = false;84 gatheringPromise = Promise.resolve();85 }86 };87}88function add(fn) {89 if (!gathering) {90 throw new Error('Cannot add promise, must be called from gather() callback');91 }92 gatheringPromise = gatheringPromise.then(fn);93 return gatheringPromise;94}95function failsWith(t, fn, subset) {96 lastFailure = null;97 fn();98 assertFailure(t, subset);99}100function eventuallyFailsWith(t, fn, subset) {101 return add(() => {102 lastFailure = null;103 return fn().then(() => {104 assertFailure(t, subset);105 });106 });107}108function fails(t, fn) {109 lastFailure = null;110 fn();111 if (lastFailure) {112 t.pass();113 } else {114 t.fail('Expected assertion to fail');115 }116}117/* Might be useful118function eventuallyFails(t, fn) {119 return add(() => {120 lastFailure = null;121 return fn().then(() => {122 if (lastFailure) {123 t.pass();124 } else {125 t.fail('Expected assertion to fail');126 }127 });128 });129}130*/131function passes(t, fn) {132 lastPassed = false;133 lastFailure = null;134 fn();135 if (lastPassed) {136 t.pass();137 } else {138 t.ifError(lastFailure, 'Expected assertion to pass');139 }140}141function eventuallyPasses(t, fn) {142 return add(() => {143 lastPassed = false;144 lastFailure = null;145 return fn().then(() => {146 if (lastPassed) {147 t.pass();148 } else {149 t.ifError(lastFailure, 'Expected assertion to pass');150 }151 });152 });153}154test('.pass()', t => {155 passes(t, () => {156 assertions.pass();157 });158 t.end();159});160test('.fail()', t => {161 failsWith(t, () => {162 assertions.fail();163 }, {164 assertion: 'fail',165 message: 'Test failed via `t.fail()`'166 });167 failsWith(t, () => {168 assertions.fail('my message');169 }, {170 assertion: 'fail',171 message: 'my message'172 });173 t.end();174});175test('.is()', t => {176 passes(t, () => {177 assertions.is('foo', 'foo');178 });179 passes(t, () => {180 assertions.is('', '');181 });182 passes(t, () => {183 assertions.is(true, true);184 });185 passes(t, () => {186 assertions.is(false, false);187 });188 passes(t, () => {189 assertions.is(null, null);190 });191 passes(t, () => {192 assertions.is(undefined, undefined);193 });194 passes(t, () => {195 assertions.is(1, 1);196 });197 passes(t, () => {198 assertions.is(0, 0);199 });200 passes(t, () => {201 assertions.is(-0, -0);202 });203 passes(t, () => {204 assertions.is(NaN, NaN);205 });206 passes(t, () => {207 assertions.is(0 / 0, NaN);208 });209 passes(t, () => {210 const someRef = {foo: 'bar'};211 assertions.is(someRef, someRef);212 });213 fails(t, () => {214 assertions.is(0, -0);215 });216 fails(t, () => {217 assertions.is(0, false);218 });219 fails(t, () => {220 assertions.is('', false);221 });222 fails(t, () => {223 assertions.is('0', 0);224 });225 fails(t, () => {226 assertions.is('17', 17);227 });228 fails(t, () => {229 assertions.is([1, 2], '1,2');230 });231 fails(t, () => {232 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins233 assertions.is(new String('foo'), 'foo');234 });235 fails(t, () => {236 assertions.is(null, undefined);237 });238 fails(t, () => {239 assertions.is(null, false);240 });241 fails(t, () => {242 assertions.is(undefined, false);243 });244 fails(t, () => {245 // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins246 assertions.is(new String('foo'), new String('foo'));247 });248 fails(t, () => {249 assertions.is(0, null);250 });251 fails(t, () => {252 assertions.is(0, NaN);253 });254 fails(t, () => {255 assertions.is('foo', NaN);256 });257 failsWith(t, () => {258 assertions.is({foo: 'bar'}, {foo: 'bar'});259 }, {260 assertion: 'is',261 message: '',262 actual: {foo: 'bar'},263 expected: {foo: 'bar'},264 values: [{265 label: 'Values are deeply equal to each other, but they are not the same:',266 formatted: /foo/267 }]268 });269 failsWith(t, () => {270 assertions.is('foo', 'bar');271 }, {272 assertion: 'is',273 message: '',274 raw: {actual: 'foo', expected: 'bar'},275 values: [276 {label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}277 ]278 });279 failsWith(t, () => {280 assertions.is('foo', 42);281 }, {282 actual: 'foo',283 assertion: 'is',284 expected: 42,285 message: '',286 values: [287 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}288 ]289 });290 failsWith(t, () => {291 assertions.is('foo', 42, 'my message');292 }, {293 assertion: 'is',294 message: 'my message',295 values: [296 {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}297 ]298 });299 failsWith(t, () => {300 assertions.is(0, -0, 'my message');301 }, {302 assertion: 'is',303 message: 'my message',304 values: [305 {label: 'Difference:', formatted: /- 0\n\+ -0/}306 ]307 });308 failsWith(t, () => {309 assertions.is(-0, 0, 'my message');310 }, {311 assertion: 'is',312 message: 'my message',313 values: [314 {label: 'Difference:', formatted: /- -0\n\+ 0/}315 ]316 });317 t.end();318});319test('.not()', t => {320 passes(t, () => {321 assertions.not('foo', 'bar');322 });323 fails(t, () => {324 assertions.not(NaN, NaN);325 });326 fails(t, () => {327 assertions.not(0 / 0, NaN);328 });329 failsWith(t, () => {330 assertions.not('foo', 'foo');331 }, {332 assertion: 'not',333 message: '',334 raw: {actual: 'foo', expected: 'foo'},335 values: [{label: 'Value is the same as:', formatted: /foo/}]336 });337 failsWith(t, () => {338 assertions.not('foo', 'foo', 'my message');339 }, {340 assertion: 'not',341 message: 'my message',342 values: [{label: 'Value is the same as:', formatted: /foo/}]343 });344 t.end();345});346test('.deepEqual()', t => {347 // Tests starting here are to detect regressions in the underlying libraries348 // used to test deep object equality349 fails(t, () => {350 assertions.deepEqual({a: false}, {a: 0});351 });352 passes(t, () => {353 assertions.deepEqual({354 a: 'a',355 b: 'b'356 }, {357 b: 'b',358 a: 'a'359 });360 });361 passes(t, () => {362 assertions.deepEqual({363 a: 'a',364 b: 'b',365 c: {366 d: 'd'367 }368 }, {369 c: {370 d: 'd'371 },372 b: 'b',373 a: 'a'374 });375 });376 fails(t, () => {377 assertions.deepEqual([1, 2, 3], [1, 2, 3, 4]);378 });379 passes(t, () => {380 assertions.deepEqual([1, 2, 3], [1, 2, 3]);381 });382 fails(t, () => {383 const fnA = a => a;384 const fnB = a => a;385 assertions.deepEqual(fnA, fnB);386 });387 passes(t, () => {388 const x1 = {z: 4};389 const y1 = {x: x1};390 x1.y = y1;391 const x2 = {z: 4};392 const y2 = {x: x2};393 x2.y = y2;394 assertions.deepEqual(x1, x2);395 });396 passes(t, () => {397 function Foo(a) {398 this.a = a;399 }400 const x = new Foo(1);401 const y = new Foo(1);402 assertions.deepEqual(x, y);403 });404 fails(t, () => {405 function Foo(a) {406 this.a = a;407 }408 function Bar(a) {409 this.a = a;410 }411 const x = new Foo(1);412 const y = new Bar(1);413 assertions.deepEqual(x, y);414 });415 fails(t, () => {416 assertions.deepEqual({417 a: 'a',418 b: 'b',419 c: {420 d: false421 }422 }, {423 c: {424 d: 0425 },426 b: 'b',427 a: 'a'428 });429 });430 fails(t, () => {431 assertions.deepEqual({}, []);432 });433 fails(t, () => {434 assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);435 });436 fails(t, () => {437 assertions.deepEqual({a: 1}, {a: 1, b: undefined});438 });439 fails(t, () => {440 assertions.deepEqual(new Date('1972-08-01'), null);441 });442 fails(t, () => {443 assertions.deepEqual(new Date('1972-08-01'), undefined);444 });445 passes(t, () => {446 assertions.deepEqual(new Date('1972-08-01'), new Date('1972-08-01'));447 });448 passes(t, () => {449 assertions.deepEqual({x: new Date('1972-08-01')}, {x: new Date('1972-08-01')});450 });451 fails(t, () => {452 assertions.deepEqual(() => {}, () => {});453 });454 passes(t, () => {455 assertions.deepEqual(undefined, undefined);456 assertions.deepEqual({x: undefined}, {x: undefined});457 assertions.deepEqual({x: [undefined]}, {x: [undefined]});458 });459 passes(t, () => {460 assertions.deepEqual(null, null);461 assertions.deepEqual({x: null}, {x: null});462 assertions.deepEqual({x: [null]}, {x: [null]});463 });464 passes(t, () => {465 assertions.deepEqual(0, 0);466 assertions.deepEqual(1, 1);467 assertions.deepEqual(3.14, 3.14);468 });469 fails(t, () => {470 assertions.deepEqual(0, 1);471 });472 fails(t, () => {473 assertions.deepEqual(1, -1);474 });475 fails(t, () => {476 assertions.deepEqual(3.14, 2.72);477 });478 fails(t, () => {479 assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);480 });481 passes(t, () => {482 assertions.deepEqual(483 [484 {foo: {z: 100, y: 200, x: 300}},485 'bar',486 11,487 {baz: {d: 4, a: 1, b: 2, c: 3}}488 ],489 [490 {foo: {x: 300, y: 200, z: 100}},491 'bar',492 11,493 {baz: {c: 3, b: 2, a: 1, d: 4}}494 ]495 );496 });497 passes(t, () => {498 assertions.deepEqual(499 {x: {a: 1, b: 2}, y: {c: 3, d: 4}},500 {y: {d: 4, c: 3}, x: {b: 2, a: 1}}501 );502 });503 passes(t, () => {504 assertions.deepEqual(505 renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON(),506 React.createElement('div', null, 'Hello ', React.createElement('mark', null, 'Sindre'))507 );508 });509 // Regression test end here510 passes(t, () => {511 assertions.deepEqual({a: 'a'}, {a: 'a'});512 });513 passes(t, () => {514 assertions.deepEqual(['a', 'b'], ['a', 'b']);515 });516 fails(t, () => {517 assertions.deepEqual({a: 'a'}, {a: 'b'});518 });519 fails(t, () => {520 assertions.deepEqual(['a', 'b'], ['a', 'a']);521 });522 fails(t, () => {523 assertions.deepEqual([['a', 'b'], 'c'], [['a', 'b'], 'd']);524 });525 fails(t, () => {526 const circular = ['a', 'b'];527 circular.push(circular);528 assertions.deepEqual([circular, 'c'], [circular, 'd']);529 });530 failsWith(t, () => {531 assertions.deepEqual('foo', 'bar');532 }, {533 assertion: 'deepEqual',534 message: '',535 raw: {actual: 'foo', expected: 'bar'},536 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}]537 });538 failsWith(t, () => {539 assertions.deepEqual('foo', 42);540 }, {541 assertion: 'deepEqual',542 message: '',543 raw: {actual: 'foo', expected: 42},544 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]545 });546 failsWith(t, () => {547 assertions.deepEqual('foo', 42, 'my message');548 }, {549 assertion: 'deepEqual',550 message: 'my message',551 values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]552 });553 t.end();554});555test('.notDeepEqual()', t => {556 passes(t, () => {557 assertions.notDeepEqual({a: 'a'}, {a: 'b'});558 });559 passes(t, () => {560 assertions.notDeepEqual(['a', 'b'], ['c', 'd']);561 });562 const actual = {a: 'a'};563 const expected = {a: 'a'};564 failsWith(t, () => {565 assertions.notDeepEqual(actual, expected);566 }, {567 actual,568 assertion: 'notDeepEqual',569 expected,570 message: '',571 raw: {actual, expected},572 values: [{label: 'Value is deeply equal:', formatted: /.*\{.*\n.*a: 'a'/}]573 });574 failsWith(t, () => {575 assertions.notDeepEqual(['a', 'b'], ['a', 'b'], 'my message');576 }, {577 assertion: 'notDeepEqual',578 message: 'my message',579 values: [{label: 'Value is deeply equal:', formatted: /.*\[.*\n.*'a',\n.*'b',/}]580 });581 t.end();582});583test('.throws()', gather(t => {584 // Fails because function doesn't throw.585 failsWith(t, () => {586 assertions.throws(() => {});587 }, {588 assertion: 'throws',589 message: '',590 values: [{label: 'Function returned:', formatted: /undefined/}]591 });592 // Fails because function doesn't throw. Asserts that 'my message' is used593 // as the assertion message (*not* compared against the error).594 failsWith(t, () => {595 assertions.throws(() => {}, null, 'my message');596 }, {597 assertion: 'throws',598 message: 'my message',599 values: [{label: 'Function returned:', formatted: /undefined/}]600 });601 // Fails because the function returned a promise.602 failsWith(t, () => {603 assertions.throws(() => Promise.resolve());604 }, {605 assertion: 'throws',606 message: '',607 values: [{label: 'Function returned a promise. Use `t.throwsAsync()` instead:', formatted: /Promise/}]608 });609 // Fails because thrown exception is not an error610 failsWith(t, () => {611 assertions.throws(() => {612 const err = 'foo';613 throw err;614 });615 }, {616 assertion: 'throws',617 message: '',618 values: [619 {label: 'Function threw exception that is not an error:', formatted: /'foo'/}620 ]621 });622 // Fails because thrown error's message is not equal to 'bar'623 failsWith(t, () => {624 const err = new Error('foo');625 assertions.throws(() => {626 throw err;627 }, 'bar');628 }, {629 assertion: 'throws',630 message: '',631 values: [632 {label: 'Function threw unexpected exception:', formatted: /foo/},633 {label: 'Expected message to equal:', formatted: /bar/}634 ]635 });636 // Fails because thrown error is not the right instance637 failsWith(t, () => {638 const err = new Error('foo');639 assertions.throws(() => {640 throw err;641 }, class Foo {});642 }, {643 assertion: 'throws',644 message: '',645 values: [646 {label: 'Function threw unexpected exception:', formatted: /foo/},647 {label: 'Expected instance of:', formatted: /Foo/}648 ]649 });650 // Passes because thrown error's message is equal to 'bar'651 passes(t, () => {652 const err = new Error('foo');653 assertions.throws(() => {654 throw err;655 }, 'foo');656 });657 // Passes because an error is thrown.658 passes(t, () => {659 assertions.throws(() => {660 throw new Error('foo');661 });662 });663 // Passes because the correct error is thrown.664 passes(t, () => {665 const err = new Error('foo');666 assertions.throws(() => {667 throw err;668 }, {is: err});669 });670 // Fails because the thrown value is not an error671 fails(t, () => {672 const obj = {};673 assertions.throws(() => {674 throw obj;675 }, {is: obj});676 });677 // Fails because the thrown value is not the right one678 fails(t, () => {679 const err = new Error('foo');680 assertions.throws(() => {681 throw err;682 }, {is: {}});683 });684 // Passes because the correct error is thrown.685 passes(t, () => {686 assertions.throws(() => {687 throw new TypeError(); // eslint-disable-line unicorn/error-message688 }, {name: 'TypeError'});689 });690 // Fails because the thrown value is not an error691 fails(t, () => {692 assertions.throws(() => {693 const err = {name: 'Bob'};694 throw err;695 }, {name: 'Bob'});696 });697 // Fails because the thrown value is not the right one698 fails(t, () => {699 assertions.throws(() => {700 throw new Error('foo');701 }, {name: 'TypeError'});702 });703 // Passes because the correct error is thrown.704 passes(t, () => {705 assertions.throws(() => {706 const err = new TypeError(); // eslint-disable-line unicorn/error-message707 err.code = 'ERR_TEST';708 throw err;709 }, {code: 'ERR_TEST'});710 });711 // Passes because the correct error is thrown.712 passes(t, () => {713 assertions.throws(() => {714 const err = new TypeError(); // eslint-disable-line unicorn/error-message715 err.code = 42;716 throw err;717 }, {code: 42});718 });719 // Fails because the thrown value is not the right one720 fails(t, () => {721 assertions.throws(() => {722 const err = new TypeError(); // eslint-disable-line unicorn/error-message723 err.code = 'ERR_NOPE';724 throw err;725 }, {code: 'ERR_TEST'});726 });727}));728test('.throws() returns the thrown error', t => {729 const expected = new Error();730 const actual = assertions.throws(() => {731 throw expected;732 });733 t.is(actual, expected);734 t.end();735});736test('.throwsAsync()', gather(t => {737 // Fails because the promise is resolved, not rejected.738 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve('foo')), {739 assertion: 'throwsAsync',740 message: '',741 values: [{label: 'Promise resolved with:', formatted: /'foo'/}]742 });743 // Fails because the promise is resolved with an Error744 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.resolve(new Error())), {745 assertion: 'throwsAsync',746 message: '',747 values: [{label: 'Promise resolved with:', formatted: /Error/}]748 });749 // Fails because the function returned a promise that resolved, not rejected.750 eventuallyFailsWith(t, () => assertions.throwsAsync(() => Promise.resolve('foo')), {751 assertion: 'throwsAsync',752 message: '',753 values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}]754 });755 // Passes because the promise was rejected with an error.756 eventuallyPasses(t, () => assertions.throwsAsync(Promise.reject(new Error())));757 // Passes because the function returned a promise rejected with an error.758 eventuallyPasses(t, () => assertions.throwsAsync(() => Promise.reject(new Error())));759 // Passes because the error's message matches the regex760 eventuallyPasses(t, () => assertions.throwsAsync(Promise.reject(new Error('abc')), /abc/));761 // Fails because the error's message does not match the regex762 eventuallyFailsWith(t, () => assertions.throwsAsync(Promise.reject(new Error('abc')), /def/), {763 assertion: 'throwsAsync',764 message: '',765 values: [766 {label: 'Promise rejected with unexpected exception:', formatted: /Error/},767 {label: 'Expected message to match:', formatted: /\/def\//}768 ]769 });770 // Fails because the function throws synchronously771 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {772 throw new Error('sync');773 }, null, 'message'), {774 assertion: 'throwsAsync',775 message: 'message',776 values: [777 {label: 'Function threw synchronously. Use `t.throws()` instead:', formatted: /Error/}778 ]779 });780 // Fails because the function did not return a promise781 eventuallyFailsWith(t, () => assertions.throwsAsync(() => {}, null, 'message'), {782 assertion: 'throwsAsync',783 message: 'message',784 values: [785 {label: 'Function returned:', formatted: /undefined/}786 ]787 });788}));789test('.throwsAsync() returns the rejection reason of promise', t => {790 const expected = new Error();791 return assertions.throwsAsync(Promise.reject(expected)).then(actual => {792 t.is(actual, expected);793 t.end();794 });795});796test('.throwsAsync() returns the rejection reason of a promise returned by the function', t => {797 const expected = new Error();798 return assertions.throwsAsync(() => {799 return Promise.reject(expected);800 }).then(actual => {801 t.is(actual, expected);802 t.end();803 });804});805test('.throws() fails if passed a bad value', t => {806 failsWith(t, () => {807 assertions.throws('not a function');808 }, {809 assertion: 'throws',810 message: '`t.throws()` must be called with a function',811 values: [{label: 'Called with:', formatted: /not a function/}]812 });813 t.end();814});815test('.throwsAsync() fails if passed a bad value', t => {816 failsWith(t, () => {817 assertions.throwsAsync('not a function');818 }, {819 assertion: 'throwsAsync',820 message: '`t.throwsAsync()` must be called with a function or promise',821 values: [{label: 'Called with:', formatted: /not a function/}]822 });823 t.end();824});825test('.throws() fails if passed a bad expectation', t => {826 failsWith(t, () => {827 assertions.throws(() => {}, true);828 }, {829 assertion: 'throws',830 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',831 values: [{label: 'Called with:', formatted: /true/}]832 });833 failsWith(t, () => {834 assertions.throws(() => {}, {});835 }, {836 assertion: 'throws',837 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',838 values: [{label: 'Called with:', formatted: /\{\}/}]839 });840 failsWith(t, () => {841 assertions.throws(() => {}, []);842 }, {843 assertion: 'throws',844 message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',845 values: [{label: 'Called with:', formatted: /\[\]/}]846 });847 failsWith(t, () => {848 assertions.throws(() => {}, {code: {}});849 }, {850 assertion: 'throws',851 message: 'The `code` property of the second argument to `t.throws()` must be a string or number',852 values: [{label: 'Called with:', formatted: /code: {}/}]853 });854 failsWith(t, () => {855 assertions.throws(() => {}, {instanceOf: null});856 }, {857 assertion: 'throws',858 message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',859 values: [{label: 'Called with:', formatted: /instanceOf: null/}]860 });861 failsWith(t, () => {862 assertions.throws(() => {}, {message: null});863 }, {864 assertion: 'throws',865 message: 'The `message` property of the second argument to `t.throws()` must be a string or regular expression',866 values: [{label: 'Called with:', formatted: /message: null/}]867 });868 failsWith(t, () => {869 assertions.throws(() => {}, {name: null});870 }, {871 assertion: 'throws',872 message: 'The `name` property of the second argument to `t.throws()` must be a string',873 values: [{label: 'Called with:', formatted: /name: null/}]874 });875 failsWith(t, () => {876 assertions.throws(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});877 }, {878 assertion: 'throws',879 message: 'The second argument to `t.throws()` contains unexpected properties',880 values: [{label: 'Called with:', formatted: /foo: null/}]881 });882 t.end();883});884test('.throwsAsync() fails if passed a bad expectation', t => {885 failsWith(t, () => {886 assertions.throwsAsync(() => {}, true);887 }, {888 assertion: 'throwsAsync',889 message: 'The second argument to `t.throwsAsync()` must be a function, string, regular expression, expectation object or `null`',890 values: [{label: 'Called with:', formatted: /true/}]891 });892 failsWith(t, () => {893 assertions.throwsAsync(() => {}, {});894 }, {895 assertion: 'throwsAsync',896 message: 'The second argument to `t.throwsAsync()` must be a function, string, regular expression, expectation object or `null`',897 values: [{label: 'Called with:', formatted: /\{\}/}]898 });899 failsWith(t, () => {900 assertions.throwsAsync(() => {}, []);901 }, {902 assertion: 'throwsAsync',903 message: 'The second argument to `t.throwsAsync()` must be a function, string, regular expression, expectation object or `null`',904 values: [{label: 'Called with:', formatted: /\[\]/}]905 });906 failsWith(t, () => {907 assertions.throwsAsync(() => {}, {code: {}});908 }, {909 assertion: 'throwsAsync',910 message: 'The `code` property of the second argument to `t.throwsAsync()` must be a string or number',911 values: [{label: 'Called with:', formatted: /code: {}/}]912 });913 failsWith(t, () => {914 assertions.throwsAsync(() => {}, {instanceOf: null});915 }, {916 assertion: 'throwsAsync',917 message: 'The `instanceOf` property of the second argument to `t.throwsAsync()` must be a function',918 values: [{label: 'Called with:', formatted: /instanceOf: null/}]919 });920 failsWith(t, () => {921 assertions.throwsAsync(() => {}, {message: null});922 }, {923 assertion: 'throwsAsync',924 message: 'The `message` property of the second argument to `t.throwsAsync()` must be a string or regular expression',925 values: [{label: 'Called with:', formatted: /message: null/}]926 });927 failsWith(t, () => {928 assertions.throwsAsync(() => {}, {name: null});929 }, {930 assertion: 'throwsAsync',931 message: 'The `name` property of the second argument to `t.throwsAsync()` must be a string',932 values: [{label: 'Called with:', formatted: /name: null/}]933 });934 failsWith(t, () => {935 assertions.throwsAsync(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});936 }, {937 assertion: 'throwsAsync',938 message: 'The second argument to `t.throwsAsync()` contains unexpected properties',939 values: [{label: 'Called with:', formatted: /foo: null/}]940 });941 t.end();942});943test('.notThrows()', gather(t => {944 // Passes because the function doesn't throw945 passes(t, () => {946 assertions.notThrows(() => {});947 });948 // Fails because the function throws.949 failsWith(t, () => {950 assertions.notThrows(() => {951 throw new Error('foo');952 });953 }, {954 assertion: 'notThrows',955 message: '',956 values: [{label: 'Function threw:', formatted: /foo/}]957 });958 // Fails because the function throws. Asserts that message is used for the959 // assertion, not to validate the thrown error.960 failsWith(t, () => {961 assertions.notThrows(() => {962 throw new Error('foo');963 }, 'my message');964 }, {965 assertion: 'notThrows',966 message: 'my message',967 values: [{label: 'Function threw:', formatted: /foo/}]968 });969}));970test('.notThrowsAsync()', gather(t => {971 // Passes because the promise is resolved972 eventuallyPasses(t, () => assertions.notThrowsAsync(Promise.resolve()));973 // Fails because the promise is rejected974 eventuallyFailsWith(t, () => assertions.notThrowsAsync(Promise.reject(new Error())), {975 assertion: 'notThrowsAsync',976 message: '',977 values: [{label: 'Promise rejected with:', formatted: /Error/}]978 });979 // Passes because the function returned a resolved promise980 eventuallyPasses(t, () => assertions.notThrowsAsync(() => Promise.resolve()));981 // Fails because the function returned a rejected promise982 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => Promise.reject(new Error())), {983 assertion: 'notThrowsAsync',984 message: '',985 values: [{label: 'Returned promise rejected with:', formatted: /Error/}]986 });987 // Fails because the function throws synchronously988 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => {989 throw new Error('sync');990 }, 'message'), {991 assertion: 'notThrowsAsync',992 message: 'message',993 values: [994 {label: 'Function threw:', formatted: /Error/}995 ]996 });997 // Fails because the function did not return a promise998 eventuallyFailsWith(t, () => assertions.notThrowsAsync(() => {}, 'message'), {999 assertion: 'notThrowsAsync',1000 message: 'message',1001 values: [1002 {label: 'Function did not return a promise. Use `t.notThrows()` instead:', formatted: /undefined/}1003 ]1004 });1005}));1006test('.notThrowsAsync() returns undefined for a fulfilled promise', t => {1007 return assertions.notThrowsAsync(Promise.resolve(Symbol(''))).then(actual => {1008 t.is(actual, undefined);1009 });1010});1011test('.notThrowsAsync() returns undefined for a fulfilled promise returned by the function', t => {1012 return assertions.notThrowsAsync(() => {1013 return Promise.resolve(Symbol(''));1014 }).then(actual => {1015 t.is(actual, undefined);1016 });1017});1018test('.notThrows() fails if passed a bad value', t => {1019 failsWith(t, () => {1020 assertions.notThrows('not a function');1021 }, {1022 assertion: 'notThrows',1023 message: '`t.notThrows()` must be called with a function',1024 values: [{label: 'Called with:', formatted: /not a function/}]1025 });1026 t.end();1027});1028test('.notThrowsAsync() fails if passed a bad value', t => {1029 failsWith(t, () => {1030 assertions.notThrowsAsync('not a function');1031 }, {1032 assertion: 'notThrowsAsync',1033 message: '`t.notThrowsAsync()` must be called with a function or promise',1034 values: [{label: 'Called with:', formatted: /not a function/}]1035 });1036 t.end();1037});1038test('.snapshot()', t => {1039 // Set to `true` to update the snapshot, then run:1040 // "$(npm bin)"/tap --no-cov -R spec test/assert.js1041 //1042 // Ignore errors and make sure not to run tests with the `-b` (bail) option.1043 const updating = false;1044 const projectDir = path.join(__dirname, 'fixture');1045 const manager = snapshotManager.load({1046 file: path.join(projectDir, 'assert.js'),1047 projectDir,1048 fixedLocation: null,1049 updating1050 });1051 const setup = title => {1052 return new Test({1053 title,1054 compareTestSnapshot: options => manager.compare(options)1055 });1056 };1057 passes(t, () => {1058 const testInstance = setup('passes');1059 assertions.snapshot.call(testInstance, {foo: 'bar'});1060 assertions.snapshot.call(testInstance, {foo: 'bar'}, {id: 'fixed id'}, 'message not included in snapshot report');1061 assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Sindre'}));1062 assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());1063 });1064 {1065 const testInstance = setup('fails');1066 if (updating) {1067 assertions.snapshot.call(testInstance, {foo: 'bar'});1068 } else {1069 failsWith(t, () => {1070 assertions.snapshot.call(testInstance, {foo: 'not bar'});1071 }, {1072 assertion: 'snapshot',1073 message: 'Did not match snapshot',1074 values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]1075 });1076 }1077 }1078 failsWith(t, () => {1079 const testInstance = setup('fails (fixed id)');1080 assertions.snapshot.call(testInstance, {foo: 'not bar'}, {id: 'fixed id'}, 'different message, also not included in snapshot report');1081 }, {1082 assertion: 'snapshot',1083 message: 'different message, also not included in snapshot report',1084 values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]1085 });1086 {1087 const testInstance = setup('fails');1088 if (updating) {1089 assertions.snapshot.call(testInstance, {foo: 'bar'}, 'my message');1090 } else {1091 failsWith(t, () => {1092 assertions.snapshot.call(testInstance, {foo: 'not bar'}, 'my message');1093 }, {1094 assertion: 'snapshot',1095 message: 'my message',1096 values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]1097 });1098 }1099 }1100 {1101 const testInstance = setup('rendered comparison');1102 if (updating) {1103 assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());1104 } else {1105 passes(t, () => {1106 assertions.snapshot.call(testInstance, React.createElement('div', null, 'Hello ', React.createElement('mark', null, 'Sindre')));1107 });1108 }1109 }1110 {1111 const testInstance = setup('rendered comparison');1112 if (updating) {1113 assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());1114 } else {1115 failsWith(t, () => {1116 assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Vadim'})).toJSON());1117 }, {1118 assertion: 'snapshot',1119 message: 'Did not match snapshot',1120 values: [{label: 'Difference:', formatted: ' <div>\n Hello \n <mark>\n- Vadim\n+ Sindre\n </mark>\n </div>'}]1121 });1122 }1123 }1124 {1125 const testInstance = setup('element comparison');1126 if (updating) {1127 assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Sindre'}));1128 } else {1129 failsWith(t, () => {1130 assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Vadim'}));1131 }, {1132 assertion: 'snapshot',1133 message: 'Did not match snapshot',1134 values: [{label: 'Difference:', formatted: ' <HelloMessageâ\n- name="Vadim"\n+ name="Sindre"\n />'}]1135 });1136 }1137 }1138 manager.save();1139 t.end();1140});1141test('.truthy()', t => {1142 failsWith(t, () => {1143 assertions.truthy(0);1144 }, {1145 assertion: 'truthy',1146 message: '',1147 operator: '!!',1148 values: [{label: 'Value is not truthy:', formatted: /0/}]1149 });1150 failsWith(t, () => {1151 assertions.truthy(false, 'my message');1152 }, {1153 assertion: 'truthy',1154 message: 'my message',1155 operator: '!!',1156 values: [{label: 'Value is not truthy:', formatted: /false/}]1157 });1158 passes(t, () => {1159 assertions.truthy(1);1160 assertions.truthy(true);1161 });1162 t.end();1163});1164test('.falsy()', t => {1165 failsWith(t, () => {1166 assertions.falsy(1);1167 }, {1168 assertion: 'falsy',1169 message: '',1170 operator: '!',1171 values: [{label: 'Value is not falsy:', formatted: /1/}]1172 });1173 failsWith(t, () => {1174 assertions.falsy(true, 'my message');1175 }, {1176 assertion: 'falsy',1177 message: 'my message',1178 operator: '!',1179 values: [{label: 'Value is not falsy:', formatted: /true/}]1180 });1181 passes(t, () => {1182 assertions.falsy(0);1183 assertions.falsy(false);1184 });1185 t.end();1186});1187test('.true()', t => {1188 failsWith(t, () => {1189 assertions.true(1);1190 }, {1191 assertion: 'true',1192 message: '',1193 values: [{label: 'Value is not `true`:', formatted: /1/}]1194 });1195 failsWith(t, () => {1196 assertions.true(0);1197 }, {1198 assertion: 'true',1199 message: '',1200 values: [{label: 'Value is not `true`:', formatted: /0/}]1201 });1202 failsWith(t, () => {1203 assertions.true(false);1204 }, {1205 assertion: 'true',1206 message: '',1207 values: [{label: 'Value is not `true`:', formatted: /false/}]1208 });1209 failsWith(t, () => {1210 assertions.true('foo', 'my message');1211 }, {1212 assertion: 'true',1213 message: 'my message',1214 values: [{label: 'Value is not `true`:', formatted: /foo/}]1215 });1216 passes(t, () => {1217 assertions.true(true);1218 });1219 t.end();1220});1221test('.false()', t => {1222 failsWith(t, () => {1223 assertions.false(0);1224 }, {1225 assertion: 'false',1226 message: '',1227 values: [{label: 'Value is not `false`:', formatted: /0/}]1228 });1229 failsWith(t, () => {1230 assertions.false(1);1231 }, {1232 assertion: 'false',1233 message: '',1234 values: [{label: 'Value is not `false`:', formatted: /1/}]1235 });1236 failsWith(t, () => {1237 assertions.false(true);1238 }, {1239 assertion: 'false',1240 message: '',1241 values: [{label: 'Value is not `false`:', formatted: /true/}]1242 });1243 failsWith(t, () => {1244 assertions.false('foo', 'my message');1245 }, {1246 assertion: 'false',1247 message: 'my message',1248 values: [{label: 'Value is not `false`:', formatted: /foo/}]1249 });1250 passes(t, () => {1251 assertions.false(false);1252 });1253 t.end();1254});1255test('.regex()', t => {1256 passes(t, () => {1257 assertions.regex('abc', /^abc$/);1258 });1259 failsWith(t, () => {1260 assertions.regex('foo', /^abc$/);1261 }, {1262 assertion: 'regex',1263 message: '',1264 values: [1265 {label: 'Value must match expression:', formatted: /foo/},1266 {label: 'Regular expression:', formatted: /\/\^abc\$\//}1267 ]1268 });1269 failsWith(t, () => {1270 assertions.regex('foo', /^abc$/, 'my message');1271 }, {1272 assertion: 'regex',1273 message: 'my message',1274 values: [1275 {label: 'Value must match expression:', formatted: /foo/},1276 {label: 'Regular expression:', formatted: /\/\^abc\$\//}1277 ]1278 });1279 t.end();1280});1281test('.regex() fails if passed a bad value', t => {1282 failsWith(t, () => {1283 assertions.regex(42, /foo/);1284 }, {1285 assertion: 'regex',1286 message: '`t.regex()` must be called with a string',1287 values: [{label: 'Called with:', formatted: /42/}]1288 });1289 failsWith(t, () => {1290 assertions.regex('42', {});1291 }, {1292 assertion: 'regex',1293 message: '`t.regex()` must be called with a regular expression',1294 values: [{label: 'Called with:', formatted: /\{\}/}]1295 });1296 t.end();1297});1298test('.notRegex()', t => {1299 passes(t, () => {1300 assertions.notRegex('abc', /def/);1301 });1302 failsWith(t, () => {1303 assertions.notRegex('abc', /abc/);1304 }, {1305 assertion: 'notRegex',1306 message: '',1307 values: [1308 {label: 'Value must not match expression:', formatted: /abc/},1309 {label: 'Regular expression:', formatted: /\/abc\//}1310 ]1311 });1312 failsWith(t, () => {1313 assertions.notRegex('abc', /abc/, 'my message');1314 }, {1315 assertion: 'notRegex',1316 message: 'my message',1317 values: [1318 {label: 'Value must not match expression:', formatted: /abc/},1319 {label: 'Regular expression:', formatted: /\/abc\//}1320 ]1321 });1322 t.end();1323});1324test('.notRegex() fails if passed a bad value', t => {1325 failsWith(t, () => {1326 assertions.notRegex(42, /foo/);1327 }, {1328 assertion: 'notRegex',1329 message: '`t.notRegex()` must be called with a string',1330 values: [{label: 'Called with:', formatted: /42/}]1331 });1332 failsWith(t, () => {1333 assertions.notRegex('42', {});1334 }, {1335 assertion: 'notRegex',1336 message: '`t.notRegex()` must be called with a regular expression',1337 values: [{label: 'Called with:', formatted: /\{\}/}]1338 });1339 t.end();...
test-renderer-jest.js
Source:test-renderer-jest.js
1import types from './types/types';2import * as testRendererAssertions from './assertions/testRendererAssertions';3import * as testRendererAgainstRawAssertions from './assertions/testRendererAgainstRawAssertions';4import * as jestSnapshotAssertions from './assertions/jestSnapshotTestRendererAssertions';5import * as snapshotFunctionType from './assertions/snapshotFunctionType';6import * as snapshotFunctionAssertions from './assertions/snapshotFunctionAssertions';7module.exports = {8 name: 'unexpected-react-test-renderer',9 10 installInto(expect) {11 12 expect.installPlugin(require('magicpen-prism'));13 14 types.installInto(expect);15 16 // This is a bit of a hack. The AssertionGenerator generates a type for context to add the pending event,17 // and this type must be re-used when adding further assertions with a different expected type18 // - in this case that's the RawAdapter type rather than the ReactElement type.19 20 // It works, but it's ugly. It would be nicer to split the AssertionGenerator out further21 // such that this could be less ugly - not sure how that would look though.22 23 // This /may/ be solved by the upcoming (possibly already existing!) expect.context interface.24 // When that's available, we won't need the intermediate type for pending events, and we can just25 // add the pending event to the context and have the main assertions handle it.26 27 // But we can't rely on that yet, I don't think28 29 const mainAssertionGenerator = testRendererAssertions.installInto(expect);30 testRendererAgainstRawAssertions.installAsAlternative(expect, mainAssertionGenerator);31 32 jestSnapshotAssertions.installInto(expect);33 snapshotFunctionType.installInto(expect);34 snapshotFunctionAssertions.installInto(expect);35 expect.addAssertion('<RawReactTestRendererJson> to match snapshot', function (expect, subject) {36 expect.errorMode = 'bubble';37 expect.fail({38 message: function (output) {39 return output.text('To assert snapshots, use the testRenderer directly, not the result of `.toJSON()`')40 .nl().i()41 .text('e.g.')42 .nl().i()43 .text(' const testRenderer = ReactTestRenderer.create(<MyComponent />);').nl().i()44 .text(' expect(testRenderer, \'to match snapshot\');');45 }46 });47 });48 },49 50 clearAll() {51 // No-op. Left in so that tests can easily use either interface52 }...
jest.js
Source:jest.js
1import RenderHook from 'react-render-hook';2import types from './types/types';3import domTypes from './types/dom-types';4import * as deepAssertions from './assertions/deepAssertions';5import * as deepAgainstRawAssertions from './assertions/deepAgainstRawAssertions';6import * as shallowAssertions from './assertions/shallowAssertions';7import * as shallowAgainstRawAssertions from './assertions/shallowAgainstRawAssertions';8import * as jestSnapshotStandardRendererAssertions from './assertions/jestSnapshotStandardRendererAssertions';9import * as snapshotFunctionType from './assertions/snapshotFunctionType';10import * as snapshotFunctionAssertions from './assertions/snapshotFunctionAssertions';11module.exports = {12 name: 'unexpected-react',13 14 installInto(expect) {15 16 expect.installPlugin(require('magicpen-prism'));17 18 types.installInto(expect);19 domTypes.installInto(expect);20 const mainAssertionGenerator = shallowAssertions.installInto(expect);21 shallowAgainstRawAssertions.installAsAlternative(expect, mainAssertionGenerator);22 23 const deepMainAssertionGenerator = deepAssertions.installInto(expect);24 deepAgainstRawAssertions.installAsAlternative(expect, deepMainAssertionGenerator);25 jestSnapshotStandardRendererAssertions.installInto(expect);26 snapshotFunctionType.installInto(expect);27 snapshotFunctionAssertions.installInto(expect);28 },29 30 clearAll() {31 RenderHook.clearAll();32 }...
Using AI Code Generation
1import test from 'ava';2import { JSDOM } from 'jsdom';3import fs from 'fs';4test('my snapshot', t => {5 const html = fs.readFileSync('./index.html', 'utf8');6 const dom = new JSDOM(html);7 t.snapshot(dom.window.document.body.innerHTML);8});9{10 "scripts": {11 },12 "ava": {13 }14}15{16 "scripts": {17 },18 "ava": {19 }20}21{22 "scripts": {23 },24 "ava": {25 }26}27{28 "scripts": {29 },30 "ava": {31 }32}33{34 "scripts": {35 },36 "ava": {37 }38}39{40 "scripts": {41 },42 "ava": {43 }44}45{46 "scripts": {47 },48 "ava": {49 }50}51{52 "scripts": {53 },54 "ava": {55 }56}
Using AI Code Generation
1import test from 'ava';2import {readFileSync} from 'fs';3import {join} from 'path';4import {JSDOM} from 'jsdom';5import {createStore} from 'redux';6import {Provider} from 'react-redux';7import {renderToString} from 'react-dom/server';8import React from 'react';9import {reducer} from '../src/reducers/index';10import {App} from '../src/components/App';11test('App snapshot', t => {12 const store = createStore(reducer);13 const html = renderToString(14 <Provider store={store}>15 );16 const dom = new JSDOM(html);17 t.snapshot(dom.window.document.body.innerHTML);18});19test('App snapshot with data', t => {20 const store = createStore(reducer);21 store.dispatch({22 payload: {23 }24 });25 const html = renderToString(26 <Provider store={store}>27 );28 const dom = new JSDOM(html);29 t.snapshot(dom.window.document.body.innerHTML);30});31test('App snapshot with data and total', t => {32 const store = createStore(reducer);33 store.dispatch({34 payload: {35 }36 });37 store.dispatch({38 payload: {39 }40 });41 const html = renderToString(42 <Provider store={store}>43 );44 const dom = new JSDOM(html);45 t.snapshot(dom.window.document.body.innerHTML);46});47test('App snapshot with data and total and updated data', t => {48 const store = createStore(reducer);49 store.dispatch({50 payload: {51 }52 });53 store.dispatch({54 payload: {
Using AI Code Generation
1const test = require('ava')2const { snapshot } = require('ava/lib/concordance-options')3test('my passing test', t => {4 t.snapshot({ hello: 'world' }, snapshot())5})6const test = require('ava')7const { snapshot } = require('ava/lib/concordance-options')8test('my passing test', t => {9 t.snapshot({ hello: 'world' }, snapshot())10})11const test = require('ava')12const { snapshot } = require('ava/lib/concordance-options')13test('my passing test', t => {14 t.snapshot({ hello: 'world' }, snapshot())15})16const test = require('ava')17const { snapshot } = require('ava/lib/concordance-options')18test('my passing test', t => {19 t.snapshot({ hello: 'world' }, snapshot())20})21const test = require('ava')22const { snapshot } = require('ava/lib/concordance-options')23test('my passing test', t => {24 t.snapshot({ hello: 'world' }, snapshot())25})26const test = require('ava')27const { snapshot } = require('ava/lib/concordance-options')28test('my passing test', t => {29 t.snapshot({ hello: 'world' }, snapshot())30})31const test = require('ava')32const { snapshot } = require('ava/lib/concordance-options')33test('my passing test', t => {34 t.snapshot({ hello: 'world' }, snapshot())35})36const test = require('ava')37const { snapshot } = require('ava/lib/concordance-options')38test('my passing test', t => {39 t.snapshot({ hello: 'world' }, snapshot())40})41const test = require('ava')42const { snapshot } = require('ava/lib/concordance-options')43test('my passing test
Using AI Code Generation
1const test = require('ava');2const assert = require('assert');3const snapshot = require('snap-shot-it');4const { getSnapshotPath } = require('snap-shot-it');5const path = require('path');6test('snapshot', t => {7 const snapshotPath = getSnapshotPath(t);8 const expectedSnapshotPath = path.join(__dirname, '__snapshots__', 'test.js.snap');9 assert.strictEqual(snapshotPath, expectedSnapshotPath);10 snapshot({ foo: 'bar' });11});12Object {13}14`;15const test = require('ava');16const assert = require('assert');17const { getSnapshotPath } = require('snap-shot-it');18const path = require('path');19test('snapshot', t => {20 const snapshotPath = getSnapshotPath(t);21 const expectedSnapshotPath = path.join(__dirname, '__snapshots__', 'test.js.snap');22 assert.strictEqual(snapshotPath, expectedSnapshotPath);23 t.snapshot({ foo: 'bar' });24});25Object {26}27`;28const { getSnapshotPath } = require('snap-shot-it');29const path = require('path');30test('snapshot', () => {31 const snapshotPath = getSnapshotPath();32 const expectedSnapshotPath = path.join(__dirname, '__snapshots__', 'test.js.snap');33 expect(snapshotPath).toBe(expectedSnapshotPath);34 expect({ foo: 'bar' }).toMatchSnapshot();35});36Object {37}38`;39const assert = require('assert');40const { getSnapshotPath } = require('snap-shot-it');41const path = require('path');42it('snapshot', () => {43 const snapshotPath = getSnapshotPath();44 const expectedSnapshotPath = path.join(__dirname, '__snapshots__', 'test.js.snap');45 assert.strictEqual(snapshotPath, expectedSnapshotPath);46 assert.deepEqual({ foo: 'bar' }, snapshot());47});
Using AI Code Generation
1const test = require('ava');2const snapshot = require('snap-shot-it');3const { getSnapshot } = require('snap-shot-it');4test('snapshot', t => {5 const object = {6 };7 snapshot(object);8 t.pass();9});10test('snapshot with custom name', t => {11 const object = {12 };13 snapshot(object, 'custom name');14 t.pass();15});16test('snapshot with custom options', t => {17 const object = {18 };19 snapshot(object, {20 });21 t.pass();22});23test('snapshot with custom options', t => {24 const object = {25 };26 snapshot(object, {27 });28 t.pass();29});30test('snapshot with custom options', t => {31 const object = {32 };33 snapshot(object, {34 });35 t.pass();36});37test('snapshot with custom options', t => {38 const object = {39 };40 snapshot(object, {41 });42 t.pass();43});44test('snapshot with custom options', t => {45 const object = {46 };47 snapshot(object, {48 });49 t.pass();50});51test('snapshot with custom options', t => {52 const object = {53 };54 snapshot(object, {55 });56 t.pass();57});58test('snapshot with custom options', t => {59 const object = {60 };61 snapshot(object, {62 });63 t.pass();64});65test('snapshot with custom options', t => {66 const object = {67 };68 snapshot(object, {69 });70 t.pass();71});72test('snapshot with custom options', t => {73 const object = {74 };75 snapshot(object, {
Using AI Code Generation
1const test = require('ava');2const { snapshot } = require('ava');3const { get } = require('request-promise');4const { JSDOM } = require('jsdom');5const { window } = new JSDOM('');6const { document } = (new JSDOM('')).window;7global.document = document;8const $ = jQuery = require('jquery')(window);9const { getProducts } = require('../js/getProducts.js');10const { getProductsByCategory } = require('../js/getProducts.js');11const { getProductsBySearch } = require('../js/getProducts.js');12const { getProductsByPrice } = require('../js/getProducts.js');13const { getProductsByPriceAndCategory } = require('../js/getProducts.js');14const { getProductsByPriceAndSearch } = require('../js/getProducts.js');15const { getProductsByCategoryAndSearch } = require('../js/getProducts.js');16const { getProductsByPriceAndCategoryAndSearch } = require('../js/getProducts.js');17const { sortProducts } = require('../js/getProducts.js');18const { getProductsByPriceAndCategoryAndSearchAndSort } = require('../js/getProducts.js');19test('getProducts should return a list of products', async t => {20 const response = await getProducts();21 t.is(response.length, 10);22});23test('getProductsByCategory should return a list of products with the given category', async t => {24 const response = await getProductsByCategory('books');25 t.is(response.length, 2);26});27test('getProductsBySearch should return a list of products with the given search term', async t => {28 const response = await getProductsBySearch('book');29 t.is(response.length, 2);30});31test('getProductsByPrice should return a list of products with the given price', async t => {32 const response = await getProductsByPrice(10, 20);33 t.is(response.length, 1);34});35test('getProductsByPriceAndCategory should return a list of products with the given price and category', async t => {36 const response = await getProductsByPriceAndCategory(10, 20, 'books');
Using AI Code Generation
1import test from 'ava';2import {snapshot} from 'ava/lib/assert';3import {getRandomColor} from './getRandomColor.js';4test('getRandomColor', t => {5 const actual = getRandomColor();6 snapshot(t, actual);7});8export const getRandomColor = () => {9 const colors = ['red', 'green', 'blue', 'yellow'];10 const randomColor = colors[Math.floor(Math.random() * colors.length)];11 return randomColor;12};13import test from 'ava';14import {snapshot} from 'ava/lib/assert';15import {getRandomColor} from './getRandomColor.js';16test('getRandomColor', t => {17 const actual = getRandomColor();18 snapshot(t, actual);19});20export const getRandomColor = () => {21 const colors = ['red', 'green', 'blue', 'yellow'];22 const randomColor = colors[Math.floor(Math.random() * colors.length)];23 return randomColor;24};25import test from 'ava';26import {snapshot} from 'ava/lib/assert';27import {getRandomColor} from './getRandomColor.js';28test('getRandomColor', t => {29 const actual = getRandomColor();30 snapshot(t, actual);31});32export const getRandomColor = () => {33 const colors = ['red', 'green', 'blue', 'yellow'];34 const randomColor = colors[Math.floor(Math.random() * colors.length)];35 return randomColor;36};37import test from 'ava';38import {snapshot} from 'ava/lib/assert';39import {getRandomColor} from './getRandomColor.js';40test('getRandomColor
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!!