question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
ahnpnlcommented, Aug 19, 2021

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.

0reactions
wszgrcycommented, Aug 19, 2021

The error you get comes from the fact that when loading custom transformers, ts-jest uses require which only accepts js.

The workaround is you compile your transformer by tsc to js and use that js in your jest config for ts-jest, that should work.

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 .

require('ts-node').register({transformers});
module.exports = require('./jest-test-transformer');

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found