Skip to content

test: refactor @marko/testing-library tests #582

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 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 14 additions & 28 deletions tests/create-testing-library-rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,40 +442,26 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
{
code: `
...['@testing-library/react', '@marko/testing-library'].map(
(testingFramework) =>
({
code: `
// case: render imported from Testing Library module
import { render } from '@testing-library/react'
import { render } from '${testingFramework}'
import { somethingElse } from 'another-module'
const foo = require('bar')

const utils = render();
`,
errors: [
{
line: 7,
column: 21,
messageId: 'renderError',
},
],
},
{
code: `
// case: render imported from Testing Library module
import { render } from '@marko/testing-library'
import { somethingElse } from 'another-module'
const foo = require('bar')

const utils = render();
`,
errors: [
{
line: 7,
column: 21,
messageId: 'renderError',
},
],
},
errors: [
{
line: 7,
column: 21,
messageId: 'renderError',
},
],
} as const)
),
{
code: `
// case: render imported from Testing Library module (require version)
Expand Down
35 changes: 14 additions & 21 deletions tests/lib/rules/await-async-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { createRuleTester } from '../test-utils';

const ruleTester = createRuleTester();

const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/react',
'@marko/testing-library',
];

interface TestCode {
code: string;
isAsync?: boolean;
Expand Down Expand Up @@ -325,33 +330,21 @@ ruleTester.run(RULE_NAME, rule, {
],

invalid: [
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
(query) =>
({
code: `// async queries without await operator or then method are not valid
import { render } from '@testing-library/react'

test("An example test", async () => {
doSomething()
const foo = ${query}('foo')
});
`,
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
} as const)
),
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
(query) =>
({
code: `// async queries for @marko/testing-library without await operator or then method are not valid
import { render } from '@marko/testing-library'
...SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) =>
ALL_ASYNC_COMBINATIONS_TO_TEST.map(
(query) =>
({
code: `// async queries without await operator or then method are not valid
import { render } from '${testingFramework}'

test("An example test", async () => {
doSomething()
const foo = ${query}('foo')
});
`,
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
} as const)
errors: [{ messageId: 'awaitAsyncQuery', line: 6, column: 21 }],
} as const)
)
),
...ALL_ASYNC_COMBINATIONS_TO_TEST.map(
(query) =>
Expand Down
87 changes: 43 additions & 44 deletions tests/lib/rules/await-async-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,54 @@ import { createRuleTester } from '../test-utils';

const ruleTester = createRuleTester();

const SUPPORTED_TESTING_FRAMEWORKS = [
'@testing-library/dom',
'@marko/testing-library',
];

ruleTester.run(RULE_NAME, rule, {
valid: [
valid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util directly waited with await operator is valid', async () => {
doSomethingElse();
await ${asyncUtil}(() => getByLabelText('email'));
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util promise saved in var and waited with await operator is valid', async () => {
doSomethingElse();
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
await aPromise;
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util directly chained with then is valid', () => {
doSomethingElse();
${asyncUtil}(() => getByLabelText('email')).then(() => { console.log('done') });
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util promise saved in var and chained with then is valid', () => {
doSomethingElse();
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
aPromise.then(() => { console.log('done') });
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util directly returned in arrow function is valid', async () => {
const makeCustomWait = () =>
${asyncUtil}(() =>
Expand All @@ -59,10 +60,9 @@ ruleTester.run(RULE_NAME, rule, {
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util explicitly returned in arrow function is valid', async () => {
const makeCustomWait = () => {
return ${asyncUtil}(() =>
Expand All @@ -72,10 +72,9 @@ ruleTester.run(RULE_NAME, rule, {
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util returned in regular function is valid', async () => {
function makeCustomWait() {
return ${asyncUtil}(() =>
Expand All @@ -85,10 +84,9 @@ ruleTester.run(RULE_NAME, rule, {
});
`,
})),

...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util promise saved in var and returned in function is valid', async () => {
const makeCustomWait = () => {
const aPromise = ${asyncUtil}(() =>
Expand Down Expand Up @@ -132,7 +130,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util used in with Promise.all() is valid', async () => {
await Promise.all([
${asyncUtil}(callback1),
Expand All @@ -143,7 +141,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util used in with Promise.all() with an await is valid', async () => {
await Promise.all([
await ${asyncUtil}(callback1),
Expand All @@ -154,7 +152,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util used in with Promise.all() with ".then" is valid', async () => {
Promise.all([
${asyncUtil}(callback1),
Expand All @@ -165,7 +163,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
{
code: `
import { waitFor, waitForElementToBeRemoved } from '@testing-library/dom';
import { waitFor, waitForElementToBeRemoved } from '${testingFramework}';
test('combining different async methods with Promise.all does not throw an error', async () => {
await Promise.all([
waitFor(() => getByLabelText('email')),
Expand All @@ -176,7 +174,7 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `
import { waitForElementToBeRemoved } from '@testing-library/dom';
import { waitForElementToBeRemoved } from '${testingFramework}';
test('waitForElementToBeRemoved receiving element rather than callback is valid', async () => {
doSomethingElse();
const emailInput = getByLabelText('email');
Expand All @@ -186,7 +184,7 @@ ruleTester.run(RULE_NAME, rule, {
},
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util used in Promise.allSettled + await expression is valid', async () => {
await Promise.allSettled([
${asyncUtil}(callback1),
Expand All @@ -197,7 +195,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util used in Promise.allSettled + then method is valid', async () => {
Promise.allSettled([
${asyncUtil}(callback1),
Expand All @@ -208,7 +206,7 @@ ruleTester.run(RULE_NAME, rule, {
})),
...ASYNC_UTILS.map((asyncUtil) => ({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';

function waitForSomethingAsync() {
return ${asyncUtil}(() => somethingAsync())
Expand All @@ -230,23 +228,24 @@ ruleTester.run(RULE_NAME, rule, {
})
`,
},

// edge case for coverage
// valid async query usage without any function defined
// so there is no innermost function scope found
`
import { waitFor } from '@testing-library/dom';
test('edge case for no innermost function scope', () => {
const foo = waitFor
})
`,
],
invalid: [
{
// edge case for coverage
// valid async query usage without any function defined
// so there is no innermost function scope found
code: `
import { waitFor } from '${testingFramework}';
test('edge case for no innermost function scope', () => {
const foo = waitFor
})
`,
},
]),
invalid: SUPPORTED_TESTING_FRAMEWORKS.flatMap((testingFramework) => [
...ASYNC_UTILS.map(
(asyncUtil) =>
({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util not waited is invalid', () => {
doSomethingElse();
${asyncUtil}(() => getByLabelText('email'));
Expand All @@ -266,7 +265,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util not waited is invalid', () => {
doSomethingElse();
const el = ${asyncUtil}(() => getByLabelText('email'));
Expand All @@ -286,7 +285,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import * as asyncUtil from '@testing-library/dom';
import * as asyncUtil from '${testingFramework}';
test('asyncUtil.${asyncUtil} util not handled is invalid', () => {
doSomethingElse();
asyncUtil.${asyncUtil}(() => getByLabelText('email'));
Expand All @@ -306,7 +305,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('${asyncUtil} util promise saved not handled is invalid', () => {
doSomethingElse();
const aPromise = ${asyncUtil}(() => getByLabelText('email'));
Expand All @@ -326,7 +325,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import { ${asyncUtil} } from '@testing-library/dom';
import { ${asyncUtil} } from '${testingFramework}';
test('several ${asyncUtil} utils not handled are invalid', () => {
const aPromise = ${asyncUtil}(() => getByLabelText('username'));
doSomethingElse(aPromise);
Expand All @@ -353,7 +352,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import { ${asyncUtil}, render } from '@testing-library/dom';
import { ${asyncUtil}, render } from '${testingFramework}';

function waitForSomethingAsync() {
return ${asyncUtil}(() => somethingAsync())
Expand Down Expand Up @@ -400,7 +399,7 @@ ruleTester.run(RULE_NAME, rule, {
(asyncUtil) =>
({
code: `
import { ${asyncUtil}, render } from '@testing-library/dom';
import { ${asyncUtil}, render } from '${testingFramework}';

function waitForSomethingAsync() {
return ${asyncUtil}(() => somethingAsync())
Expand Down Expand Up @@ -443,5 +442,5 @@ ruleTester.run(RULE_NAME, rule, {
],
} as const)
),
],
]),
});
Loading