Jest + TypeScript run locally but not on circle

I have been hitting this error after upgrading to the latest version of TypeScript which includes new features like optional chaining, although the circle server is failing to run the tests.


  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    /home/circleci/project/src/components/Header/context.tsx:26
          color: value?.color || '',
                       ^

    SyntaxError: Unexpected token .

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (src/test-utils/index.tsx:19:1)

Locally on the machines here it works fine which is strange, even after rimrafing my local node_modules folder and reinstalled everything and they still pass locally.

I have set a new name for “CACHE_VERSION” to ensure it is including the changes I made in my yarn.lock file. I also ssh’d into the machine and ran the tests but they also do not run there.

Kinda stumped on where to go from here but also curious as to why it runs locally and not on circleci.

1 Like

I have the same problem like that. Do you know how to fix it now?

The error message in your post likely covers the issue.

Take a look at this Q&A: javascript - Unexpected token export with jest - Stack Overflow

There is likely a library that is being imported that is not javascript - and based on the filename referred to in the error output it looks to be a Typescript file - so modifying the transformIgnorePatterns value in your jest config file according to the SO post might solve the issue for you.