Skip to content

Commit 5496eb0

Browse files
committed
Add innerTextContets to the array of locators
1 parent 3132216 commit 5496eb0

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

src/rules/prefer-web-first-assertions.test.ts

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,257 @@ runRuleTester('prefer-web-first-assertions', rule, {
780780
`),
781781
},
782782

783+
// allInnerTexts
784+
{
785+
code: test('expect(await foo.allInnerTexts()).toBe("bar")'),
786+
errors: [
787+
{
788+
column: 28,
789+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
790+
endColumn: 61,
791+
line: 1,
792+
messageId: 'useWebFirstAssertion',
793+
},
794+
],
795+
output: test('await expect(foo).toHaveText("bar")'),
796+
},
797+
{
798+
code: test('expect(await foo.allInnerTexts()).not.toBe("bar")'),
799+
errors: [
800+
{
801+
column: 28,
802+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
803+
endColumn: 61,
804+
line: 1,
805+
messageId: 'useWebFirstAssertion',
806+
},
807+
],
808+
output: test('await expect(foo).not.toHaveText("bar")'),
809+
},
810+
{
811+
code: test('expect(await foo.allInnerTexts()).toEqual("bar")'),
812+
errors: [
813+
{
814+
column: 28,
815+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
816+
endColumn: 61,
817+
line: 1,
818+
messageId: 'useWebFirstAssertion',
819+
},
820+
],
821+
output: test('await expect(foo).toHaveText("bar")'),
822+
},
823+
{
824+
code: test('expect.soft(await foo.allInnerTexts()).toBe("bar")'),
825+
errors: [
826+
{
827+
column: 28,
828+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
829+
endColumn: 66,
830+
line: 1,
831+
messageId: 'useWebFirstAssertion',
832+
},
833+
],
834+
output: test('await expect.soft(foo).toHaveText("bar")'),
835+
},
836+
{
837+
code: test('expect["soft"](await foo.allInnerTexts()).not.toEqual("bar")'),
838+
errors: [
839+
{
840+
column: 28,
841+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
842+
endColumn: 69,
843+
line: 1,
844+
messageId: 'useWebFirstAssertion',
845+
},
846+
],
847+
output: test('await expect["soft"](foo).not.toHaveText("bar")'),
848+
},
849+
{
850+
code: test(`
851+
const fooLocator = page.locator('.fooClass');
852+
const fooLocatorText = await fooLocator.allInnerTexts();
853+
expect(fooLocatorText).toEqual('foo');
854+
`),
855+
errors: [
856+
{
857+
column: 9,
858+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
859+
endColumn: 31,
860+
line: 4,
861+
messageId: 'useWebFirstAssertion',
862+
},
863+
],
864+
output: test(`
865+
const fooLocator = page.locator('.fooClass');
866+
const fooLocatorText = fooLocator;
867+
await expect(fooLocatorText).toHaveText('foo');
868+
`),
869+
},
870+
{
871+
code: test(`
872+
const fooLocator = page.locator('.fooClass');
873+
let fooLocatorText = await fooLocator.allInnerTexts();
874+
expect(fooLocatorText).toEqual('foo');
875+
fooLocatorText = 'foo';
876+
expect(fooLocatorText).toEqual('foo');
877+
`),
878+
errors: [
879+
{
880+
column: 9,
881+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
882+
endColumn: 31,
883+
line: 4,
884+
messageId: 'useWebFirstAssertion',
885+
},
886+
],
887+
output: test(`
888+
const fooLocator = page.locator('.fooClass');
889+
let fooLocatorText = fooLocator;
890+
await expect(fooLocatorText).toHaveText('foo');
891+
fooLocatorText = 'foo';
892+
expect(fooLocatorText).toEqual('foo');
893+
`),
894+
},
895+
{
896+
code: test(`
897+
let fooLocatorText;
898+
const fooLocator = page.locator('.fooClass');
899+
fooLocatorText = 'Unrelated';
900+
fooLocatorText = await fooLocator.allInnerTexts();
901+
expect(fooLocatorText).toEqual('foo');
902+
`),
903+
errors: [
904+
{
905+
column: 9,
906+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
907+
endColumn: 31,
908+
line: 6,
909+
messageId: 'useWebFirstAssertion',
910+
},
911+
],
912+
output: test(`
913+
let fooLocatorText;
914+
const fooLocator = page.locator('.fooClass');
915+
fooLocatorText = 'Unrelated';
916+
fooLocatorText = fooLocator;
917+
await expect(fooLocatorText).toHaveText('foo');
918+
`),
919+
},
920+
{
921+
code: test(`
922+
let fooLocatorText;
923+
let fooLocatorText2;
924+
const fooLocator = page.locator('.fooClass');
925+
fooLocatorText = await fooLocator.allInnerTexts();
926+
fooLocatorText2 = await fooLocator.allInnerTexts();
927+
expect(fooLocatorText).toEqual('foo');
928+
`),
929+
errors: [
930+
{
931+
column: 9,
932+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
933+
endColumn: 31,
934+
line: 7,
935+
messageId: 'useWebFirstAssertion',
936+
},
937+
],
938+
output: test(`
939+
let fooLocatorText;
940+
let fooLocatorText2;
941+
const fooLocator = page.locator('.fooClass');
942+
fooLocatorText = fooLocator;
943+
fooLocatorText2 = await fooLocator.allInnerTexts();
944+
await expect(fooLocatorText).toHaveText('foo');
945+
`),
946+
},
947+
{
948+
code: test(`
949+
let fooLocatorText;
950+
fooLocatorText = 'foo';
951+
expect(fooLocatorText).toEqual('foo');
952+
fooLocatorText = await page.locator('.fooClass').allInnerTexts();
953+
expect(fooLocatorText).toEqual('foo');
954+
`),
955+
errors: [
956+
{
957+
column: 9,
958+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
959+
endColumn: 31,
960+
line: 6,
961+
messageId: 'useWebFirstAssertion',
962+
},
963+
],
964+
output: test(`
965+
let fooLocatorText;
966+
fooLocatorText = 'foo';
967+
expect(fooLocatorText).toEqual('foo');
968+
fooLocatorText = page.locator('.fooClass');
969+
await expect(fooLocatorText).toHaveText('foo');
970+
`),
971+
},
972+
{
973+
code: test(`
974+
const unrelatedAssignment = "unrelated";
975+
const fooLocatorText = await page.locator('.foo').allInnerTexts();
976+
expect(fooLocatorText).toEqual('foo');
977+
`),
978+
errors: [
979+
{
980+
column: 9,
981+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
982+
endColumn: 31,
983+
line: 4,
984+
messageId: 'useWebFirstAssertion',
985+
},
986+
],
987+
output: test(`
988+
const unrelatedAssignment = "unrelated";
989+
const fooLocatorText = page.locator('.foo');
990+
await expect(fooLocatorText).toHaveText('foo');
991+
`),
992+
},
993+
{
994+
code: test(`
995+
const locatorFoo = page.locator(".foo")
996+
const isBarText = await locatorFoo.locator(".bar").allInnerTexts()
997+
expect(isBarText).toBe("bar")
998+
`),
999+
errors: [
1000+
{
1001+
column: 9,
1002+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
1003+
endColumn: 26,
1004+
line: 4,
1005+
messageId: 'useWebFirstAssertion',
1006+
},
1007+
],
1008+
output: test(`
1009+
const locatorFoo = page.locator(".foo")
1010+
const isBarText = locatorFoo.locator(".bar")
1011+
await expect(isBarText).toHaveText("bar")
1012+
`),
1013+
},
1014+
{
1015+
code: test(`
1016+
const content = await foo.allInnerTexts();
1017+
expect(content).toBe("bar")
1018+
`),
1019+
errors: [
1020+
{
1021+
column: 9,
1022+
data: { matcher: 'toHaveText', method: 'allInnerTexts' },
1023+
endColumn: 24,
1024+
line: 3,
1025+
messageId: 'useWebFirstAssertion',
1026+
},
1027+
],
1028+
output: test(`
1029+
const content = foo;
1030+
await expect(content).toHaveText("bar")
1031+
`),
1032+
},
1033+
7831034
// isChecked
7841035
{
7851036
code: test('expect(await page.locator("howdy").isChecked()).toBe(true)'),

src/rules/prefer-web-first-assertions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MethodConfig = {
1616
}
1717

1818
const methods: Record<string, MethodConfig> = {
19+
allInnerTexts: { matcher: 'toHaveText', type: 'string' },
1920
allTextContents: { matcher: 'toHaveText', type: 'string' },
2021
getAttribute: {
2122
matcher: 'toHaveAttribute',

0 commit comments

Comments
 (0)