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

Jest encountered an unexpected token after update #635

Open
serverlesspolska opened this issue Jun 27, 2022 · 9 comments
Open

Jest encountered an unexpected token after update #635

serverlesspolska opened this issue Jun 27, 2022 · 9 comments

Comments

@serverlesspolska
Copy link

After updating from v 1.1.1 to most recent 2.1.4 I got this 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.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/pawel/moje/serverless-hexagonal-template/node_modules/filter-obj/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default function filterObject(object, predicate) {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (../node_modules/aws-testing-library/src/common/dynamoDb.ts:1:1)

I've tried many different solutions fount on the internet but nothing seems to work.

My jest version is 28.1.1 and node is 14.

@erezrokah
Copy link
Owner

erezrokah commented Jun 27, 2022

Hi @serverlesspolska, thanks for opening the issue. This is related to Jest trying to transform packages under node_modules and failing since 2.1.4 uses an es6 module.

You can configure Jest not to do so via https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L2 and https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L8

@serverlesspolska
Copy link
Author

serverlesspolska commented Jun 28, 2022

Thank you @erezrokah

Unfortunately, that didn't help. I tried very similar setups already. I'm working on updating Serverless Framework template https://github.com/serverlesspolska/serverless-hexagonal-template

My configuration is a little bit more complex as I have 3 config types, each for different test types: unit, integration, and e2e. I use aws-testing-library in e2e tests only.

The original config that works with v1.1.1 is located here https://github.com/serverlesspolska/serverless-hexagonal-template/blob/main/config/e2e.jest.config.js.

Based on your comment, I modified it as follows:

const { config } = require('dotenv');
const { resolve } = require('path');

const esModules = ['aws-testing-library', 'filter-obj'].join('|');

module.exports = {
  testEnvironment: 'node',
  roots: ['../__tests__/'],
  testMatch: ['**/*.(e2e).js'],
  testTimeout: 60000 * 7, // 7 minutes timeout
  // Enable this if you're using aws-testing-library
  setupFilesAfterEnv: ['../node_modules/aws-testing-library/lib/jest/index.js'],
  preset: 'ts-jest/presets/js-with-ts',
  transformIgnorePatterns: [`../node_modules/(?!${esModules})`],
}

// Load environment variables generated by serverless-export-env plugin
config({
  path: resolve(__dirname, '../.awsenv'),
  bail: 1
})

And I also installed ts-jest lib. Still got the same error on node 14 and 16.
To be honest, I am little confused, why I needed it since I use pure JS.

@erezrokah could you please take a look at my project and suggest something?

@RaphaelManke
Copy link

Hi we fixed it by doing the following

  1. add Babel jest to the project https://jestjs.io/docs/getting-started#using-babel
  2. add to your jest config
transformIgnorePatterns: [
   `node_modules/(?!aws-testing-library|filter-obj)`,
],
transform: {
    "^.+\\.jsx?$": "babel-jest",
    "^.+\\.tsx?$": "ts-jest",
},

@AlessandroVol23
Copy link

I have the same issue. Locally it works with @RaphaelManke 's fix but in GitHub actions, it somehow doesn't. I have no idea atm.
I have my config in my own config file jest.config.ts not in the package.json

@AlessandroVol23
Copy link

Ok the last issue was because I missed the babel config due to a gitignore for JS files 🙄

It "works" now but I need to introduce babel only for this package and it gives me headaches for some other things like the MongoDB preset. Is there any way to get the aws-testing-library running without babel?

@erezrokah
Copy link
Owner

Hi all and thanks for the workaround @RaphaelManke. I think my suggestion in #635 (comment) does something similar via a preset:
https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L6
https://github.com/erezrokah/aws-testing-library-examples/blob/8cbffeb58725baa168a1d10fa8568369146cf3dc/jest.config.js#L6

This is a jest issue, see related issues in https://github.com/facebook/jest/issues?q=is%3Aissue+Jest+encountered+an+unexpected+token and specifically jestjs/jest#9430.

However I'll try to see if there's anything to be done on aws-testing-library side.
It will probably take me a week or so to look into this

@serverlesspolska
Copy link
Author

hey @erezrokah,

I just updated to "aws-testing-library": "^4.0.4" and have the same issue as half a year ago :-(

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.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/pawel/moje/cloudpouch-backend/node_modules/filter-obj/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default function filterObject(object, predicate) {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1449:14)
      at Object.<anonymous> (../node_modules/aws-testing-library/src/common/dynamoDb.ts:1:1)
      at Object.<anonymous> (../node_modules/aws-testing-library/src/jest/dynamoDb.ts:4:1)
      at Object.<anonymous> (../node_modules/aws-testing-library/src/jest/index.ts:6:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.911 s
Ran all test suites matching /send/i.

As others said, I don't want to introduce babel just for the sake of this issue.

Any suggestion on how to fix it?

@renanbym
Copy link

Any update around this?

@renanbym
Copy link

renanbym commented Mar 22, 2023

try to fix in PR #782

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

No branches or pull requests

5 participants