oke-py / npm-audit-action

GitHub Action to run `npm audit`
MIT License
41 stars 26 forks source link

`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 3 years ago

oke-py commented 3 years ago

I'd like to update strip-ansi to 7.0.0. strip-ansi@7.0.0 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.

> npm-audit-action@1.8.1 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.
tomprats commented 2 years ago

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

oke-py commented 2 years ago

@tomprats No. Could you help me?

tomprats commented 2 years ago

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

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

oke-py commented 2 years ago

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

theycallmeswift commented 3 months ago

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 commented 3 months ago

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

smypf commented 2 months ago

@theycallmeswift Perhaps this will help you? https://github.com/chalk/strip-ansi/releases/tag/v6.0.0