Skip to content

Commit b8d122c

Browse files
committed
add kcd-scripts, adjust for lint rules
1 parent 325d59e commit b8d122c

14 files changed

+8418
-1603
lines changed

Diff for: .eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tsup.config.ts
2+
dist/

Diff for: .eslintrc.cjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
extends: 'kentcdodds',
3+
rules: {
4+
'@typescript-eslint/no-explicit-any': 'off',
5+
'@typescript-eslint/no-empty-interface': 'off',
6+
'@typescript-eslint/no-non-null-assertion': 'off',
7+
'@typescript-eslint/unified-signatures': 'off',
8+
'@typescript-eslint/no-unused-vars': [
9+
'error',
10+
{
11+
args: 'after-used',
12+
argsIgnorePattern: '^_',
13+
ignoreRestSiblings: true,
14+
varsIgnorePattern: '^_',
15+
},
16+
],
17+
},
18+
}

Diff for: .git-blame-ignore-revs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# format with kcd-scripts
2+
325d59e3cd0bf4c7ab738381e1bb49aef3bc7363

Diff for: .prettierrc.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("kcd-scripts/prettier.js");

Diff for: package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@types/react": "^18",
6262
"@types/react-dom": "^18",
6363
"expect": "^29.7.0",
64+
"kcd-scripts": "^16.0.0",
6465
"pkg-pr-new": "^0.0.29",
6566
"prettier": "^3.3.3",
6667
"publint": "^0.2.11",
@@ -79,7 +80,12 @@
7980
"build": "tsup",
8081
"pkg-pr-new-publish": "yarn build && pkg-pr-new publish --no-template",
8182
"prepack": "yarn build",
83+
"format": "kcd-scripts format",
84+
"lint": "kcd-scripts lint --config .eslintrc.cjs",
8285
"verify": "attw --pack . && publint"
8386
},
84-
"packageManager": "[email protected]"
87+
"packageManager": "[email protected]",
88+
"resolutions": {
89+
"eslint-config-kentcdodds": "^21.0.0"
90+
}
8591
}

Diff for: src/assertable.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RenderStream} from './renderStream/createRenderStream.js'
1+
import {type RenderStream} from './renderStream/createRenderStream.js'
22

33
export const assertableSymbol = Symbol.for(
44
'@testing-library/react-render-stream:assertable',

Diff for: src/jest/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import {expect} from '@jest/globals'
2-
import {toRerender, toRenderExactlyTimes} from './renderStreamMatchers.js'
3-
import type {RenderStreamMatchers} from './renderStreamMatchers.js'
2+
import {
3+
toRerender,
4+
toRenderExactlyTimes,
5+
type RenderStreamMatchers,
6+
} from './renderStreamMatchers.js'
47

58
expect.extend({
69
toRerender,
710
toRenderExactlyTimes,
811
})
912

1013
declare global {
14+
// eslint-disable-next-line @typescript-eslint/no-namespace
1115
namespace jest {
16+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
1217
interface Matchers<R = void, T = {}> extends RenderStreamMatchers<R, T> {}
1318
}
1419
}

Diff for: src/jest/renderStreamMatchers.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type {MatcherFunction} from 'expect'
2-
import {WaitForRenderTimeoutError} from '@testing-library/react-render-stream'
3-
import type {
4-
Assertable,
5-
NextRenderOptions,
6-
RenderStream,
1+
import {MatcherContext, type MatcherFunction} from 'expect'
2+
import {
3+
WaitForRenderTimeoutError,
4+
type Assertable,
5+
type NextRenderOptions,
6+
type RenderStream,
77
} from '@testing-library/react-render-stream'
88
// explicitly imported the symbol from the internal file
99
// this will bundle the `Symbol.for` call twice, but we keep it private
@@ -24,7 +24,7 @@ export interface RenderStreamMatchers<R = void, T = {}> {
2424
}
2525

2626
export const toRerender: MatcherFunction<[options?: NextRenderOptions]> =
27-
async function (actual, options) {
27+
async function toRerender(this: MatcherContext, actual, options) {
2828
const _stream = actual as RenderStream<any> | Assertable
2929
const stream =
3030
assertableSymbol in _stream ? _stream[assertableSymbol] : _stream
@@ -44,20 +44,24 @@ export const toRerender: MatcherFunction<[options?: NextRenderOptions]> =
4444
pass,
4545
message() {
4646
return (
47-
hint +
48-
`\n\nExpected component to${pass ? ' not' : ''} rerender, ` +
47+
`${hint}\n\nExpected component to${pass ? ' not' : ''} rerender, ` +
4948
`but it did${pass ? '' : ' not'}.`
5049
)
5150
},
5251
}
5352
}
5453

5554
/** to be thrown to "break" test execution and fail it */
56-
const failed = {}
55+
const failed = new Error()
5756

5857
export const toRenderExactlyTimes: MatcherFunction<
5958
[times: number, options?: NextRenderOptions]
60-
> = async function (actual, times, optionsPerRender) {
59+
> = async function toRenderExactlyTimes(
60+
this: MatcherContext,
61+
actual,
62+
times,
63+
optionsPerRender,
64+
) {
6165
const _stream = actual as RenderStream<any> | Assertable
6266
const stream =
6367
assertableSymbol in _stream ? _stream[assertableSymbol] : _stream
@@ -70,6 +74,7 @@ export const toRenderExactlyTimes: MatcherFunction<
7074
}
7175
try {
7276
while (stream.totalRenderCount() < times) {
77+
// eslint-disable-next-line no-await-in-loop
7378
await stream.waitForNextRender(options)
7479
}
7580
} catch (e) {
@@ -95,8 +100,9 @@ export const toRenderExactlyTimes: MatcherFunction<
95100
pass,
96101
message() {
97102
return (
98-
hint +
99-
` Expected component to${pass ? ' not' : ''} render exactly ${times}.` +
103+
`${
104+
hint
105+
} Expected component to${pass ? ' not' : ''} render exactly ${times}.` +
100106
` It rendered ${stream.totalRenderCount()} times.`
101107
)
102108
},

Diff for: src/renderHookToSnapshotStream.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {RenderHookOptions} from '@testing-library/react'
2+
import {createElement} from 'rehackt'
23
import {createRenderStream} from './renderStream/createRenderStream.js'
3-
import type {NextRenderOptions} from './renderStream/createRenderStream.js'
4+
import {type NextRenderOptions} from './renderStream/createRenderStream.js'
45

56
import {Render} from './renderStream/Render.js'
6-
import {createElement} from 'rehackt'
77
import {Assertable, assertableSymbol, markAssertable} from './assertable.js'
88

99
export interface SnapshotStream<Snapshot, Props> extends Assertable {
@@ -48,7 +48,7 @@ export interface SnapshotStream<Snapshot, Props> extends Assertable {
4848

4949
export function renderHookToSnapshotStream<ReturnValue, Props extends {}>(
5050
renderCallback: (props: Props) => ReturnValue,
51-
{initialProps, ...options}: RenderHookOptions<Props> = {},
51+
{initialProps, ...renderOptions}: RenderHookOptions<Props> = {},
5252
): SnapshotStream<ReturnValue, Props> {
5353
const {render, ...stream} = createRenderStream<{value: ReturnValue}>()
5454

@@ -59,7 +59,7 @@ export function renderHookToSnapshotStream<ReturnValue, Props extends {}>(
5959

6060
const {rerender: baseRerender, unmount} = render(
6161
createElement(HookComponent, initialProps),
62-
options,
62+
renderOptions,
6363
)
6464

6565
function rerender(rerenderCallbackProps: Props) {

Diff for: src/renderStream/Render.tsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ export class RenderInstance<Snapshot> implements Render<Snapshot> {
7070
startTime: number
7171
commitTime: number
7272
count: number
73+
public snapshot: Snapshot
74+
private stringifiedDOM: string | undefined
75+
public renderedComponents: Array<string | React.ComponentType>
7376

7477
constructor(
7578
baseRender: BaseRender,
76-
public snapshot: Snapshot,
77-
private stringifiedDOM: string | undefined,
78-
public renderedComponents: Array<string | React.ComponentType>,
79+
snapshot: Snapshot,
80+
stringifiedDOM: string | undefined,
81+
renderedComponents: Array<string | React.ComponentType>,
7982
) {
83+
this.snapshot = snapshot
84+
this.stringifiedDOM = stringifiedDOM
85+
this.renderedComponents = renderedComponents
8086
this.id = baseRender.id
8187
this.phase = baseRender.phase
8288
this.actualDuration = baseRender.actualDuration
@@ -188,7 +194,7 @@ export function errorOnDomInteraction() {
188194
throw new Error(`
189195
DOM interaction with a snapshot detected in test.
190196
Please don't interact with the DOM you get from \`withinDOM\`,
191-
but still use \`screen\' to get elements for simulating user interaction.
197+
but still use \`screen\` to get elements for simulating user interaction.
192198
`)
193199
}
194200
events.forEach(event => {

0 commit comments

Comments
 (0)