Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

@neutrinojs/jest doesn't work transformer overrides for typescript #1044

Closed
AlanFoster opened this issue Aug 25, 2018 · 6 comments
Closed

@neutrinojs/jest doesn't work transformer overrides for typescript #1044

AlanFoster opened this issue Aug 25, 2018 · 6 comments

Comments

@AlanFoster
Copy link
Contributor

AlanFoster commented Aug 25, 2018

Bug or issue?

Bug

Please try to answer the following questions:

  • What version of Neutrino are you using?

8.3.0

  • Are you trying to use any presets? If so, which ones, and what versions?

"@neutrinojs/airbnb-base": "^8.3.0",
"@neutrinojs/jest": "^8.3.0",
"@neutrinojs/node": "^8.3.0",

  • Are you using the Yarn client or the npm client? What version?

Yarn 1.9.4

  • What version of Node.js are you using?

Node v8.9.4

  • What operating system are you using?

macOS Sierra 10.12.6

  • What did you do?

Attempted to run Jest with typescript a file called __tests__/index.spec.ts with the following configuration to allow for typescript overrides:

    [
      '@neutrinojs/jest',
      {
        "transform": {
          "^.+\\.tsx?$": "ts-jest"
        },
        "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
        "moduleFileExtensions": [
          "ts",
          "tsx",
          "js",
          "jsx",
          "json",
          "node"
        ]
      }
    ],
  • What did you expect to happen?

The jest configuration would be valid for typescript support, and my test would be picked up correctly.

  • What actually happened, contrary to your expectations?

The typescript transformer doesn't get picked up and instead the default transformer fails:

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
          at Generator.next (<anonymous>)
          at new Promise (<anonymous>)
          at Generator.next (<anonymous>)

Inspecting the generated jest config file within my operating system's tmp directory I can see the transform value has been set:

  "transform": {
    "\\.(js|jsx|vue|ts|tsx|mjs)$": "/Users/foo/test_neutrino/node_modules/@neutrinojs/jest/src/transformer.js",
    "^.+\\.tsx?$": "ts-jest"
  },

Upon further inspection I can see why the default transform is still being used instead of the expected ts-jest implementation:

# @neutrinojs/jest/src/index.js

  const transformNames = `\\.(${neutrino.options.extensions.join('|')})$`; // This includes ts already.

  // ...

  // Deep merged:
  return merge.all([
    {
      // ...
      transform: { [transformNames]: require.resolve('./transformer') },
      // ...
    },
    opts
  ]);

Current workaround

I am currently working around this with the following "fix":

    // Remove typescript from the default extension list before Jest runs,
    // as it generates invalid configuration otherwise
    (neutrino) => {
      neutrino.options.extensions = [ 'js', 'jsx', 'vue', 'mjs' ];
    },

    [
      '@neutrinojs/jest',
      {
        "transform": {
          "^.+\\.tsx?$": "ts-jest"
        },
        "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
        "moduleFileExtensions": [
          "ts",
          "tsx",
          "js",
          "jsx",
          "json",
          "node"
        ]
      }
    ],

Which now gives me a jest configuration file that works:

  "transform": {
    "\\.(js|jsx|vue|mjs)$": "/Users/foo/test_neutrino/node_modules/@neutrinojs/jest/src/transformer.js",
    "^.+\\.tsx?$": "ts-jest"
  },

And Jest correctly uses the typescript transformer:

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.476s
@AlanFoster AlanFoster changed the title @neutrinojs/jest doesn't work with ts-jest @neutrinojs/jest doesn't work transformer overrides for typescript Aug 25, 2018
@edmorley
Copy link
Member

edmorley commented Aug 31, 2018

Hi!

I'm not overly familiar with TypeScript (on the ever-growing list of things to try out with existing projects), so this might be a daft question - but is there a specific reason for using ts-jest vs adding the @babel/preset-typescript preset to the existing Babel configuration used by @neutrinojs/jest's default transformer? This could be achieved via the @neutrinojs/react-components's babel option (see web preset docs).

@eliperelman
Copy link
Member

@edmorley would you consider this issue resolved?

@AlanFoster
Copy link
Contributor Author

Thanks - I'll grab the latest neutrino release, and I'll try the suggestion out and report back shortly on the outcome 👍

@AlanFoster
Copy link
Contributor Author

Hm, I'm afraid I've still had no luck with an alternative setup.

I believe that there the original bug noted by this issue still exists, i.e. the merging functionality of Jest, but I'll try this again once the latest version of neutrino is released and create separate ticket if I spot any other issues 👍

Thanks everyone!

@eliperelman
Copy link
Member

@AlanFoster correct, this issue isn't resolved in the latest release, it would be for the next version.

@edmorley
Copy link
Member

@AlanFoster, Hi! We've just released a Neutrino 9 beta, if you'd like to try it out? See #1129.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants