Skip to content

Commit

Permalink
test: use uvu to run rule tests for better reports
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Oct 5, 2021
1 parent e13381f commit 8a23cda
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,62 @@ import rule, {
} from '../../src/rules/store/avoid-combining-selectors'
import { ruleTester } from '../utils'

ruleTester().run(path.parse(__filename).name, rule, {
valid: [
`
export const test = () =>
ruleTester().run(path.parse(__filename).name, rule, {
valid: [
`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
readonly test$ = somethingOutside();
}`,
`
`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
vm$ = this.store.select(selectItems)
constructor(private store: Store){}
}
`,
`
`
import { Store, select } from '@ngrx/store'
@Component()
export class FixtureComponent {
vm$ = this.store.pipe(select(selectItems))
constructor(private store: Store){}
}
`,
`
`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
vm$ = combineLatest(this.store.select(selectItems), this.somethingElse())
constructor(private store: Store){}
}
`,
`
`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
vm$ = combineLatest(this.somethingElse(), this.store.select(selectItems))
constructor(private store: Store){}
}
`,
],
invalid: [
fromFixture(
stripIndent`
],
invalid: [
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
vm$ = combineLatest(this.store.select(selectItems), this.store.select(selectOtherItems))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [${messageId}]
constructor(private store: Store){}
}
`,
),
fromFixture(
stripIndent`
),
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
Expand All @@ -69,9 +70,9 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private store: Store){}
}
`,
),
fromFixture(
stripIndent`
),
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
Expand All @@ -80,9 +81,9 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private customName: Store){}
}
`,
),
fromFixture(
stripIndent`
),
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
Expand All @@ -91,9 +92,9 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private customName: Store){}
}
`,
),
fromFixture(
stripIndent`
),
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
Expand All @@ -102,10 +103,10 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private store: Store){}
}
`,
),
),

fromFixture(
stripIndent`
fromFixture(
stripIndent`
import { Store } from '@ngrx/store'
@Component()
export class FixtureComponent {
Expand All @@ -114,6 +115,6 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private store: Store){}
}
`,
),
],
})
),
],
})
15 changes: 15 additions & 0 deletions tests/rules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from 'path'
import { test } from 'uvu'
import { traverseFolder } from '../src/utils'

for (const rule of traverseFolder(path.join(__dirname, 'fixtures'))) {
test(rule.file, () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const result = require(rule.path)
if (result?.test) {
result.test()
}
})
}

test.run()
61 changes: 33 additions & 28 deletions tests/rules/avoid-cyclic-effects.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { stripIndent } from 'common-tags'
import { fromFixture } from 'eslint-etc'
import path from 'path'
import { test } from 'uvu'
import rule, { messageId } from '../../src/rules/effects/avoid-cyclic-effects'
import { ruleTester } from '../utils'

const setup = `
test(__filename, () => {
const setup = `
import type { OnRunEffects } from '@ngrx/effects'
import { EffectConfig } from '@ngrx/effects'
import { Actions, createEffect, ofType } from '@ngrx/effects'
Expand All @@ -19,16 +21,16 @@ const setup = `
bar
}
`.concat(
[
"const subject = 'SUBJECT'",
'const genericFoo = createAction(`${subject} FOO`);',
'const genericBar = createAction(`${subject} BAR`);',
].join('\n'),
)
[
"const subject = 'SUBJECT'",
'const genericFoo = createAction(`${subject} FOO`);',
'const genericBar = createAction(`${subject} BAR`);',
].join('\n'),
)

ruleTester().run(path.parse(__filename).name, rule, {
valid: [
`
ruleTester().run(path.parse(__filename).name, rule, {
valid: [
`
${setup}
class Effect {
foo$ = createEffect(() =>
Expand All @@ -43,7 +45,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`,
`
`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -58,7 +60,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`,
`
`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -73,7 +75,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`,
`
`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -88,7 +90,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`,
`
`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -104,7 +106,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`,
`
`
${setup}
class Effect {
foo$: CreateEffectMetadata
Expand All @@ -121,8 +123,8 @@ ruleTester().run(path.parse(__filename).name, rule, {
}
}
`,
// https://github.com/timdeschryver/eslint-plugin-ngrx/issues/223
`
// https://github.com/timdeschryver/eslint-plugin-ngrx/issues/223
`
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
Expand Down Expand Up @@ -154,14 +156,14 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private readonly actions$: Actions) {}
}
`,
],
invalid: [
fromFixture(stripIndent`
],
invalid: [
fromFixture(stripIndent`
${setup}
class Effect {
foo$ = createEffect(() =>
this.actions$.pipe(
~~~~~~~~~~~~~~~~~~ [${messageId}]
~~~~~~~~~~~~~~~~~~~ [${messageId}]
ofType(foo),
tap(() => alert('hi'))
),
Expand All @@ -172,7 +174,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`),
fromFixture(stripIndent`
fromFixture(stripIndent`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -188,7 +190,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`),
fromFixture(stripIndent`
fromFixture(stripIndent`
${setup}
class Effect {
foo$ = createEffect(() => {
Expand All @@ -204,7 +206,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
) {}
}
`),
fromFixture(stripIndent`
fromFixture(stripIndent`
${setup}
class Effect {
foo$ = createEffect(
Expand All @@ -222,7 +224,7 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private actions$: Actions) {}
}
`),
fromFixture(stripIndent`
fromFixture(stripIndent`
${setup}
class Effect {
foo$: CreateEffectMetadata
Expand All @@ -239,8 +241,8 @@ ruleTester().run(path.parse(__filename).name, rule, {
}
}
`),
// https://github.com/timdeschryver/eslint-plugin-ngrx/issues/223
fromFixture(stripIndent`
// https://github.com/timdeschryver/eslint-plugin-ngrx/issues/223
fromFixture(stripIndent`
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Action } from '@ngrx/store';
Expand Down Expand Up @@ -273,5 +275,8 @@ ruleTester().run(path.parse(__filename).name, rule, {
constructor(private readonly actions$: Actions) {}
}
`),
],
],
})
})

test.run()

0 comments on commit 8a23cda

Please sign in to comment.