Skip to content

Commit

Permalink
feat(test): add jest support
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-buss committed Nov 11, 2024
1 parent 185b405 commit 50039df
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 62 deletions.
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default style(
{
ignores: ["fixtures", "_fixtures", "src/typegen.d.ts"],
roblox: false,
test: true,
},
{
files: ["src/**/*.ts"],
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"eslint-plugin-jsonc": "2.16.0",
"eslint-plugin-markdown": "5.1.0",
"eslint-plugin-no-autofix": "2.1.0",
"eslint-plugin-no-only-tests": "3.3.0",
"eslint-plugin-package-json": "0.15.3",
"eslint-plugin-perfectionist": "3.6.0",
"eslint-plugin-promise": "7.1.0",
Expand Down Expand Up @@ -104,6 +105,7 @@
"@types/yargs": "17.0.33",
"bumpp": "9.7.1",
"eslint": "9.13.0",
"eslint-plugin-jest": "28.9.0",
"eslint-plugin-react-roblox-hooks": "5.1.0-rbx.1",
"eslint-plugin-simple-import-sort": "12.1.1",
"eslint-typegen": "0.3.2",
Expand All @@ -121,6 +123,7 @@
"peerDependencies": {
"@eslint-react/eslint-plugin": "^1.14.0",
"eslint": "^9.10.0",
"eslint-plugin-jest": "^28.9.0",
"eslint-plugin-react-roblox-hooks": "^5.1.0-rbx.1"
},
"peerDependenciesMeta": {
Expand All @@ -129,6 +132,9 @@
},
"eslint-plugin-react-roblox-hooks": {
"optional": true
},
"eslint-plugin-jest": {
"optional": true
}
},
"packageManager": "[email protected]",
Expand Down
34 changes: 28 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions scripts/typegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { builtinRules } from "eslint/use-at-your-own-risk";
import { flatConfigsToRulesDTS } from "eslint-typegen/core";
import fs from "node:fs/promises";
import { test } from "src/configs/test";

import {
combine,
Expand Down Expand Up @@ -53,6 +54,7 @@ const configs = await combine(
sortTsconfig(),
spelling(),
stylistic(),
test(),
typescript(),
unicorn(),
yaml(),
Expand Down
93 changes: 93 additions & 0 deletions src/configs/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { GLOB_TESTS } from "../globs";
import type {
OptionsFiles,
OptionsIsInEditor,
OptionsOverrides,
TypedFlatConfigItem,
} from "../types";
import { ensurePackages, interopDefault } from "../utils";

// Hold the reference so we don't redeclare the plugin on each call
let pluginTest: any;

export async function test(
options: OptionsFiles & OptionsIsInEditor & OptionsOverrides = {},
): Promise<Array<TypedFlatConfigItem>> {
const { files = GLOB_TESTS, isInEditor = false, overrides = {} } = options;

await ensurePackages(["eslint-plugin-jest"]);

const pluginJest = await interopDefault(import("eslint-plugin-jest"));

pluginTest ||= {
...pluginJest,
};

return [
{
name: "style/test/setup",
plugins: {
test: pluginTest,
},
},
{
files,
name: "style/test/rules",
rules: {
// Jest
"test/consistent-test-it": "error",
"test/expect-expect": "warn",
"test/max-expects": "warn",
"test/max-nested-describe": "error",
"test/no-alias-methods": "error",
"test/no-commented-out-tests": "warn",
"test/no-conditional-expect": "error",
"test/no-conditional-in-test": "error",
"test/no-disabled-tests": "warn",
"test/no-done-callback": "error",
"test/no-duplicate-hooks": "error",
"test/no-export": "error",
"test/no-focused-tests": isInEditor ? "off" : "error",
"test/no-identical-title": "error",
"test/no-standalone-expect": "error",
"test/no-test-prefixes": "error",
"test/no-untyped-mock-factory": "error",
"test/padding-around-all": "warn",
"test/prefer-called-with": "warn",
"test/prefer-comparison-matcher": "warn",
"test/prefer-each": "warn",
"test/prefer-equality-matcher": "warn",
"test/prefer-hooks-in-order": "warn",
"test/prefer-lowercase-title": "warn",
"test/prefer-strict-equal": "error",
"test/prefer-to-be": "error",
"test/prefer-to-contain": "error",
"test/prefer-to-have-length": "error",
"test/prefer-todo": "warn",
"test/require-hook": "error",
"test/require-to-throw-message": "warn",
"test/require-top-level-describe": "error",
"test/unbound-method": "error",
"test/valid-describe-callback": "error",
"test/valid-expect": "error",
"test/valid-expect-in-promise": "error",
"test/valid-title": "error",

// Disables
...{
"antfu/no-top-level-await": "off",
"no-unused-expressions": "off",
"ts/explicit-function-return-type": "off",
},

...overrides,
},
settings: {
jest: {
globalPackage: "@rbxts/jest-globals",
version: 27,
},
},
},
];
}
22 changes: 21 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import {
import { formatters } from "./configs/formatters";
import { packageJson } from "./configs/package-json";
import { spelling } from "./configs/spelling";
import { test } from "./configs/test";
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from "./types";
import { getOverrides, interopDefault, resolveSubOptions } from "./utils";
import { getOverrides, interopDefault, isInEditorEnvironment, resolveSubOptions } from "./utils";

const flatConfigProps: Array<keyof TypedFlatConfigItem> = [
"name",
Expand Down Expand Up @@ -82,6 +83,16 @@ export function style(
typescript: enableTypeScript,
} = options;

let isInEditor = options.isInEditor;
if (isInEditor === undefined) {
isInEditor = isInEditorEnvironment();
if (isInEditor) {
console.log(
"[@isentinel/eslint-config] Detected running in editor, some rules are disabled.",
);
}
}

const stylisticOptions =
options.stylistic === false
? false
Expand Down Expand Up @@ -153,6 +164,15 @@ export function style(
configs.push(stylistic(stylisticOptions));
}

if (options.test ?? false) {
configs.push(
test({
isInEditor,
overrides: getOverrides(options, "test"),
}),
);
}

if (enableReact) {
configs.push(
react({
Expand Down
8 changes: 8 additions & 0 deletions src/globs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export const GLOB_HTML = "**/*.htm?(l)";

export const GLOB_MARKDOWN_CODE = `${GLOB_MARKDOWN}/${GLOB_SRC}`;

export const GLOB_TESTS = [
`**/__tests__/**/*.${GLOB_SRC_EXT}`,
`**/*.spec.${GLOB_SRC_EXT}`,
`**/*.test.${GLOB_SRC_EXT}`,
`**/*.bench.${GLOB_SRC_EXT}`,
`**/*.benchmark.${GLOB_SRC_EXT}`,
];

export const GLOB_ALL_SRC = [GLOB_SRC, GLOB_STYLE, GLOB_JSON, GLOB_JSON5, GLOB_MARKDOWN, GLOB_HTML];

export const GLOB_EXCLUDE = [
Expand Down
Loading

0 comments on commit 50039df

Please sign in to comment.