|
1 | 1 | /* eslint-disable no-sparse-arrays */
|
2 |
| -import { AssertionError } from 'node:assert' |
| 2 | +import nodeAssert, { AssertionError } from 'node:assert' |
3 | 3 | import { stripVTControlCharacters } from 'node:util'
|
4 | 4 | import { generateToBeMessage } from '@vitest/expect'
|
5 | 5 | import { processError } from '@vitest/utils/error'
|
@@ -1114,15 +1114,15 @@ describe('async expect', () => {
|
1114 | 1114 | expect.unreachable()
|
1115 | 1115 | }
|
1116 | 1116 | catch (error) {
|
1117 |
| - expect(error).toEqual(new Error('promise resolved "+0" instead of rejecting')) |
| 1117 | + expect(error).toMatchObject({ message: 'promise resolved "+0" instead of rejecting' }) |
1118 | 1118 | }
|
1119 | 1119 |
|
1120 | 1120 | try {
|
1121 | 1121 | await expect({ then: (_: any, reject: any) => reject(0) }).resolves.toBe(0)
|
1122 | 1122 | expect.unreachable()
|
1123 | 1123 | }
|
1124 | 1124 | catch (error) {
|
1125 |
| - expect(error).toEqual(new Error('promise rejected "+0" instead of resolving')) |
| 1125 | + expect(error).toMatchObject({ message: 'promise rejected "+0" instead of resolving' }) |
1126 | 1126 | }
|
1127 | 1127 | })
|
1128 | 1128 | })
|
@@ -1608,6 +1608,174 @@ it('asymmetric matcher error', () => {
|
1608 | 1608 | }).toThrow(MyError1))
|
1609 | 1609 | })
|
1610 | 1610 |
|
| 1611 | +it('error equality', () => { |
| 1612 | + class MyError extends Error { |
| 1613 | + constructor(message: string, public custom: string) { |
| 1614 | + super(message) |
| 1615 | + } |
| 1616 | + } |
| 1617 | + |
| 1618 | + class YourError extends Error { |
| 1619 | + constructor(message: string, public custom: string) { |
| 1620 | + super(message) |
| 1621 | + } |
| 1622 | + } |
| 1623 | + |
| 1624 | + { |
| 1625 | + // different custom property |
| 1626 | + const e1 = new MyError('hi', 'a') |
| 1627 | + const e2 = new MyError('hi', 'b') |
| 1628 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1629 | + expect(e1).not.toEqual(e2) |
| 1630 | + expect(e1).not.toStrictEqual(e2) |
| 1631 | + assert.deepEqual(e1, e2) |
| 1632 | + nodeAssert.notDeepStrictEqual(e1, e2) |
| 1633 | + |
| 1634 | + // toThrowError also compare errors similar to toEqual |
| 1635 | + snapshotError(() => |
| 1636 | + expect(() => { |
| 1637 | + throw e1 |
| 1638 | + }).toThrowError(e2), |
| 1639 | + ) |
| 1640 | + } |
| 1641 | + |
| 1642 | + { |
| 1643 | + // different message |
| 1644 | + const e1 = new MyError('hi', 'a') |
| 1645 | + const e2 = new MyError('hello', 'a') |
| 1646 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1647 | + expect(e1).not.toEqual(e2) |
| 1648 | + expect(e1).not.toStrictEqual(e2) |
| 1649 | + assert.notDeepEqual(e1, e2) |
| 1650 | + nodeAssert.notDeepStrictEqual(e1, e2) |
| 1651 | + } |
| 1652 | + |
| 1653 | + { |
| 1654 | + // different class |
| 1655 | + const e1 = new MyError('hello', 'a') |
| 1656 | + const e2 = new YourError('hello', 'a') |
| 1657 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1658 | + expect(e1).not.toEqual(e2) |
| 1659 | + expect(e1).not.toStrictEqual(e2) // toStrictEqual checks constructor already |
| 1660 | + assert.deepEqual(e1, e2) |
| 1661 | + nodeAssert.notDeepStrictEqual(e1, e2) |
| 1662 | + } |
| 1663 | + |
| 1664 | + { |
| 1665 | + // same |
| 1666 | + const e1 = new MyError('hi', 'a') |
| 1667 | + const e2 = new MyError('hi', 'a') |
| 1668 | + expect(e1).toEqual(e2) |
| 1669 | + expect(e1).toStrictEqual(e2) |
| 1670 | + assert.deepEqual(e1, e2) |
| 1671 | + nodeAssert.deepStrictEqual(e1, e2) |
| 1672 | + |
| 1673 | + expect(() => { |
| 1674 | + throw e1 |
| 1675 | + }).toThrowError(e2) |
| 1676 | + } |
| 1677 | + |
| 1678 | + { |
| 1679 | + // same |
| 1680 | + const e1 = new MyError('hi', 'a') |
| 1681 | + const e2 = new MyError('hi', 'a') |
| 1682 | + expect(e1).toEqual(e2) |
| 1683 | + expect(e1).toStrictEqual(e2) |
| 1684 | + assert.deepEqual(e1, e2) |
| 1685 | + nodeAssert.deepStrictEqual(e1, e2) |
| 1686 | + } |
| 1687 | + |
| 1688 | + { |
| 1689 | + // different cause |
| 1690 | + const e1 = new Error('hello', { cause: 'x' }) |
| 1691 | + const e2 = new Error('hello', { cause: 'y' }) |
| 1692 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1693 | + expect(e1).not.toEqual(e2) |
| 1694 | + } |
| 1695 | + |
| 1696 | + { |
| 1697 | + // different cause (asymmetric fail) |
| 1698 | + const e1 = new Error('hello') |
| 1699 | + const e2 = new Error('hello', { cause: 'y' }) |
| 1700 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1701 | + expect(e1).not.toEqual(e2) |
| 1702 | + } |
| 1703 | + |
| 1704 | + { |
| 1705 | + // different cause (asymmetric pass) |
| 1706 | + const e1 = new Error('hello', { cause: 'x' }) |
| 1707 | + const e2 = new Error('hello') |
| 1708 | + expect(e1).toEqual(e2) |
| 1709 | + } |
| 1710 | + |
| 1711 | + { |
| 1712 | + // different cause (fail by other props) |
| 1713 | + const e1 = new Error('hello', { cause: 'x' }) |
| 1714 | + const e2 = new Error('world') |
| 1715 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1716 | + } |
| 1717 | + |
| 1718 | + { |
| 1719 | + // different cause |
| 1720 | + const e1 = new Error('hello', { cause: 'x' }) |
| 1721 | + const e2 = { something: 'else' } |
| 1722 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1723 | + } |
| 1724 | + |
| 1725 | + { |
| 1726 | + // AggregateError (pass) |
| 1727 | + const e1 = new AggregateError([new Error('inner')], 'outer', { cause: 'x' }) |
| 1728 | + const e2 = new AggregateError([new Error('inner')], 'outer', { cause: 'x' }) |
| 1729 | + expect(e1).toEqual(e2) |
| 1730 | + } |
| 1731 | + |
| 1732 | + { |
| 1733 | + // AggregateError (fail) |
| 1734 | + const e1 = new AggregateError([new Error('inner', { cause: 'x' })], 'outer', { cause: 'x' }) |
| 1735 | + const e2 = new AggregateError([new Error('inner', { cause: 'y' })], 'outer', { cause: 'x' }) |
| 1736 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1737 | + } |
| 1738 | + |
| 1739 | + { |
| 1740 | + // cyclic (pass) |
| 1741 | + const e1 = new Error('hi') |
| 1742 | + e1.cause = e1 |
| 1743 | + const e2 = new Error('hi') |
| 1744 | + e2.cause = e2 |
| 1745 | + expect(e1).toEqual(e2) |
| 1746 | + } |
| 1747 | + |
| 1748 | + { |
| 1749 | + // cyclic (fail) |
| 1750 | + const e1 = new Error('hello') |
| 1751 | + e1.cause = e1 |
| 1752 | + const e2 = new Error('world') |
| 1753 | + e2.cause = e2 |
| 1754 | + snapshotError(() => expect(e1).toEqual(e2)) |
| 1755 | + } |
| 1756 | + |
| 1757 | + { |
| 1758 | + // asymmetric matcher |
| 1759 | + const e1 = new Error('hello', { cause: 'x' }) |
| 1760 | + expect(e1).toEqual(expect.objectContaining({ |
| 1761 | + message: 'hello', |
| 1762 | + cause: 'x', |
| 1763 | + })) |
| 1764 | + snapshotError(() => expect(e1).toEqual(expect.objectContaining({ |
| 1765 | + message: 'hello', |
| 1766 | + cause: 'y', |
| 1767 | + }))) |
| 1768 | + snapshotError(() => expect(e1).toEqual(expect.objectContaining({ |
| 1769 | + message: 'world', |
| 1770 | + cause: 'x', |
| 1771 | + }))) |
| 1772 | + snapshotError(() => expect(e1).toEqual(expect.objectContaining({ |
| 1773 | + message: 'world', |
| 1774 | + cause: 'y', |
| 1775 | + }))) |
| 1776 | + } |
| 1777 | +}) |
| 1778 | + |
1611 | 1779 | it('toHaveBeenNthCalledWith error', () => {
|
1612 | 1780 | const fn = vi.fn()
|
1613 | 1781 | fn('World')
|
|
0 commit comments