Skip to content

Commit a79f402

Browse files
committed
test(prefer-expect-assertions): add more cases
1 parent 31c53e4 commit a79f402

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

src/rules/__tests__/prefer-expect-assertions.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,29 @@ ruleTester.run('prefer-expect-assertions (loops)', rule, {
12421242
`,
12431243
options: [{ onlyFunctionsWithExpectInLoop: true }],
12441244
},
1245+
// todo: ideally this should be considered valid
1246+
// {
1247+
// code: dedent`
1248+
// describe('my tests', () => {
1249+
// beforeEach(expect.hasAssertions);
1250+
//
1251+
// it("is a number that is greater than four", () => {
1252+
// expect(number).toBeGreaterThan(4);
1253+
// });
1254+
//
1255+
// it("returns numbers that are greater than four", () => {
1256+
// for (const number of getNumbers()) {
1257+
// expect(number).toBeGreaterThan(4);
1258+
// }
1259+
// });
1260+
// });
1261+
//
1262+
// it("returns numbers that are greater than five", () => {
1263+
// expect(number).toBeGreaterThan(5);
1264+
// });
1265+
// `,
1266+
// options: [{ onlyFunctionsWithExpectInLoop: true }],
1267+
// },
12451268
],
12461269
invalid: [
12471270
{
@@ -1538,6 +1561,84 @@ ruleTester.run('prefer-expect-assertions (loops)', rule, {
15381561
},
15391562
],
15401563
},
1564+
{
1565+
code: dedent`
1566+
describe('my tests', () => {
1567+
beforeEach(expect.hasAssertions);
1568+
it("is a number that is greater than four", () => {
1569+
expect(number).toBeGreaterThan(4);
1570+
});
1571+
});
1572+
1573+
describe('more tests', () => {
1574+
it("returns numbers that are greater than four", () => {
1575+
for (const number of getNumbers()) {
1576+
expect(number).toBeGreaterThan(4);
1577+
}
1578+
});
1579+
});
1580+
1581+
it("returns numbers that are greater than five", () => {
1582+
expect(number).toBeGreaterThan(5);
1583+
});
1584+
`,
1585+
options: [{ onlyFunctionsWithExpectInLoop: true }],
1586+
errors: [
1587+
{
1588+
messageId: 'haveExpectAssertions',
1589+
column: 3,
1590+
line: 9,
1591+
suggestions: [
1592+
{
1593+
messageId: 'suggestAddingHasAssertions',
1594+
output: dedent`
1595+
describe('my tests', () => {
1596+
beforeEach(expect.hasAssertions);
1597+
it("is a number that is greater than four", () => {
1598+
expect(number).toBeGreaterThan(4);
1599+
});
1600+
});
1601+
1602+
describe('more tests', () => {
1603+
it("returns numbers that are greater than four", () => {expect.hasAssertions();
1604+
for (const number of getNumbers()) {
1605+
expect(number).toBeGreaterThan(4);
1606+
}
1607+
});
1608+
});
1609+
1610+
it("returns numbers that are greater than five", () => {
1611+
expect(number).toBeGreaterThan(5);
1612+
});
1613+
`,
1614+
},
1615+
{
1616+
messageId: 'suggestAddingAssertions',
1617+
output: dedent`
1618+
describe('my tests', () => {
1619+
beforeEach(expect.hasAssertions);
1620+
it("is a number that is greater than four", () => {
1621+
expect(number).toBeGreaterThan(4);
1622+
});
1623+
});
1624+
1625+
describe('more tests', () => {
1626+
it("returns numbers that are greater than four", () => {expect.assertions();
1627+
for (const number of getNumbers()) {
1628+
expect(number).toBeGreaterThan(4);
1629+
}
1630+
});
1631+
});
1632+
1633+
it("returns numbers that are greater than five", () => {
1634+
expect(number).toBeGreaterThan(5);
1635+
});
1636+
`,
1637+
},
1638+
],
1639+
},
1640+
],
1641+
},
15411642
{
15421643
code: dedent`
15431644
it.each([1, 2, 3])("returns numbers that are greater than four", () => {

0 commit comments

Comments
 (0)