zentered / issue-forms-body-parser

GitHub Issue Forms Body Parser Action
MIT License
20 stars 7 forks source link

Feature request: Support for TS + Jest as a dependency #44

Closed benlei closed 1 month ago

benlei commented 1 month ago

Is your feature request related to a problem? Please describe.

I was trying to create a TS action that used this dependency, but it's missing a declaration file... but even if I were to declare a type, the import seems to fail for some reason.

I created an example repo where you can also test it out: https://github.com/benlei/issue-forms-body-parser-bug

Commands ran:

npm install
npx jest

Error Observed:

 FAIL  __tests__/index.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/blei/workspace/issue-forms-body-parser-bug/node_modules/unified/index.js:2
    export {unified} from './lib/index.js'
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import * as core from '@actions/core'
    > 2 | import bodyParser from '@zentered/issue-forms-body-parser'
        | ^
      3 | import { wait } from './wait'
      4 |
      5 | /**

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@zentered/issue-forms-body-parser/pkg/parse.cjs:1:109)
      at Object.require (src/main.ts:2:1)
      at Object.<anonymous> (__tests__/index.test.ts:5:1)

Describe the solution you would like

I would like to be able to use your dependency in my TS action that I created through GitHub's own TS action template. With that being said, I may also be creating TS applications that will need to also use this dependency that'll also likely run into the same error.

Describe alternatives you have considered

I can pass the data output of your action to my own action/step to export the outputs... but if I have many workflows + need to update one/both of those steps I'd have to update all the workflows too. Would be more convenient if I can just access outputs directly.

Plus if I were to create a TS application... that wouldn't be possible.

Additional context

It's probably the case that this can't really be made to work with CommonJS projects... but at the very least if types can be defined/provided, that'd be great.

PatrickHeneise commented 1 month ago

Thanks @benlei.

I'm not sure about your TypeScript configuration and jest. We use the native test runner and it's working fine.

Unexpected token 'export' isn't really a code issue but suggests there's an issue with the setup. I put together a small gist with a TypeScript config that works here:

https://gist.github.com/PatrickHeneise/c21aba158085b5dd1165b6239453c8f8

Feel free to add a d.ts file in a pull request