How to use createTopLevelExpect method in unexpected

Best JavaScript code snippet using unexpected

createTopLevelExpect.js

Source:createTopLevelExpect.js Github

copy

Full Screen

...1418 const clonedAssertions = {};1419 Object.keys(this.assertions).forEach(function (assertion) {1420 clonedAssertions[assertion] = [].concat(this.assertions[assertion]);1421 }, this);1422 const expect = createTopLevelExpect({1423 assertions: clonedAssertions,1424 types: [].concat(this.types),1425 typeByName: extend({}, this.typeByName),1426 output: this.output.clone(),1427 format: this.outputFormat(),1428 installedPlugins: [].concat(this.installedPlugins),1429 });1430 // Install the hooks:1431 this.installedHooks.forEach((hook) => {1432 expect.hook(hook);1433 });1434 // expect._expect = this._expect;1435 // Make sure that changes to the parent's preferredWidth doesn't propagate:1436 expect.output.preferredWidth = this.output.preferredWidth;1437 return expect;1438};1439expectPrototype.child = function () {1440 this._assertTopLevelExpect();1441 const childExpect = createTopLevelExpect(1442 {1443 assertions: {},1444 types: [],1445 typeByName: {},1446 output: this.output.clone(),1447 format: this.outputFormat(),1448 installedPlugins: [],1449 },1450 this1451 );1452 childExpect.exportAssertion = function (testDescription, handler) {1453 childExpect.parent.addAssertion(testDescription, handler, childExpect);1454 return this;1455 };1456 childExpect.exportType = function (type) {1457 if (childExpect.getType(type.name) !== type) {1458 childExpect.addType(type);1459 }1460 childExpect.parent.addType(type, childExpect);1461 return this;1462 };1463 childExpect.exportStyle = function (name, handler, allowRedefinition) {1464 childExpect.parent.addStyle(1465 name,1466 function (...args) {1467 const childOutput = childExpect.createOutput(this.format);1468 this.append(handler.call(childOutput, ...args) || childOutput);1469 },1470 allowRedefinition1471 );1472 return this;1473 };1474 return childExpect;1475};1476expectPrototype.freeze = function () {1477 this._assertTopLevelExpect();1478 this._frozen = true;1479 return this;1480};1481expectPrototype.outputFormat = function (format) {1482 if (typeof format === 'undefined') {1483 return this._outputFormat;1484 } else {1485 this._assertTopLevelExpect();1486 this._outputFormat = format;1487 return this;1488 }1489};1490expectPrototype.createOutput = function (format) {1491 const that = this;1492 const output = this.output.clone(format || 'text');1493 output.addStyle('appendInspected', function (value, depth) {1494 this.append(that.inspect(value, depth, this.clone()));1495 });1496 return output;1497};1498expectPrototype.hook = function (fn) {1499 this._assertTopLevelExpect();1500 if (this._frozen) {1501 throw new Error(1502 'Cannot install a hook into a frozen instance, please run .clone() first'1503 );1504 }1505 this.installedHooks.push(fn);1506 this._expect = fn(this._expect.bind(this));1507};1508// This is not super elegant, but wrappedExpect.fail was different:1509expectPrototype.fail = function (...args) {1510 // Cannot use this !== this._topLevelExpect due to https://github.com/unexpectedjs/unexpected/issues/6311511 if (this.flags) {1512 this._callInNestedContext(() => {1513 this._topLevelExpect._fail(...args);1514 });1515 } else {1516 try {1517 this._fail(...args);1518 } catch (e) {1519 if (e && e._isUnexpected) {1520 this.setErrorMessage(e);1521 }1522 throw e;1523 }1524 }1525};1526function lookupAssertionsInParentChain(assertionString, expect) {1527 const assertions = [];1528 for (let instance = expect; instance; instance = instance.parent) {1529 if (instance.assertions[assertionString]) {1530 assertions.push(...instance.assertions[assertionString]);1531 }1532 }1533 return assertions;1534}1535function findSuffixAssertions(assertionString, expect) {1536 if (typeof assertionString !== 'string') {1537 return null;1538 }1539 const straightforwardAssertions = lookupAssertionsInParentChain(1540 assertionString,1541 expect1542 );1543 if (straightforwardAssertions.length > 0) {1544 return straightforwardAssertions;1545 }1546 const tokens = assertionString.split(' ');1547 for (let n = tokens.length - 1; n > 0; n -= 1) {1548 const suffix = tokens.slice(n).join(' ');1549 const suffixAssertions = lookupAssertionsInParentChain(suffix, expect);1550 if (1551 findSuffixAssertions(tokens.slice(0, n).join(' '), expect) &&1552 suffixAssertions.length > 01553 ) {1554 return suffixAssertions;1555 }1556 }1557 return null;1558}1559expectPrototype.standardErrorMessage = function (output, options) {1560 this._assertWrappedExpect();1561 options = typeof options === 'object' ? options : {};1562 if ('omitSubject' in output) {1563 options.subject = this.subject;1564 }1565 if (options && options.compact) {1566 options.compactSubject = (output) => {1567 output.jsFunctionName(this.subjectType.name);1568 };1569 }1570 return createStandardErrorMessage(1571 output,1572 this.subjectOutput,1573 this.testDescription,1574 this.argsOutput,1575 options1576 );1577};1578expectPrototype._callInNestedContext = function (callback) {1579 this._assertWrappedExpect();1580 try {1581 let result = oathbreaker(callback());1582 if (utils.isPromise(result)) {1583 result = wrapPromiseIfNecessary(result);1584 if (result.isPending()) {1585 result = result.then(undefined, (e) => {1586 if (e && e._isUnexpected) {1587 const wrappedError = new UnexpectedError(this, e);1588 wrappedError.originalError = e.originalError;1589 throw wrappedError;1590 }1591 throw e;1592 });1593 }1594 } else {1595 result = makePromise.resolve(result);1596 }1597 return addAdditionalPromiseMethods(result, this.execute, this.subject);1598 } catch (e) {1599 if (e && e._isUnexpected) {1600 const wrappedError = new UnexpectedError(this, e);1601 wrappedError.originalError = e.originalError;1602 throw wrappedError;1603 }1604 throw e;1605 }1606};1607expectPrototype.shift = function (subject, assertionIndex) {1608 this._assertWrappedExpect();1609 if (arguments.length <= 1) {1610 if (arguments.length === 0) {1611 subject = this.subject;1612 }1613 assertionIndex = -1;1614 for (let i = 0; i < this.assertionRule.args.length; i += 1) {1615 const type = this.assertionRule.args[i].type;1616 if (type.is('assertion') || type.is('expect.it')) {1617 assertionIndex = i;1618 break;1619 }1620 }1621 } else if (arguments.length === 3) {1622 // The 3-argument syntax for wrappedExpect.shift is deprecated, please omit the first (expect) arg1623 subject = arguments[1];1624 assertionIndex = arguments[2];1625 }1626 if (assertionIndex !== -1) {1627 const args = this.args.slice(0, assertionIndex);1628 const rest = this.args.slice(assertionIndex);1629 const nextArgumentType = this.findTypeOf(rest[0]);1630 if (arguments.length > 1) {1631 // Legacy1632 this.argsOutput = (output) => {1633 args.forEach((arg, index) => {1634 if (index > 0) {1635 output.text(', ');1636 }1637 output.appendInspected(arg);1638 });1639 if (args.length > 0) {1640 output.sp();1641 }1642 if (nextArgumentType.is('string')) {1643 output.error(rest[0]);1644 } else if (rest.length > 0) {1645 output.appendInspected(rest[0]);1646 }1647 if (rest.length > 1) {1648 output.sp();1649 }1650 rest.slice(1).forEach((arg, index) => {1651 if (index > 0) {1652 output.text(', ');1653 }1654 output.appendInspected(arg);1655 });1656 };1657 }1658 if (nextArgumentType.is('expect.it')) {1659 return this.withError(1660 () => rest[0](subject),1661 (err) => {1662 this.fail(err);1663 }1664 );1665 } else if (nextArgumentType.is('string')) {1666 return this.execute(subject, ...rest);1667 } else {1668 return subject;1669 }1670 } else {1671 // No assertion to delegate to. Provide the new subject as the fulfillment value:1672 return subject;1673 }1674};1675expectPrototype._getSubjectType = function () {1676 this._assertWrappedExpect();1677 return this.findTypeOfWithParentType(1678 this.subject,1679 this.assertionRule.subject.type1680 );1681};1682expectPrototype._getArgTypes = function (index) {1683 this._assertWrappedExpect();1684 const lastIndex = this.assertionRule.args.length - 1;1685 return this.args.map((arg, index) => {1686 return this.findTypeOfWithParentType(1687 arg,1688 this.assertionRule.args[Math.min(index, lastIndex)].type1689 );1690 });1691};1692expectPrototype._getAssertionIndices = function () {1693 this._assertWrappedExpect();1694 if (!this._assertionIndices) {1695 const assertionIndices = [];1696 const args = this.args;1697 let currentAssertionRule = this.assertionRule;1698 let offset = 0;1699 // eslint-disable-next-line no-labels1700 OUTER: while (true) {1701 if (1702 currentAssertionRule.args.length > 1 &&1703 isAssertionArg(1704 currentAssertionRule.args[currentAssertionRule.args.length - 2]1705 )1706 ) {1707 assertionIndices.push(offset + currentAssertionRule.args.length - 2);1708 const suffixAssertions = findSuffixAssertions(1709 args[offset + currentAssertionRule.args.length - 2],1710 this1711 );1712 if (suffixAssertions) {1713 for (let i = 0; i < suffixAssertions.length; i += 1) {1714 if (suffixAssertions[i].args.some(isAssertionArg)) {1715 offset += currentAssertionRule.args.length - 1;1716 currentAssertionRule = suffixAssertions[i];1717 // eslint-disable-next-line no-labels1718 continue OUTER;1719 }1720 }1721 }1722 }1723 // No further assertions found,1724 break;1725 }1726 this._assertionIndices = assertionIndices;1727 }1728 return this._assertionIndices;1729};1730Object.defineProperty(expectPrototype, 'subjectType', {1731 enumerable: true,1732 get() {1733 this._assertWrappedExpect();1734 return this._getSubjectType();1735 },1736});1737Object.defineProperty(expectPrototype, 'argTypes', {1738 enumerable: true,1739 get() {1740 this._assertWrappedExpect();1741 return this._getArgTypes();1742 },1743});1744function createTopLevelExpect(1745 {1746 assertions = {},1747 typeByName = { any: anyType },1748 types = [anyType],1749 output,1750 format = magicpen.defaultFormat,1751 installedPlugins = [],1752 } = {},1753 parentExpect1754) {1755 const expect = function (...args) {1756 return expect._expect(new Context(), args);1757 };1758 if (parentExpect) {...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1const expect = require('./createTopLevelExpect')()2 .use(require('./styles'))3 .use(require('./types'))4 .use(require('./assertions'))5 .freeze();6// Add an inspect method to all the promises we return that will make the REPL, console.log, and util.inspect render it nicely in node.js:7require('unexpected-bluebird').prototype[require('./nodeJsCustomInspect')] =8 function () {9 return expect10 .createOutput(require('magicpen').defaultFormat)11 .appendInspected(this)12 .toString();13 };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-sinon'));4const expect = require('unexpected')5 .clone()6 .use(require('unexpected-sinon'));7const expect = require('unexpected')8 .clone()9 .use(require('unexpected-sinon'));10const expect = require('unexpected')11 .clone()12 .use(require('unexpected-sinon'));13const expect = require('unexpected')14 .clone()15 .use(require('unexpected-sinon'));16const expect = require('unexpected')17 .clone()18 .use(require('unexpected-sinon'));19const expect = require('unexpected')20 .clone()21 .use(require('unexpected-sinon'));22const expect = require('unexpected')23 .clone()24 .use(require('unexpected-sinon'));25const expect = require('unexpected')26 .clone()27 .use(require('unexpected-sinon'));28'use strict';29const expect = require('unexpected')30 .clone()31 .use(require('unexpected-sinon'));32const expect = require('unexpected')33 .clone()34 .use(require('unexpected-sinon'));35describe('Test suite', () => {36 it('should pass', () => {37 expect(1, 'to be', 1);38 });39});

Full Screen

Using AI Code Generation

copy

Full Screen

1var expect = require('unexpected')2 .clone()3 .use(require('unexpected-mitm'));4var http = require('http');5describe('http', function () {6 it('should work', function () {7 return expect(8 function (cb) {9 },10 {11 },12 );13 });14});

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected');2const test = require('unexpected').clone().use(require('unexpected-sinon'));3const sinon = require('sinon');4const fs = require('fs');5const { readFile } = require('fs');6const { promisify } = require('util');7const readFileAsync = promisify(readFile);8const { readFile: readFileCallback } = require('fs');9const { promisify: promisifyCallback } = require('util');10const readFileCallbackAsync = promisifyCallback(readFileCallback);11const { readFileWithCallback, readFileWithPromise } = require('./index');12describe('readFileWithCallback', () => {13 it('should read file content', done => {14 const expected = 'Hello World!';15 const filePath = './file.txt';16 const readFileStub = sinon.stub(fs, 'readFile').callsFake((path, options, callback) => {17 callback(null, expected);18 });19 readFileWithCallback(filePath, (err, content) => {20 expect(readFileStub, 'was called with', filePath, 'utf8');21 expect(content, 'to equal', expected);22 done();23 });24 });25 it('should throw error when file does not exist', done => {26 const expected = 'Hello World!';27 const filePath = './file.txt';28 const readFileStub = sinon.stub(fs, 'readFile').callsFake((path, options, callback) => {29 callback(new Error('File does not exist'), null);30 });31 readFileWithCallback(filePath, (err, content) => {32 expect(readFileStub, 'was called with', filePath, 'utf8');33 expect(err, 'to equal', new Error('File does not exist'));34 done();35 });36 });37});38describe('readFileWithPromise', () => {39 it('should read file content', async () => {40 const expected = 'Hello World!';41 const filePath = './file.txt';42 const readFileStub = sinon.stub(fs, 'readFile').callsFake((path, options, callback) => {43 callback(null, expected);44 });45 const content = await readFileWithPromise(filePath);46 expect(readFileStub, 'was called with', filePath, 'utf8');47 expect(content, 'to

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected').clone().use(require('unexpected-sinon'));2const sinon = require('sinon');3const expect2 = expect.createTopLevelExpect();4const spy = sinon.spy();5expect2(spy, 'was not called');6expect2(spy, 'was not called');7expect2(spy, 'was not called');8expect2(spy, 'was not called');9expect2(spy, 'was not called');10expect2(spy, 'was not called');11 at expect2 (/Users/andrew/Projects/unexpected-sinon/node_modules/unexpected/lib/Unexpected.js:252:21)12 at Object.<anonymous> (/Users/andrew/Projects/unexpected-sinon/test.js:7:5)13 at Module._compile (internal/modules/cjs/loader.js:1138:30)14 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)15 at Module.load (internal/modules/cjs/loader.js:986:32)16 at Function.Module._load (internal/modules/cjs/loader.js:879:14)17 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

Full Screen

Using AI Code Generation

copy

Full Screen

1var expect = require('unexpected')2 .clone()3 .use(require('unexpected-dom'))4 .use(require('unexpected-sinon'));5expect.addAssertion('<string> to have a class <string>', function (expect, subject, value) {6 return expect(subject, 'to have attributes', {7 class: expect.it('to contain', value)8 });9});10expect.addAssertion('<string> to have an id <string>', function (expect, subject, value) {11 return expect(subject, 'to have attributes', {12 });13});14expect.addAssertion('<string> to contain <string>', function (expect, subject, value) {15 return expect(subject, 'to contain', value);16});17expect.addAssertion('<string> to have a length of <number>', function (expect, subject, value) {18 return expect(subject, 'to have length', value);19});20expect.addAssertion('<string> to be visible', function (expect, subject) {21 return expect(subject, 'to have attributes', {22 class: expect.it('to contain', 'visible')23 });24});25expect.addAssertion('<string> to be invisible', function (expect, subject) {26 return expect(subject, 'to have attributes', {27 class: expect.it('to contain', 'invisible')28 });29});30expect.addAssertion('<string> to be disabled', function (expect, subject) {31 return expect(subject, 'to have attributes', {32 });33});34expect.addAssertion('<string> to be enabled', function (expect, subject) {35 return expect(subject, 'not to have attributes', {36 });37});38expect.addAssertion('<string> to be checked', function (expect, subject) {39 return expect(subject, 'to have attributes', {40 });41});42expect.addAssertion('<string> to be unchecked', function (expect, subject) {43 return expect(subject, 'not to have attributes', {

Full Screen

Using AI Code Generation

copy

Full Screen

1var expect = require('unexpected')2 .clone()3 .use(require('unexpected-mitm'));4var expect = require('unexpected')5 .clone()6 .use(require('unexpected-mitm'));7var expect = require('unexpected')8 .clone()9 .use(require('unexpected-mitm'));10var expect = require('unexpected')11 .clone()12 .use(require('unexpected-mitm'));13var expect = require('unexpected')14 .clone()15 .use(require('unexpected-mitm'));16var expect = require('unexpected')17 .clone()18 .use(require('unexpected-mitm'));19var expect = require('unexpected')20 .clone()21 .use(require('unexpected-mitm'));22var expect = require('unexpected')23 .clone()24 .use(require('unexpected-mitm'));25var expect = require('unexpected')26 .clone()27 .use(require('unexpected-mitm'));

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-dom'));4it('should render', () => {5 return expect(6 );7});8const expect = require('unexpected')9 .clone()10 .use(require('unexpected-react-shallow'));11it('should render', () => {12 return expect(13 );14});

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected')2 .clone()3 .use(require('unexpected-mitm'))4 .createTopLevelExpect({ output: { styles: { text: { pass: { color: 'green' } } } } });5describe('http request', function() {6 it('should return an object with the correct properties', function() {7 return expect(8 {9 headers: {10 }11 },12 {13 response: {14 headers: {15 },16 body: {17 }18 }19 },20 {21 headers: {22 },23 body: {24 }25 }26 );27 });28});

Full Screen

Using AI Code Generation

copy

Full Screen

1const expect = require('unexpected').createTopLevelExpect();2expect([1,2,3], 'to be an array');3expect({a:1, b:2}, 'to be an object');4expect('Hello World', 'to be a string');5expect(3.14159, 'to be a number');6expect(true, 'to be a boolean');7expect(null, 'to be null');8expect(undefined, 'to be undefined');9expect(function(){}, 'to be a function');10expect(Symbol(), 'to be a symbol');11expect(new Date(), 'to be a date');12expect(/a-z/, 'to be a regexp');13expect(new Error(), 'to be an error');14expect(new Promise(function(){}), 'to be a promise');15expect(new Set(), 'to be a set');16expect(new Map(), 'to be a map');

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run unexpected automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful