Skip to content

Utils: add runRuleTester helper function #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
83ca91d
rule: add new noElementHandle rule
elaichenkov Apr 10, 2022
09acdee
chore: move end range to const
elaichenkov Apr 10, 2022
bf274cc
fix: getRange method for non-awaiting statements
elaichenkov Apr 10, 2022
6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov Apr 10, 2022
e151e3c
chore: improve noElementHandle rule
elaichenkov Apr 10, 2022
fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov Apr 11, 2022
3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov Apr 11, 2022
54725f3
rule: change noElementHandle to warn level
elaichenkov Apr 11, 2022
bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov Apr 11, 2022
4890c75
test: add valid tests for noElementHandle rule
elaichenkov Apr 11, 2022
7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov Apr 11, 2022
03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov Apr 11, 2022
c776eed
rule: update noElementHandle with getRange method
elaichenkov Apr 11, 2022
5d81362
devops: add NPM publish workflow (#42)
mxschmitt Apr 11, 2022
a709eb9
Add no eval rule (#41)
elaichenkov Apr 12, 2022
f906083
Add new "noFocusedTest" rule (#44)
elaichenkov Apr 13, 2022
edfff56
Add new "noWaitForTimeout" rule (#46)
elaichenkov Apr 15, 2022
d4129b0
Add new "noSkippedTest" rule (#45)
elaichenkov Apr 15, 2022
40cf01f
Merge branch 'master' of https://github.com/playwright-community/esli…
elaichenkov Apr 23, 2022
4c5bbd6
utils: add runRuleTest helper function
elaichenkov Apr 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/utils/rule-tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { RuleTester } = require('eslint');

/**
*
* @param {string} name - The name of the rule
* @param {string} rule - Path to the rule to test
* @param {Object} tests - The tests to run
* @param {string[]} tests.valid - Valid tests
* @param {string[]} tests.invalid - Invalid tests
*
* @example
* const rule = require('../lib/rules/missing-playwright-await');
*
* runRuleTester('missing-playwright-await', rule, {
* valid: ['await expect(page.locator('checkbox')).toBeChecked()'],
* invalid: ['expect(page.locator('checkbox')).toBeChecked()'],
* });
*/
function runRuleTester(name, rule, tests) {
const config = {
parserOptions: {
ecmaVersion: 2018,
},
};

return new RuleTester(config).run(name, rule, tests);
}

const wrapInTest = (input) => `test('test', async () => { ${input} })`;

module.exports = {
runRuleTester,
wrapInTest,
};
69 changes: 18 additions & 51 deletions test/missing-playwright-await.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const { RuleTester } = require("eslint");
const rule = require("../lib/rules/missing-playwright-await");

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const wrapInTest = (input) => `test('a', async () => { ${input} })`;
const { runRuleTester, wrapInTest } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/missing-playwright-await');

const invalid = (messageId, code, output, options = []) => ({
code: wrapInTest(code),
Expand All @@ -21,53 +13,28 @@ const valid = (code, options = []) => ({
options,
});

const options = [{ customMatchers: ["toBeCustomThing"] }];
const options = [{ customMatchers: ['toBeCustomThing'] }];

new RuleTester().run("missing-playwright-await", rule, {
runRuleTester('missing-playwright-await', rule, {
invalid: [
invalid(
"expect",
"expect(page).toBeChecked()",
"await expect(page).toBeChecked()"
),
invalid(
"expect",
"expect(page).not.toBeEnabled()",
"await expect(page).not.toBeEnabled()"
),
invalid('expect', 'expect(page).toBeChecked()', 'await expect(page).toBeChecked()'),
invalid('expect', 'expect(page).not.toBeEnabled()', 'await expect(page).not.toBeEnabled()'),

// Custom matchers
invalid('expect', 'expect(page).toBeCustomThing(false)', 'await expect(page).toBeCustomThing(false)', options),
invalid(
"expect",
"expect(page).toBeCustomThing(false)",
"await expect(page).toBeCustomThing(false)",
options
),
invalid(
"expect",
"expect(page).not.toBeCustomThing(true)",
"await expect(page).not.toBeCustomThing(true)",
'expect',
'expect(page).not.toBeCustomThing(true)',
'await expect(page).not.toBeCustomThing(true)',
options
),

// expect.soft
invalid(
"expect",
"expect.soft(page).toBeChecked()",
"await expect.soft(page).toBeChecked()"
),
invalid(
"expect",
"expect.soft(page).toBeChecked()",
"await expect.soft(page).toBeChecked()"
),
invalid('expect', 'expect.soft(page).toBeChecked()', 'await expect.soft(page).toBeChecked()'),
invalid('expect', 'expect.soft(page).toBeChecked()', 'await expect.soft(page).toBeChecked()'),

// test.step
invalid(
"testStep",
"test.step('foo', async () => {})",
"await test.step('foo', async () => {})"
),
invalid('testStep', "test.step('foo', async () => {})", "await test.step('foo', async () => {})"),
],
valid: [
valid('await expect(page).toEqualTitle("text")'),
Expand All @@ -81,11 +48,11 @@ new RuleTester().run("missing-playwright-await", rule, {
},

// Custom matchers
valid("await expect(page).toBeCustomThing(true)", options),
valid("await expect(page).toBeCustomThing(true)", options),
valid("await expect(page).toBeCustomThing(true)", options),
valid("await expect(page).toBeCustomThing(true)"),
valid("expect(page).toBeCustomThing(true)"),
valid('await expect(page).toBeCustomThing(true)', options),
valid('await expect(page).toBeCustomThing(true)', options),
valid('await expect(page).toBeCustomThing(true)', options),
valid('await expect(page).toBeCustomThing(true)'),
valid('expect(page).toBeCustomThing(true)'),

// expect.soft
valid('await expect.soft(page).toHaveText("text")'),
Expand Down
12 changes: 2 additions & 10 deletions test/no-element-handle.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { RuleTester } = require('eslint');
const { runRuleTester, wrapInTest } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-element-handle');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const wrapInTest = (input) => `test('verify noElementHandle rule', async () => { ${input} })`;

const invalid = (code, output) => ({
code: wrapInTest(code),
errors: [
Expand All @@ -28,7 +20,7 @@ const valid = (code) => ({
code: wrapInTest(code),
});

new RuleTester().run('no-element-handle', rule, {
runRuleTester('no-element-handle', rule, {
invalid: [
// element handle as const
invalid('const handle = await page.$("text=Submit");', 'const handle = page.locator("text=Submit");'),
Expand Down
12 changes: 2 additions & 10 deletions test/no-eval.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { RuleTester } = require('eslint');
const { runRuleTester, wrapInTest } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-eval');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const wrapInTest = (input) => `test('verify noEval rule', async () => { ${input} })`;

const invalid = (code) => ({
code: wrapInTest(code),
errors: [{ messageId: code.includes('page.$eval') ? 'noEval' : 'noEvalAll' }],
Expand All @@ -18,7 +10,7 @@ const valid = (code) => ({
code: wrapInTest(code),
});

new RuleTester().run('no-eval', rule, {
runRuleTester('no-eval', rule, {
invalid: [
// $eval with no arguments as const
invalid('const searchValue = await page.$eval("#search", el => el.value);'),
Expand Down
20 changes: 4 additions & 16 deletions test/no-focused-test.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const { RuleTester } = require('eslint');
const { runRuleTester } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-focused-test');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const invalid = (code, output) => ({
code,
errors: [
Expand All @@ -17,12 +11,9 @@ const invalid = (code, output) => ({
],
});

new RuleTester().run('no-focused-test', rule, {
runRuleTester('no-focused-test', rule, {
invalid: [
invalid(
'test.describe.only("skip this describe", () => {});',
'test.describe("skip this describe", () => {});'
),
invalid('test.describe.only("skip this describe", () => {});', 'test.describe("skip this describe", () => {});'),

invalid(
'test.describe.parallel.only("skip this describe", () => {});',
Expand All @@ -34,10 +25,7 @@ new RuleTester().run('no-focused-test', rule, {
'test.describe.serial("skip this describe", () => {});'
),

invalid(
'test.only("skip this test", async ({ page }) => {});',
'test("skip this test", async ({ page }) => {});'
),
invalid('test.only("skip this test", async ({ page }) => {});', 'test("skip this test", async ({ page }) => {});'),
],
valid: [
'test.describe("describe tests", () => {});',
Expand Down
23 changes: 6 additions & 17 deletions test/no-page-pause.spec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
const { RuleTester } = require("eslint");
const rule = require("../lib/rules/no-page-pause");

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const wrapInTest = (input) => `test('a', async () => { ${input} })`;
const { runRuleTester, wrapInTest } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-page-pause');

const invalid = (code) => ({
code: wrapInTest(code),
errors: [{ messageId: "noPagePause" }],
errors: [{ messageId: 'noPagePause' }],
});

const valid = (code) => ({
code: wrapInTest(code),
});

new RuleTester().run("no-page-pause", rule, {
invalid: [invalid("await page.pause()")],
valid: [
valid("await page.click()"),
valid("await expect(page).toBePaused()"),
],
runRuleTester('no-page-pause', rule, {
invalid: [invalid('await page.pause()')],
valid: [valid('await page.click()'), valid('await expect(page).toBePaused()')],
});
10 changes: 2 additions & 8 deletions test/no-skipped-test.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const { RuleTester } = require('eslint');
const { runRuleTester } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-skipped-test');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const invalid = (code, output) => ({
code,
errors: [
Expand All @@ -17,7 +11,7 @@ const invalid = (code, output) => ({
],
});

new RuleTester().run('no-skipped-test', rule, {
runRuleTester('no-skipped-test', rule, {
invalid: [
invalid('test.skip("skip this test", async ({ page }) => {});', 'test("skip this test", async ({ page }) => {});'),
invalid('test.describe.skip("skip this describe", () => {});', 'test.describe("skip this describe", () => {});'),
Expand Down
10 changes: 2 additions & 8 deletions test/no-wait-for-timeout.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const { RuleTester } = require('eslint');
const { runRuleTester } = require('../lib/utils/rule-tester');
const rule = require('../lib/rules/no-wait-for-timeout');

RuleTester.setDefaultConfig({
parserOptions: {
ecmaVersion: 2018,
},
});

const invalid = (code, output) => ({
code,
errors: [
Expand All @@ -17,7 +11,7 @@ const invalid = (code, output) => ({
],
});

new RuleTester().run('no-wait-for-timeout', rule, {
runRuleTester('no-wait-for-timeout', rule, {
invalid: [
invalid(`async function fn() { await page.waitForTimeout(1000) }`, 'async function fn() { }'),
invalid('async function fn() { return page.waitForTimeout(1000); }', 'async function fn() { }'),
Expand Down