Skip to content

Commit

Permalink
feat: support providing aliases for @jest/globals package (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 19, 2024
1 parent 70c048d commit d070ca7
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 3 deletions.
192 changes: 192 additions & 0 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,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: [
{
Expand Down
9 changes: 6 additions & 3 deletions src/rules/utils/parseJestFnCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ declare module '@typescript-eslint/utils/dist/ts-eslint' {
export interface SharedConfigurationSettings {
jest?: {
globalAliases?: Record<string, string[]>;
globalPackage?: string;
version?: number | string;
};
}
Expand Down Expand Up @@ -567,9 +568,11 @@ 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.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,
Expand Down

0 comments on commit d070ca7

Please sign in to comment.