Skip to content

Commit

Permalink
Fixed testing-library config and TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
burtek committed Aug 16, 2023
1 parent 5f3e38b commit 287e919
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 35 deletions.
24 changes: 14 additions & 10 deletions .github/workflows/make-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,21 @@ jobs:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
scope: '@dtrw'
- name: 'Install dependencies'
run: yarn
# - name: 'Test configs'
# run: yarn test
- name: 'Lint configs'
run: yarn lint
- name: 'Make .d.ts'
run: yarn make-types
- name: 'Publish package'
run: "yarn publish --tag ${{ ((contains(github.ref_name, '-alpha') || contains(github.ref_name, '-beta')) && 'next') || 'latest' }} --access public"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: 'Create a github release'
uses: "marvinpinto/action-automatic-releases@latest"
# TODO: https://github.com/marvinpinto/actions/pull/642
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: ${{ contains(github.ref_name, '-alpha') || contains(github.ref_name, '-beta') }}
Expand All @@ -38,13 +51,4 @@ jobs:
tests
index.js
import-types.d.ts
- name: 'Install dependencies'
run: yarn
# - name: 'Test configs'
# run: yarn test
- name: 'Lint configs'
run: yarn lint
- name: 'Publish package'
run: "yarn publish --tag ${{ ((contains(github.ref_name, '-alpha') || contains(github.ref_name, '-beta')) && 'next') || 'latest' }} --access public"
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
index.d.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
*.log
/index.d.ts
11 changes: 8 additions & 3 deletions configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { prepareConfig as node } from './node.js';
import { prepareConfig as react } from './react.js';
import { prepareConfig as testingLibrary } from './testing-library.js';

/** @satisfies {Record<string, (config?: any) => import('eslint-define-config').FlatESLintConfig[]>} */
/**
* @template T
* @typedef {Promise<T> | T} MaybePromise
*/

/** @satisfies {Record<string, (config?: any) => MaybePromise<import('eslint-define-config').FlatESLintConfig[]>>} */
export const configs = {
base,
cypress,
Expand All @@ -32,7 +37,7 @@ export const configs = {
* @param {{ [K in Exclude<keyof typeof configs, 'base'>]?: Config<K> | true }} [providedConfigs]
* @returns
*/
export function prepareConfig(providedConfigs = {}) {
export async function prepareConfig(providedConfigs = {}) {
/** @type {{ [K in keyof typeof configs]?: Config<K> | true }} */
const config = { ...providedConfigs, base: true };
const configKeys = /** @type {Array<keyof typeof configs>} */(Object.keys(configs));
Expand All @@ -54,6 +59,6 @@ export function prepareConfig(providedConfigs = {}) {

return defineFlatConfig([
...base(),
...configKeys.flatMap(mapConfig)
...(await Promise.all(configKeys.map(mapConfig))).flatMap(x => x)
]);
}
43 changes: 27 additions & 16 deletions configs/testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ const files = ['**/*.test.{js,cjs,mjs,jsx,ts,cts,mts,tsx}'];

// eslint-disable-next-line no-warning-comments
// TODO: move to FlatConfig once testing-library and jest-dom are upgraded
export function prepareConfig() {
// eslint-plugin-jest-dom depends on @testing-library/dom, let's not break eslint configs without TL installed.
const { default: jestDom } = require('eslint-plugin-jest-dom');
return defineFlatConfig([
{
files,
plugins: {
'jest-dom': { rules: jestDom.rules },
'testing-library': { rules: testingLibrary.rules }
},
rules: {
...jestDom.configs.all.rules,
...testingLibrary.configs.react.rules,
'testing-library/prefer-user-event': 'warn'
}
export async function prepareConfig() {
/** @type {import('eslint-define-config').FlatESLintConfig[]} */
const baseConfig = [{
files,
plugins: { 'testing-library': { rules: testingLibrary.rules } },
rules: {
...testingLibrary.configs.react.rules,
'testing-library/prefer-user-event': 'warn'
}
]);
}];

// eslint-plugin-jest-dom depends on @testing-library/dom
try {
const { default: jestDom } = await import('eslint-plugin-jest-dom');
baseConfig.unshift({
files,
plugins: { 'jest-dom': { rules: jestDom.rules } },
rules: { ...jestDom.configs.all.rules }
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(new Error(
'testing-library config has been requested, but some peer dependencies have not been found',
{ cause: e }
));
}

return defineFlatConfig(baseConfig);
}
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { prepareConfig } from './configs/index.js';


export default defineFlatConfig([
...prepareConfig({ json: { additionalFiles: { jsonc: ['.czrc'] } } }),
...await prepareConfig({ json: { additionalFiles: { jsonc: ['.czrc'] } } }),
{
languageOptions: {
globals: { ...globals.node },
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"auto-changelog": "^2.4.0",
"eslint": "^8.43.0",
"exit-code": "^1.0.2",
"rimraf": "^5.0.1",
"typescript": "~5.1.6"
},
"peerDependencies": {
Expand All @@ -56,6 +57,7 @@
"private": false,
"scripts": {
"lint": "eslint {configs,rules}/**/*.js",
"make-types": "rimraf index.d.ts && tsc --noEmit false --declaration --emitDeclarationOnly --outFile index.js",
"test": "node tests/run-tests.js",
"preversion-temp-disabled": "yarn test",
"push": "git push --follow-tags",
Expand All @@ -67,7 +69,7 @@
"url": "https://github.com/burtek/eslint-config"
},
"engines": {
"node": ">=14"
"node": ">=16"
},
"type": "module",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"exactOptionalPropertyTypes": true,
"resolveJsonModule": true
},
"exclude": ["types.d.ts"]
"exclude": []
}
Loading

0 comments on commit 287e919

Please sign in to comment.