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

chore: refactor Jest configuration to allow verbatimModuleSyntax #32

Merged
merged 2 commits into from
Aug 24, 2023
Merged

chore: refactor Jest configuration to allow verbatimModuleSyntax #32

merged 2 commits into from
Aug 24, 2023

Conversation

mrazauskas
Copy link
Contributor

Fixes #27

For reference see TypeScript documentation:

With [the verbatimModuleSyntax] option, what you see is what you get.

That does have some implications when it comes to module interop though. Under this flag, ECMAScript imports and exports won’t be rewritten to require calls when your settings or file extension implied a different module system. Instead, you’ll get an error. If you need to emit code that uses require and module.exports, you’ll have to use TypeScript’s module syntax that predates ES2015

The syntax in this PR follows the one seen in the documentation. That is the change needed to allow verbatimModuleSyntax: true. If you really need it.


const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest/presets/default-esm', // or other ESM presets
extensionsToTreatAsEsm: ['.ts'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests were failing in this package, because the extensionsToTreatAsEsm option was missing. I simply copied the options to make it similar to other files.

tsconfig.json Outdated
Comment on lines 34 to 35
"include": [
"src/**/*.ts"
]
} No newline at end of file
"include": ["**/src/**/*.ts"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently with "src/**/*.ts" no files are included and TS is complaining. Not sure what was the exact idea here. Perhaps "include": [] was intended?

Comment on lines -2 to -6
"ts-node": {
"moduleTypes": {
"jest.config.ts": "cjs"
}
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it does nothing in this setup.

export = jestConfig;
Copy link
Owner

Choose a reason for hiding this comment

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

What's the difference between export default & export equals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See documentation of the verbatimModuleSyntax option (link was provided in the description):

Screenshot 2023-08-24 at 05 45 54

@juanjoDiaz juanjoDiaz merged commit ceb328a into juanjoDiaz:main Aug 24, 2023
5 checks passed
@mrazauskas mrazauskas deleted the chore-jest-configuration branch August 24, 2023 17:06
@mrazauskas
Copy link
Contributor Author

mrazauskas commented Aug 24, 2023

@juanjoDiaz One detail which might be important. I was playing with verbatimModuleSyntax: true in one projects which is using const enums and has preserveConstEnums: true. In my case I was expecting enum members to be inlined at use site, but noticed that tsc does not do this if verbatimModuleSyntax: true.

Might be this is something you would like to double check. Reference: microsoft/TypeScript#52669 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move verbatimModuleSyntax to tsconfig once jest fix its issues
2 participants