Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest, jest-config): add defineConfig() helper #12801

Closed
wants to merge 30 commits into from
Closed

feat(jest, jest-config): add defineConfig() helper #12801

wants to merge 30 commits into from

Conversation

mrazauskas
Copy link
Contributor

Summary

Idea borrowed from Vue ecosystem (at this is there I saw it): a defineConfig() helper function which helps with types and autocompletion in CJS, ESM or TS:

Screenshot 2022-05-03 at 17 30 34

It takes an object, or function, or async function:

// cjs
const {defineConfig} = require('jest');

module.exports = defineConfig({
  verbose: true,
});
// esm
import jest from 'jest';

export default jest.defineConfig(() => ({
  verbose: true,
}));
// ts
import {JestConfig, defineConfig} from 'jest';

export default defineConfig(async () => {
  const verbose: JestConfig['verbose'] = await asyncGetVerbose();

  return {verbose};
});

Is this interesting and useful? Or sort of exotic and complicated?

Test plan

E2e and type tests added.


### Defaults
## Options

You can retrieve Jest's default options to expand them if needed:

```js title="jest.config.js"
const {defaults} = require('jest-config');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still thinking. Perhaps it would be better to expose defineJestConfig(), JestConfig and jestDefaults from 'jest'? Or better from 'jest-config'? It is nice to install only jest, in the other hand these are just devDependencies.

@Smrtnyk
Copy link

Smrtnyk commented May 4, 2022

this allows to have typed config without having @types/jest installed?

@mrazauskas
Copy link
Contributor Author

mrazauskas commented May 4, 2022

this allows to have typed config without having @types/jest installed?

Hm.. Do you mean @jest/types? Somehow I was sure that @types/jest doesn't include types for config.

* LICENSE file in the root directory of this source tree.
*/

import jest from 'jest';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can’t figure out why, but import {defineConfig} from 'jest' throws an error in ESM. import {defineConfig} from 'jest-config' works as expected. Seems like they both export defineConfig in the same way. Strange.

@mrazauskas
Copy link
Contributor Author

@SimenB @Smrtnyk What do you think?

Somewhat cleaner way to have type definitions and autocompletion in config files. The difference I am trying to introduce here is the following –

Current (JS with type hints and autocompletion, no type checks yet, but @jest/schema will do validation some day):

// jest.config.js

/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
  verbose: true,
};

module.exports = config;

This branch:

const {defineConfig} = require('jest');

module.exports = defineConfig({
  verbose: true,
});

Current (TS with type checking through ts-node):

// jest.config.ts

import type {Config} from '@jest/types';

const config: Config.InitialOptions = {
  verbose: true,
};

export default config;

This branch (type checks still through ts-node):

import {defineConfig} from 'jest';

export default defineConfig({
  verbose: true,
});

@mrazauskas mrazauskas marked this pull request as ready for review May 10, 2022 07:04
@mrazauskas mrazauskas marked this pull request as draft May 13, 2022 09:07
@mrazauskas mrazauskas deleted the feat-defineConfig branch May 19, 2022 06:34
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants