astTransformers use typescript plugin throw error
See original GitHub issue🐛 Bug Report
config
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json',
astTransformers: {
before: ['./test/util/jest-test-transformer.ts'],
},
},
},
run with error
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/chen/my-project/static-injector/test/util/jest-test-transformer.ts:1
import { SourceFile, TransformationContext } from 'typescript';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at resolveTransformerFunc_1 (node_modules/ts-jest/dist/config/config-set.js:223:39)
at node_modules/ts-jest/dist/config/config-set.js:235:32
at Array.map (<anonymous>)
when not add transformer, test is work.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
transformer can use with typescript
Link to repo (highly encouraged)
Debug log:
too long.....
envinfo
System:
OS: mac
Node version: v14.17.1
Npm packages:
jest:27.0.6
ts-jest:27.0.5
typescript:4.3.5
babel(optional):none
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
AST transformers option | ts-jest - GitHub Pages
The option is astTransformers and it allows ones to specify which 3 types of TypeScript AST transformers to use with ts-jest :.
Read more >Code Transformation - Jest
A transformer is a module that provides a method for transforming source files. For example, if you wanted to be able to use...
Read more >ts-jest | Yarn - Package Manager
A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript. NPM version NPM downloads Known...
Read more >Having fun with Angular and Typescript Transformers
(!angularCompilerPlugin) { console.error('Could not inject the typescript transformer: Webpack AngularCompilerPlugin not found'); return; } ...
Read more >jest-preset-angular - npm
Start using jest-preset-angular in your project by running `npm i ... AST Transformers; Angular Testing Environment Setup; Snapshot Testing.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ye that way or compile to js should work.
ts-jest
is a Jest transformer so we only compile what Jest gives to us. In this case, the error wasn’t thrown out at startup step which Jest doesn’t send the transformer file yet.We never support ts transformer before so it is a feature/bug.
But I think
ts-jest
is use typescript file to test, so transformer should support typescript file Now I use ts-node to make it work temporary .