Skip to content

Commit

Permalink
Add testing library assertion on expect
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Sep 19, 2023
1 parent d5e63fc commit 5ffd56a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion code/lib/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
"prep": "../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@storybook/instrumenter": "workspace:^",
"@storybook/client-logger": "workspace:^",
"@storybook/instrumenter": "workspace:^",
"@testing-library/dom": "^9.3.1",
"@testing-library/jest-dom": "^6.1.3",
"@testing-library/user-event": "^14.4.3",
Expand Down
11 changes: 8 additions & 3 deletions code/lib/test/src/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
setState,
} from '@vitest/expect';
import * as matchers from '@testing-library/jest-dom/matchers';
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/types/matchers';
import type { PromisifyObject } from './utils';

// We only expose the jest compatible API for now
export interface Assertion<T> extends PromisifyObject<JestAssertion<T>> {
export interface Assertion<T>
extends PromisifyObject<JestAssertion<T>>,
TestingLibraryMatchers<ReturnType<ExpectStatic['stringContaining']>, Promise<void>> {
toHaveBeenCalledOnce(): Promise<void>;
toSatisfy<E>(matcher: (value: E) => boolean, message?: string): Promise<void>;
resolves: Assertion<T>;
Expand Down Expand Up @@ -80,7 +83,8 @@ export function createExpect() {
expect.getState().assertionCalls
}`
);
if (Error.captureStackTrace) Error.captureStackTrace(errorGen(), assertions);
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function')
Error.captureStackTrace(errorGen(), assertions);

expect.setState({
expectedAssertionsNumber: expected,
Expand All @@ -90,7 +94,8 @@ export function createExpect() {

function hasAssertions() {
const error = new Error('expected any number of assertion, but got none');
if (Error.captureStackTrace) Error.captureStackTrace(error, hasAssertions);
if ('captureStackTrace' in Error && typeof Error.captureStackTrace === 'function')
Error.captureStackTrace(error, hasAssertions);

expect.setState({
isExpectingAssertions: true,
Expand Down
1 change: 0 additions & 1 deletion code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
],
"resolutions": {
"@playwright/test": "1.36.0",
"@testing-library/jest-dom": "^5.11.9",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/experimental-utils": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
Expand Down
34 changes: 32 additions & 2 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ __metadata:
languageName: node
linkType: hard

"@adobe/css-tools@npm:^4.0.1":
"@adobe/css-tools@npm:^4.0.1, @adobe/css-tools@npm:^4.3.0":
version: 4.3.1
resolution: "@adobe/css-tools@npm:4.3.1"
checksum: 05672719b544cc0c21ae3ed0eb6349bf458e9d09457578eeeb07cf0f696469ac6417e9c9be1b129e5d6a18098a061c1db55b2275591760ef30a79822436fcbfa
Expand Down Expand Up @@ -8829,7 +8829,7 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/jest-dom@npm:^5.11.9":
"@testing-library/jest-dom@npm:^5.11.9, @testing-library/jest-dom@npm:^5.16.2":
version: 5.17.0
resolution: "@testing-library/jest-dom@npm:5.17.0"
dependencies:
Expand All @@ -8846,6 +8846,36 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/jest-dom@npm:^6.1.3":
version: 6.1.3
resolution: "@testing-library/jest-dom@npm:6.1.3"
dependencies:
"@adobe/css-tools": ^4.3.0
"@babel/runtime": ^7.9.2
aria-query: ^5.0.0
chalk: ^3.0.0
css.escape: ^1.5.1
dom-accessibility-api: ^0.5.6
lodash: ^4.17.15
redent: ^3.0.0
peerDependencies:
"@jest/globals": ">= 28"
"@types/jest": ">= 28"
jest: ">= 28"
vitest: ">= 0.32"
peerDependenciesMeta:
"@jest/globals":
optional: true
"@types/jest":
optional: true
jest:
optional: true
vitest:
optional: true
checksum: 544e01939d3c14a3d44ae2e2bb9fe2a0cb5a9e4992ca2728f41188fb9fb2d56e25f1a2e1c12000be2a94d8da36cb220b24020e1b5c5c4c4bede9058a0d80583d
languageName: node
linkType: hard

"@testing-library/react@npm:^11.2.2":
version: 11.2.7
resolution: "@testing-library/react@npm:11.2.7"
Expand Down

0 comments on commit 5ffd56a

Please sign in to comment.