Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v28.1.1
Choose a base ref
...
head repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v28.2.0
Choose a head ref
  • 2 commits
  • 5 files changed
  • 2 contributors

Commits on Apr 6, 2024

  1. feat: support providing aliases for @jest/globals package (#1543)

    G-Rath authored Apr 6, 2024
    Copy the full SHA
    744d4f6 View commit details
  2. chore(release): 28.2.0 [skip ci]

    # [28.2.0](v28.1.1...v28.2.0) (2024-04-06)
    
    ### Features
    
    * support providing aliases for `@jest/globals` package ([#1543](#1543)) ([744d4f6](744d4f6))
    semantic-release-bot committed Apr 6, 2024
    Copy the full SHA
    9f918bc View commit details
Showing with 228 additions and 4 deletions.
  1. +7 −0 CHANGELOG.md
  2. +21 −0 README.md
  3. +1 −1 package.json
  4. +192 −0 src/rules/utils/__tests__/parseJestFnCall.test.ts
  5. +7 −3 src/rules/utils/parseJestFnCall.ts
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [28.2.0](https://github.com/jest-community/eslint-plugin-jest/compare/v28.1.1...v28.2.0) (2024-04-06)


### Features

* support providing aliases for `@jest/globals` package ([#1543](https://github.com/jest-community/eslint-plugin-jest/issues/1543)) ([744d4f6](https://github.com/jest-community/eslint-plugin-jest/commit/744d4f6fa5685e0c87062cc867ecadbad9b2e06c))

## [28.1.1](https://github.com/jest-community/eslint-plugin-jest/compare/v28.1.0...v28.1.1) (2024-04-06)


21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -87,6 +87,27 @@ You can tell this plugin about any global Jests you have aliased using the
}
```

#### Aliased `@jest/globals`

You can tell this plugin to treat a different package as the source of Jest
globals using the `globalPackage` setting:

```json
{
"settings": {
"jest": {
"globalPackage": "bun:test"
}
}
}
```

> [!WARNING]
>
> While this can be used to apply rules when using alternative testing libraries
> and frameworks like `bun`, `vitest` and `node`, there's no guarantee the
> semantics this plugin assumes will hold outside of Jest
### Running rules only on test-related files

The rules provided by this plugin assume that the files they are checking are
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "28.1.1",
"version": "28.2.0",
"description": "ESLint rules for Jest",
"keywords": [
"eslint",
192 changes: 192 additions & 0 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Original file line number Diff line number Diff line change
@@ -737,6 +737,198 @@ ruleTester.run('global aliases', rule, {
],
});

ruleTester.run('global package source', rule, {
valid: [
{
code: dedent`
import { expect } from 'bun:test'
expect(x).toBe(y);
`,
parserOptions: { sourceType: 'module' },
settings: { jest: { globalPackage: '@jest/globals' } },
},
{
code: dedent`
const { it } = require('@jest/globals');
it('is not considered a test function', () => {});
`,
parserOptions: { sourceType: 'script' },
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: dedent`
const { fn: it } = require('bun:test');
it('is not considered a test function', () => {});
`,
parserOptions: { sourceType: 'script' },
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: dedent`
import { it } from '@jest/globals';
it('is not considered a test function', () => {});
`,
parserOptions: { sourceType: 'module' },
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: dedent`
import { fn as it } from 'bun:test';
it('is not considered a test function', () => {});
`,
parserOptions: { sourceType: 'module' },
settings: { jest: { globalPackage: 'bun:test' } },
},
],
invalid: [
{
code: 'expect(x).toBe(y);',
parserOptions: { sourceType: 'script' },
errors: [
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'expect',
type: 'expect',
head: {
original: null,
local: 'expect',
type: 'global',
node: 'expect',
},
members: ['toBe'],
}),
column: 1,
line: 1,
},
],
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: dedent`
import { describe, expect, it } from 'bun:test'
describe('some tests', () => {
it('ensures something', () => {
expect.assertions();
});
});
`,
parserOptions: { sourceType: 'module' },
errors: [
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'describe',
type: 'describe',
head: {
original: 'describe',
local: 'describe',
type: 'import',
node: 'describe',
},
members: [],
}),
column: 1,
line: 3,
},
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'it',
type: 'test',
head: {
original: 'it',
local: 'it',
type: 'import',
node: 'it',
},
members: [],
}),
column: 3,
line: 4,
},
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'expect',
type: 'expect',
head: {
original: 'expect',
local: 'expect',
type: 'import',
node: 'expect',
},
members: ['assertions'],
}),
column: 5,
line: 5,
},
],
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: dedent`
import { expect } from 'bun:test'
expect(x).not.toBe(y);
`,
parserOptions: { sourceType: 'module' },
errors: [
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'expect',
type: 'expect',
head: {
original: 'expect',
local: 'expect',
type: 'import',
node: 'expect',
},
members: ['not', 'toBe'],
}),
column: 1,
line: 3,
},
],
settings: { jest: { globalPackage: 'bun:test' } },
},
{
code: 'context("when there is an error", () => {})',
errors: [
{
messageId: 'details' as const,
data: expectedParsedJestFnCallResultData({
name: 'describe',
type: 'describe',
head: {
original: 'describe',
local: 'context',
type: 'global',
node: 'context',
},
members: [],
}),
column: 1,
line: 1,
},
],
settings: {
jest: {
globalPackage: 'bun:test',
globalAliases: { describe: ['context'] },
},
},
},
],
});

ruleTester.run('typescript', rule, {
valid: [
{
10 changes: 7 additions & 3 deletions src/rules/utils/parseJestFnCall.ts
Original file line number Diff line number Diff line change
@@ -188,6 +188,7 @@ const ValidJestFnCallChains = [
interface SharedConfigurationSettings {
jest?: {
globalAliases?: Record<string, string[]>;
globalPackage?: string;
version?: number | string;
};
}
@@ -584,9 +585,12 @@ const resolveToJestFn = (
}

if (maybeImport) {
// the identifier is imported from @jest/globals,
// so return the original import name
if (maybeImport.source === '@jest/globals') {
const globalPackage =
(context.settings as SharedConfigurationSettings).jest?.globalPackage ??
'@jest/globals';

// the identifier is imported from our global package so return the original import name
if (maybeImport.source === globalPackage) {
return {
original: maybeImport.imported,
local: maybeImport.local,