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: Add support for the `jest.config.ts` configuration file #10564

Merged
merged 39 commits into from Oct 12, 2020

Conversation

@Gamote
Copy link
Contributor

@Gamote Gamote commented Sep 29, 2020

Summary

Allow developers to define the configuration file using Typescript. This allow a project to be written only in TypeScript and helps with auto-suggestions.

This PR resolves #9636.

Example

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

// Sync object
const config: Config.InitialOptions = {
  verbose: true,
};
export default config;

// Or async function
export default async (): Promise<Config.InitialOptions> => {
  return {
    verbose: true,
  };
};

docs and CHANGELOG.md file were updated to reflect these changes.

Test plan

Tests have been added/updated to check for this behaviour and some messages were updated to indicate to the user about the new supported extension (.ts).

CC: @SimenB

docs/Configuration.md Outdated Show resolved Hide resolved
@dl748
Copy link

@dl748 dl748 commented Sep 29, 2020

note the "export =" should never be use. It requires the tsconfig.json to use a specific output (commonjs), which may not be what your project intends. "export default" is the correct way, for both async/sync

@Gamote
Copy link
Contributor Author

@Gamote Gamote commented Sep 29, 2020

note the "export =" should never be use. It requires the tsconfig.json to use a specific output (commonjs), which may not be what your project intends. "export default" is the correct way, for both async/sync

This was my thought as well, but if I'm using export default for objects the config values are added under the default property on compilation. I've tried to stop this behaviour with different configuration but without success. As @G-Rath recommended, I will replace typescript with ts-node and I will try it again. Thanks for the input ✌️

@G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Sep 29, 2020

export = can be fine in some situations, but what you're actually wanting is to use the interop function that'll be around "somewhere" which should make these both worth.

@Gamote
Copy link
Contributor Author

@Gamote Gamote commented Sep 29, 2020

export = can be fine in some situations, but what you're actually wanting is to use the interop function that'll be around "somewhere" which should make these both worth.

I've tried to add the interlop function but I had the same result. I will see if I have the same problem with the new compilation method.

@G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Sep 29, 2020

Was this the interlop function?

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
  obj && obj.__esModule ? obj : { default: obj };

const importDefault = (moduleName: string) =>
  // eslint-disable-next-line @typescript-eslint/no-require-imports
  interopRequireDefault(require(moduleName)).default;

That's what we've got in eslint-plugin-jest, where we're using export default in place of what technically should be export =.

I think that will break for export = but that's not a very common syntax so I think it's fine to treat export default as the thing to use, since it should always be usable 🙂

@Gamote Gamote marked this pull request as draft Sep 29, 2020
@dl748
Copy link

@dl748 dl748 commented Sep 29, 2020

what i generally do is

const myconfig = require('.....')
if( myconfig.default ) {
  if(typeof(myconfig.default) === 'function') return Promise.resolve(myconfig.default())
  return Promise.resolve(myconfig.default)
}
if(typeof(myconfig) === 'function') return Promise.resolve(myconfig())
return Promise.resolve(myconfig)

that should handle just about every way a module would be exported with a "function".

I'm not partial to empty called functions, so I would generally pass in the object of the code, jest or whatever, so that the configuration file can make decisions based on things like default settings or version specific code.

@G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Sep 29, 2020

@dl748 we shouldn't need to do any of that extra checking unless this PR is also adding the ability to export a function, as that's something jest should already have support for handling.

Effectively what we're wanting to do here is handle just the requireing of the config, which makes things easier - once we've got the config requireed it can just slip into the current flow i.e check if it's a function, validation, apply defaults, use, run tests, 🎉

@dl748
Copy link

@dl748 dl748 commented Sep 29, 2020

@dl748 we shouldn't need to do any of that extra checking unless this PR is also adding the ability to export a function, as that's something jest should already have support for handling.

Effectively what we're wanting to do here is handle just the requireing of the config, which makes things easier - once we've got the config requireed it can just slip into the current flow i.e check if it's a function, validation, apply defaults, use, run tests, 🎉

Yes I know, i'm just explaining how i've implemented things like interpret, for max compatibility with targeted languages like ES6 and TS

interpret is far more lightweight than importing the entire typescript library.

@Gamote Gamote marked this pull request as ready for review Sep 29, 2020
@Gamote Gamote marked this pull request as draft Sep 29, 2020
Gamote added 2 commits Sep 29, 2020
… 'ReferenceError: globalThis is not defined' on Node 10.x (nrwl/nx#3776 (comment))
@Gamote Gamote marked this pull request as ready for review Sep 30, 2020
@septs
Copy link

@septs septs commented Sep 30, 2020

via globalTeardown and setupFiles also need support load typescript file.

see https://github.com/DimensionDev/Maskbook/blob/9b975f30/jest.config.js#L12-L17

@G-Rath
Copy link
Contributor

@G-Rath G-Rath commented Sep 30, 2020

@septs while we're wanting to support everything, this PR & issue is just for the actual config file itself :)

The global issue we've got open tracking this effort is #8810 - setupFiles are actually already supported, but globalTeardown isn't on that list so I'll update it.

@septs
Copy link

@septs septs commented Sep 30, 2020

@Gamote may be need auto detect esm and auto require esm

see https://npm.im/esm

when the user uses the pure esmodule library or tsconfig.json - module not is commonjs

e.q:

package.json Outdated Show resolved Hide resolved
Gamote added 2 commits Oct 12, 2020
chores:
- added comment in the generated '.ts' which point to the doc for more detail on type checking
- fix tests to accommodate the new config comment
- tests added to check the '.ts' file
@Gamote Gamote requested a review from SimenB Oct 12, 2020
packages/jest-cli/src/init/__tests__/init.test.js Outdated Show resolved Hide resolved
packages/jest-cli/src/init/generate_config_file.ts Outdated Show resolved Hide resolved
packages/jest-cli/src/init/__tests__/init.test.js Outdated Show resolved Hide resolved
packages/jest-config/package.json Outdated Show resolved Hide resolved
@Gamote Gamote requested a review from SimenB Oct 12, 2020
@SimenB
SimenB approved these changes Oct 12, 2020
Copy link
Collaborator

@SimenB SimenB left a comment

Thanks!

@SimenB SimenB merged commit 591d612 into facebook:master Oct 12, 2020
21 of 22 checks passed
21 of 22 checks passed
cleanup-runs
Details
Running TypeScript compiler & ESLint
Details
Node v10.x on ubuntu-latest
Details
Node v10.x on macOS-latest
Details
Node v10.x on windows-latest
Details
Node v12.x on ubuntu-latest
Details
Node v12.x on macOS-latest
Details
Node v12.x on windows-latest
Details
Node v13.x on ubuntu-latest
Details
Node v13.x on macOS-latest
Details
Node v13.x on windows-latest
Details
Node v14.x on ubuntu-latest
Details
Node v14.x on macOS-latest
Details
Node v14.x on windows-latest
Details
facebook.jest #20201012.22 failed
Details
Facebook CLA Check Contributor License Agreement is valid!
Details
ci/circleci: test-jest-circus Your tests passed on CircleCI!
Details
ci/circleci: test-node-10 Your tests passed on CircleCI!
Details
ci/circleci: test-node-12 Your tests passed on CircleCI!
Details
ci/circleci: test-node-13 Your tests passed on CircleCI!
Details
ci/circleci: test-node-14 Your tests passed on CircleCI!
Details
ci/circleci: test-or-deploy-website Your tests passed on CircleCI!
Details
@Gamote Gamote deleted the Gamote:feature/allow-ts-config-file branch Oct 21, 2020
johnmartel added a commit to johnmartel/organization-membership-action that referenced this pull request Oct 28, 2020
This brings auto-completion to Jest configuration.

See: facebook/jest#10564
See: facebook/jest#9636 (comment)
johnmartel added a commit to johnmartel/typescript-template that referenced this pull request Oct 28, 2020
This brings auto-completion to Jest configuration.

See: facebook/jest#10564
See: facebook/jest#9636 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

6 participants
You can’t perform that action at this time.