Skip to content

Commit 4b764b9

Browse files
authoredAug 13, 2023
feat: local types, supporting jest, @jest/globals, vitest (testing-library#511)
* feat!: local types, supporting jest, @jest/globals, vitest Moves matcher types back into this package and adds support for @jest/globals and vitest. BREAKING CHANGE: Removes the extend-expect script. Users should use the default import path or one of the new test platform-specific paths to automatically extend the appropriate "expect" instance. extend-expect was not documented in the Readme, so this change should have minimal impact. Users can now use the following import paths to automatically extend "expect" for their chosen test platform: - @testing-library/jest-dom - jest (@types/jest) - @testing-library/jest-dom/jest-globals - @jest/globals - @testing-library/jest-dom/vitest - vitest For example: import '@testing-library/jest-dom/jest-globals' Importing from one of the above paths will augment the appropriate matcher interface for the given test platform, assuming the import is done in a .ts file that is included in the user's tsconfig.json. It's also (still) possible to import the matchers directly without side effects: import * as matchers from '@testing-library/jest-dom/matchers' * Update kcd-scripts BREAKING CHANGE: Drop node < 14
1 parent d717c66 commit 4b764b9

22 files changed

+824
-23
lines changed
 

‎.github/workflows/validate.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
if: ${{ !contains(github.head_ref, 'all-contributors') }}
1717
strategy:
1818
matrix:
19-
node: [10.14, 12, 14, 15, 16]
19+
node: [14, 16, 18, 20]
2020
runs-on: ubuntu-latest
2121
steps:
2222
- name: ⬇️ Checkout repo

‎README.md

+48
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ clear to read and to maintain.
5151

5252
- [Installation](#installation)
5353
- [Usage](#usage)
54+
- [With `@jest/globals`](#with-jestglobals)
55+
- [With Vitest](#with-vitest)
5456
- [With TypeScript](#with-typescript)
57+
- [With another Jest-compatible `expect`](#with-another-jest-compatible-expect)
5558
- [Custom matchers](#custom-matchers)
5659
- [`toBeDisabled`](#tobedisabled)
5760
- [`toBeEnabled`](#tobeenabled)
@@ -128,6 +131,39 @@ import '@testing-library/jest-dom'
128131
setupFilesAfterEnv: ['<rootDir>/jest-setup.js']
129132
```
130133

134+
### With `@jest/globals`
135+
136+
If you are using [`@jest/globals`][jest-globals announcement] with
137+
[`injectGlobals: false`][inject-globals docs], you will need to use a different
138+
import in your tests setup file:
139+
140+
```javascript
141+
// In your own jest-setup.js (or any other name)
142+
import '@testing-library/jest-dom/jest-globals'
143+
```
144+
145+
[jest-globals announcement]:
146+
https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals
147+
[inject-globals docs]:
148+
https://jestjs.io/docs/configuration#injectglobals-boolean
149+
150+
### With Vitest
151+
152+
If you are using [vitest][], this module will work as-is, but you will need to
153+
use a different import in your tests setup file. This file should be added to
154+
the [`setupFiles`][vitest setupfiles] property in your vitest config:
155+
156+
```javascript
157+
// In your own vitest-setup.js (or any other name)
158+
import '@testing-library/jest-dom/vitest'
159+
160+
// In vitest.config.js add (if you haven't already)
161+
setupFiles: ['./vitest-setup.js']
162+
```
163+
164+
[vitest]: https://vitest.dev/
165+
[vitest setupfiles]: https://vitest.dev/config/#setupfiles
166+
131167
### With TypeScript
132168

133169
If you're using TypeScript, make sure your setup file is a `.ts` and not a `.js`
@@ -144,6 +180,18 @@ haven't already:
144180
],
145181
```
146182

183+
### With another Jest-compatible `expect`
184+
185+
If you are using a different test runner that is compatible with Jest's `expect`
186+
interface, it might be possible to use it with this library:
187+
188+
```javascript
189+
import * as matchers from '@testing-library/jest-dom/matchers'
190+
import {expect} from 'my-test-runner/expect'
191+
192+
expect.extend(matchers)
193+
```
194+
147195
## Custom matchers
148196

149197
`@testing-library/jest-dom` can work with any library or framework that returns

‎extend-expect.js

-2
This file was deleted.

‎jest-globals.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference path="types/jest-globals.d.ts" />

‎jest-globals.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const globals = require('@jest/globals')
2+
const extensions = require('./dist/matchers')
3+
4+
globals.expect.extend(extensions)

‎matchers.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as matchers from './types/matchers'
2+
3+
export = matchers

‎package.json

+50-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"version": "0.0.0-semantically-released",
44
"description": "Custom jest matchers to test the state of the DOM",
55
"main": "dist/index.js",
6+
"types": "types/index.d.ts",
67
"engines": {
7-
"node": ">=8",
8+
"node": ">=14",
89
"npm": ">=6",
910
"yarn": ">=1"
1011
},
@@ -19,8 +20,11 @@
1920
},
2021
"files": [
2122
"dist",
22-
"extend-expect.js",
23-
"matchers.js"
23+
"types",
24+
"*.d.ts",
25+
"jest-globals.js",
26+
"matchers.js",
27+
"vitest.js"
2428
],
2529
"keywords": [
2630
"testing",
@@ -32,7 +36,6 @@
3236
"license": "MIT",
3337
"dependencies": {
3438
"@babel/runtime": "^7.9.2",
35-
"@types/testing-library__jest-dom": "^5.9.1",
3639
"aria-query": "^5.0.0",
3740
"chalk": "^3.0.0",
3841
"@adobe/css-tools": "^4.0.1",
@@ -42,16 +45,44 @@
4245
"redent": "^3.0.0"
4346
},
4447
"devDependencies": {
48+
"@jest/globals": "^29.6.2",
49+
"expect": "^29.6.2",
4550
"jest-environment-jsdom-sixteen": "^1.0.3",
4651
"jest-watch-select-projects": "^2.0.0",
4752
"jsdom": "^16.2.1",
48-
"kcd-scripts": "^11.1.0",
49-
"pretty-format": "^25.1.0"
53+
"kcd-scripts": "^14.0.0",
54+
"pretty-format": "^25.1.0",
55+
"vitest": "^0.34.1",
56+
"typescript": "^5.1.6"
57+
},
58+
"peerDependencies": {
59+
"@jest/globals": ">= 28",
60+
"@types/jest": ">= 28",
61+
"jest": ">= 28",
62+
"vitest": ">= 0.32"
63+
},
64+
"peerDependenciesMeta": {
65+
"@jest/globals": {
66+
"optional": true
67+
},
68+
"@types/jest": {
69+
"optional": true
70+
},
71+
"jest": {
72+
"optional": true
73+
},
74+
"vitest": {
75+
"optional": true
76+
}
5077
},
5178
"eslintConfig": {
5279
"extends": "./node_modules/kcd-scripts/eslint.js",
80+
"parserOptions": {
81+
"sourceType": "module",
82+
"ecmaVersion": 2020
83+
},
5384
"rules": {
54-
"@babel/no-invalid-this": "off"
85+
"no-invalid-this": "off"
5586
},
5687
"overrides": [
5788
{
@@ -61,6 +92,18 @@
6192
"rules": {
6293
"max-lines-per-function": "off"
6394
}
95+
},
96+
{
97+
"files": [
98+
"**/*.d.ts"
99+
],
100+
"rules": {
101+
"@typescript-eslint/no-empty-interface": "off",
102+
"@typescript-eslint/no-explicit-any": "off",
103+
"@typescript-eslint/no-invalid-void-type": "off",
104+
"@typescript-eslint/no-unused-vars": "off",
105+
"@typescript-eslint/triple-slash-reference": "off"
106+
}
64107
}
65108
]
66109
},

‎src/extend-expect.js

-3
This file was deleted.

‎src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import './extend-expect'
1+
import * as extensions from './matchers'
2+
3+
expect.extend(extensions)

‎src/to-be-in-the-document.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function toBeInTheDocument(element) {
2929
'',
3030
),
3131
'',
32-
// eslint-disable-next-line @babel/new-cap
32+
// eslint-disable-next-line new-cap
3333
this.utils.RECEIVED_COLOR(this.isNot ? errorFound() : errorNotFound()),
3434
].join('\n')
3535
},

‎src/to-contain-element.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function toContainElement(container, element) {
1717
'element',
1818
),
1919
'',
20-
// eslint-disable-next-line @babel/new-cap
20+
// eslint-disable-next-line new-cap
2121
this.utils.RECEIVED_COLOR(`${this.utils.stringify(
2222
container.cloneNode(false),
2323
)} ${

‎src/to-contain-html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function toContainHTML(container, htmlText) {
2323
'',
2424
),
2525
'Expected:',
26-
// eslint-disable-next-line @babel/new-cap
26+
// eslint-disable-next-line new-cap
2727
` ${this.utils.EXPECTED_COLOR(htmlText)}`,
2828
'Received:',
2929
` ${this.utils.printReceived(container.cloneNode(true))}`,

‎src/utils.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class GenericTypeError extends Error {
2828
'',
2929
),
3030
'',
31-
// eslint-disable-next-line @babel/new-cap
31+
// eslint-disable-next-line new-cap
3232
`${context.utils.RECEIVED_COLOR(
3333
'received',
3434
)} value must ${expectedString}.`,
@@ -91,9 +91,9 @@ class InvalidCSSError extends Error {
9191
this.message = [
9292
received.message,
9393
'',
94-
// eslint-disable-next-line @babel/new-cap
94+
// eslint-disable-next-line new-cap
9595
context.utils.RECEIVED_COLOR(`Failing css:`),
96-
// eslint-disable-next-line @babel/new-cap
96+
// eslint-disable-next-line new-cap
9797
context.utils.RECEIVED_COLOR(`${received.css}`),
9898
].join('\n')
9999
}
@@ -137,11 +137,11 @@ function getMessage(
137137
) {
138138
return [
139139
`${matcher}\n`,
140-
// eslint-disable-next-line @babel/new-cap
140+
// eslint-disable-next-line new-cap
141141
`${expectedLabel}:\n${context.utils.EXPECTED_COLOR(
142142
redent(display(context, expectedValue), 2),
143143
)}`,
144-
// eslint-disable-next-line @babel/new-cap
144+
// eslint-disable-next-line new-cap
145145
`${receivedLabel}:\n${context.utils.RECEIVED_COLOR(
146146
redent(display(context, receivedValue), 2),
147147
)}`,

‎tests/setup-env.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import {plugins} from 'pretty-format'
2-
import '../src/extend-expect'
2+
import '../src/index'
33

44
expect.addSnapshotSerializer(plugins.ConvertAnsi)

‎tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"skipLibCheck": true
5+
},
6+
"include": ["*.d.ts", "types"]
7+
}

‎types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference path="jest.d.ts" />

‎types/jest-globals.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {type expect} from '@jest/globals'
2+
import {type TestingLibraryMatchers} from './matchers'
3+
4+
export {}
5+
declare module '@jest/expect' {
6+
export interface Matchers<R extends void | Promise<void>>
7+
extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
8+
}

‎types/jest.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference types="jest" />
2+
3+
import {type TestingLibraryMatchers} from './matchers'
4+
5+
declare global {
6+
namespace jest {
7+
interface Matchers<R = void, T = {}>
8+
extends TestingLibraryMatchers<typeof expect.stringContaining, R> {}
9+
}
10+
}

‎types/matchers.d.ts

+666
Large diffs are not rendered by default.

‎types/vitest.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {type expect} from 'vitest'
2+
import {type TestingLibraryMatchers} from './matchers'
3+
4+
export {}
5+
declare module '@vitest/expect' {
6+
interface JestAssertion<T = any>
7+
extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
8+
}

‎vitest.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference path="types/vitest.d.ts" />

‎vitest.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {expect} from 'vitest'
2+
import * as extensions from './dist/matchers'
3+
4+
expect.extend(extensions)

0 commit comments

Comments
 (0)
Please sign in to comment.