Skip to content

Commit

Permalink
Merge pull request #1 from EskiMojo14/jest-globals
Browse files Browse the repository at this point in the history
support @jest/globals
  • Loading branch information
EskiMojo14 authored Nov 3, 2023
2 parents 3b742c8 + 8e992fd commit bed3a38
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ To automatically extend `expect` with all matchers, you can use
}
```

If you're using `@jest/globals` instead of injecting globals, you should use the `jest-globals` entry point instead of `all`.

```json
"jest": {
"setupFilesAfterEnv": ["mix-n-matchers/jest-globals"]
}
```

## Typescript

If your editor does not recognise the custom `mix-n-matchers` matchers, add a `global.d.ts` file to your project with:

```ts
// global.d.ts
import "mix-n-matchers/all";
```

Expand Down Expand Up @@ -130,6 +139,35 @@ declare global {
}
```

### `@jest/globals`

If you disable `injectGlobals` for Jest and instead import from `'@jest/globals'`, the setup will look slightly different.

If you just want all of the matchers, your `global.d.ts` file should have:

```ts
// global.d.ts
import "mix-n-matchers/jest-globals";
```

If you want finer control over which matchers are added, you should use the below:

```ts
// global.d.ts
import type { MixNMatchers, AsymmetricMixNMatchers } from "mix-n-matchers";

declare module "@jest/extend" {
export interface Matchers<R>
extends Pick<
MixNMatchers<R>,
"toBeCalledWithContext" | "lastCalledWithContext" | "nthCalledWithContext"
> {}

export interface AsymmetricMatchers
extends Pick<AsymmetricMixNMatchers, "exactly"> {}
}
```

## Matchers

### Asymmetric Matchers
Expand Down
2 changes: 1 addition & 1 deletion all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"module": "../dist/all.js",
"types": "../dist/all.d.ts",
"files": [
"dist"
"../dist"
],
"repository": {
"type": "git",
Expand Down
42 changes: 42 additions & 0 deletions jest-globals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "mix-n-matchers-all-globals",
"version": "1.0.0",
"description": "Miscellaneous custom Jest matchers",
"type": "module",
"main": "../dist/jest-globals.cjs",
"module": "../dist/jest-globals.js",
"types": "../dist/jest-globals.d.ts",
"files": [
"../dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/EskiMojo14/mix-n-matchers.git"
},
"keywords": [
"jest",
"matchers",
"extend",
"extended",
"test",
"testing",
"assertions"
],
"author": "eskimojo",
"license": "MIT",
"bugs": {
"url": "https://github.com/EskiMojo14/mix-n-matchers/issues"
},
"homepage": "https://github.com/EskiMojo14/mix-n-matchers#readme",
"peerDependencies": {
"jest": ">=28.0.0"
},
"peerDependenciesMeta": {
"jest": {
"optional": true
}
},
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
}
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
"./all": {
"import": "./dist/all.js",
"require": "./dist/all.cjs"
},
"./jest-globals": {
"import": "./dist/jest-globals.js",
"require": "./dist/jest-globals.cjs"
}
},
"files": [
"dist",
"all"
"all",
"jest-globals"
],
"scripts": {
"prepare": "husky install",
Expand Down Expand Up @@ -99,7 +104,8 @@
"tsup": {
"entry": [
"src/index.ts",
"src/all.ts"
"src/all.ts",
"src/jest-globals.ts"
],
"sourcemap": true,
"format": [
Expand Down
10 changes: 10 additions & 0 deletions src/jest-globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
import { expect } from "@jest/globals";
import * as mixNMatchers from "./index";

declare module "@jest/expect" {
interface Matchers<R> extends mixNMatchers.MixNMatchers<R> {}
interface AsymmetricMatchers extends mixNMatchers.AsymmetricMixNMatchers {}
}

expect.extend(mixNMatchers);

0 comments on commit bed3a38

Please sign in to comment.