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

npm run test fails due to 'Jest encountered an unexpected token' when strip-ansi is updated to 7.0.0 #98

Open
oke-py opened this issue Oct 11, 2021 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@oke-py
Copy link
Owner

oke-py commented Oct 11, 2021

I'd like to update strip-ansi to 7.0.0.
[email protected] is pure ESM, and so the way using the module has changed.

reproduce

Edit the package.json as below.

diff --git a/package.json b/package.json
index 622a76c..499914b 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,7 @@
     "@actions/github": "^5.0.0",
     "@octokit/rest": "^18.12.0",
     "axios": "^0.21.4",
-    "strip-ansi": "^6.0.1"
+    "strip-ansi": "^7.0.0"
   },
   "devDependencies": {
     "@types/jest": "^27.0.2",

Run npm i and npm run test.

> [email protected] test
> jest

FAIL __tests__/main.test.ts
  ● 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/naoki/go/src/github.com/oke-py/npm-audit-action/node_modules/strip-ansi/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import ansiRegex from 'ansi-regex';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import {spawnSync, SpawnSyncReturns} from 'child_process'
    > 2 | import stripAnsi from 'strip-ansi'
        | ^
      3 |
      4 | const SPAWN_PROCESS_BUFFER_SIZE = 10485760 // 10MiB
      5 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1723:14)
      at Object.<anonymous> (src/audit.ts:2:1)

FAIL __tests__/audit.test.ts
  ● 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/naoki/go/src/github.com/oke-py/npm-audit-action/node_modules/strip-ansi/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import ansiRegex from 'ansi-regex';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import {spawnSync, SpawnSyncReturns} from 'child_process'
    > 2 | import stripAnsi from 'strip-ansi'
        | ^
      3 |
      4 | const SPAWN_PROCESS_BUFFER_SIZE = 10485760 // 10MiB
      5 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1723:14)
      at Object.<anonymous> (src/audit.ts:2:1)

PASS __tests__/workdir.test.ts
  isValid
    ✓ throws an error if working_directory starts with / (1 ms)
    ✓ throws an error if working_directory starts with ..

PASS __tests__/issue.test.ts
  getIssueOption
    ✓ without assignee and label (2 ms)
    ✓ with 1 assignee and 1 label (1 ms)
    ✓ with 2 assignees and 2 labels
    ✓ with label containing spaces
  getExistingIssueNumber
    ✓ gets existing open issue (1 ms)
    ✓ returns null when there is no open issue
    ✓ returns null when no issues match the issue title

Test Suites: 2 failed, 2 passed, 4 total
Tests:       9 passed, 9 total
Snapshots:   0 total
Time:        1.958 s, estimated 2 s
Ran all test suites.
@oke-py oke-py added the help wanted Extra attention is needed label Oct 11, 2021
@tomprats
Copy link

@oke-py Did you end up solving this issue?

@oke-py
Copy link
Owner Author

oke-py commented Mar 17, 2022

@tomprats No. Could you help me?

@tomprats
Copy link

I made a ton of changes and can't remember exactly which one did it but here's a summary of what I did:

  • In the jest config in moduleDirectories: [ I changed "<rootDir>/node_modules", to "node_modules",
  • Locked my versions to "babel-jest": "^27.5.1", and "jest": "^27.5.1",
  • Upgraded nodejs 14.3.0 to nodejs 14.18.0
  • Upgraded a lot of dependencies in package-lock.json

Sorry I can't be more specific, but I think it was one of those

@oke-py
Copy link
Owner Author

oke-py commented Mar 18, 2022

Thanks. I'll update nodejs to 16.x and then look into this issue.

@theycallmeswift
Copy link

I know this is two years later, but I'm also encountering this error. Did either of you ever discover a concrete workaround @oke-py / @tomprats?

@tomprats
Copy link

My best guess is that updating node fixed it, but I'm not sure

@smypf
Copy link

smypf commented Aug 7, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants