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.

Jest: Getting unexpected token 'export' from `node_modules/botframework-streaming` (ts-jest)

See original GitHub issue

Versions

What package version of the SDK are you using.

└─┬ botframework-directlinejs@0.15.1
  └── botframework-streaming@4.14.1

What nodejs version are you using

 $» npm ls node -v
8.12.1

What browser version are you using What os are you using

MacOS Monterey 12.4

Describe the bug

In our project we include botframework-directlinejs@0.15.1 as dependency. When trying to execute jest

├── jest@28.1.3
└─┬ ts-jest@28.0.7

we’re getting

● 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:

    our-project/node_modules/botframework-streaming/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/botframework-streaming/src/utilities/protocol-base.ts:8:1)

To Reproduce

Steps to reproduce the behavior:

  1. install botframework-directlinejs@0.15.1
  2. install and configure jest using as preset ts-jest
  3. write any test
  4. got Unexpected token 'export' from node_modules/botframework-streaming/node_modules/uuid/dist/esm-browser/index.js:1

Expected behavior

node_modules/botframework-streaming/node_modules/uuid/dist/esm-browser/index.js:1 not causing trouble

Additional context

This is how we setup jest:

module.exports = {
  // An array of directory names to be searched recursively up from the requiring module's location
  // @see https://stackoverflow.com/a/51174924/1614677
  moduleDirectories: ["node_modules", "src"],

  // A preset that is used as a base for Jest's configuration
  preset: "ts-jest",

  // A list of paths to directories that Jest should use to search for files in
  roots: ["<rootDir>/src"],

  // A list of paths to modules that run some code to configure or set up the testing framework before each test
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],

  // The test environment that will be used for testing
  testEnvironment: "jsdom",

  // The glob patterns Jest uses to detect test files
  testMatch: ["**/?(*.)+(spec|test).[tj]s?(x)"],
};

We’ve also tried adding this:

  transformIgnorePatterns: [
    "node_modules/(?!(botframework-streaming)/)",
  ],

with no success.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
manuarterocommented, Aug 3, 2022

hi @ramfattah thanks for the effort.

I have been able to solve this and shame on me 🤦 this wasn’t caused by node_modules/botframework-streaming but node_modules/uuid in the end.

I mean the call trace is botframework-streaming ==> uuid (in the end is uuid who is causing the issue).

Sorry for the noise

Since we’ve already open this, let’s doc the thing (if this help someone)

👇

problem: Jest is raising:

   ----------------------------------------------
    SyntaxError: Unexpected token 'export'

    Details:
      ....
      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
      at Object.<anonymous> (node_modules/botframework-directlinejs/node_modules/botframework-streaming/src/utilities/protocol-base.ts:8:1)
   ----------------------------------------------

For solving this:

  1. Jest should parse the .js file using export
    transform: {
      '^.+\\.(t|j)s$': 'ts-jest',
    },

    transformIgnorePatterns: ['node_modules/(?!(uuid))']
  1. Our ts should be able to read .js files (tsconfig.json)
    "allowJs": true,

Our problem was that I was setting

    transformIgnorePatterns: ['node_modules/(?!(botframework-streaming))']

instead of

    transformIgnorePatterns: ['node_modules/(?!(uuid))']
1reaction
ramfattahcommented, Jul 29, 2022

Hi @manuartero, I’m investigating this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected token export, when import not compiled libraries ...
Issue : First of all, I'm not sure if this is a ts-jest issue or not, so sorry if I'm not reporting this...
Read more >
Jest gives an error: "SyntaxError: Unexpected token export"
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it...
Read more >
TypeScript Jest: Unexpected Token Export - Reddit
I'm trying to run unit tests for a TypeScript project which uses another TypeScript project I've created as a dependency.
Read more >
Jest SyntaxError: Unexpected token 'export'
This time I'm going to talk about a specific error: SyntaxError: Unexpected token 'export' .
Read more >
syntaxerror: unexpected token 'export' ts-jest - You.com
I'm getting SyntaxError: Unexpected token 'export' on some node_modules , which are not transpiled (I guess?). I worked around this by adding these...
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