npm-audit-action icon indicating copy to clipboard operation
npm-audit-action copied to clipboard

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

Open oke-py opened this issue 4 years ago • 7 comments

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 avatar Oct 11 '21 12:10 oke-py

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

tomprats avatar Mar 16 '22 16:03 tomprats

@tomprats No. Could you help me?

oke-py avatar Mar 17 '22 08:03 oke-py

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

tomprats avatar Mar 17 '22 16:03 tomprats

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

oke-py avatar Mar 18 '22 10:03 oke-py