ts-jest: v23: require(...).install is not a function

ts-jest v23.0.0

If I revert back to the latest v22.4.6 and my tests work without issue.

 FAIL  __tests__/base.gun.spec.ts
  ● Test suite failed to run

    TypeError: require(...).install is not a function

    > 1 | import { myAPI } from '../src/myAPI'

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 20
  • Comments: 55 (2 by maintainers)

Most upvoted comments

@duncanhall you are using react-script-ts which has in its dependencies jest 22 and ts-jest 22. I might be wrong, but maybe it’s not compatible with jest and ts-jest version 23, which might be why your test fails when you switch to version 23 of ts-jest.

That’s really strange. I assume require(...).install is not a function is from the source-map-support library that uses an install hook to make sure the sourcemaps are gone.

However, we removed that in 23 and we’re using jests built-in source map support, so we’ve deleted the dependency entirely. It errors on line 1 because we’re prepending the install hook to the file - but it obviously can’t find it.

This is why I’m thinking it’s a cache issue, as somehow the old files don’t get the install hook removed, further supported by you not being able to reproduce on a clean repo.

At least that’s my theory.

Unsure why changing the jest version work though. I’m stil betting on a cache issue - try running the stuff here and maybe seeing if there’s both a global and a local cache.

I’m experiencing the same require(...).install is not a function issue since upgrading to ts-jest 23.0.0. Downgrading to the previous version fixes the issue.

I have made a minimum repo to reproduce the issue at https://github.com/duncanhall/ts-jest-issue-603 (CC: @ahnpnl @kulshekhar)

You can simply yarn install && yarn test to reproduce it. You can also then downgrade ts-jest to 22.4.2 and the issue goes away. More notes are available in the README

Clearing the jest cache makes absolutely no difference and from reading the responses above seems to be an entirely separate issue from require(...).install is not a function.

@GeeWee I just checked inside node_modules/ts-jest with v23 and found this image

I think the publishing process to npm had issues.

@huafu I want to share a tip about the caching issue that if you see your tests constantly repeat the same error via several runs, it’s better to run yarn jest --clearCache or npm run jest --clearCache. That will at least make sure that jest doesn’t cache the old codes and run with your correct code 😃

from looking at running react-scripts-ts test --env=jsdom "--showConfig" the version of jest is actually 20.0.4 and the version of ts-jest is probably different too.

I think you’re right.

@huafu Never mind, I think Gun 0.9.9994 is causing the issue somehow. When I reverted to .9.9993, the Jest tests are now all working. I’m not sure what ended up ultimately fixing this issue… might have been deleting node_modules and reinstalling.

https://github.com/amark/gun/issues/576

@krohrsb I don’t think so. The require(...).install was some code injected by the transformer.

I’ve ran both --no-cache and --clearCache with npm, but my problem still persists. Although the error seems to be a little different now.

  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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://facebook.github.io/jest/docs/en/configuration.html

    Details:

    /home/jdunlap/plus/github/finality/layer2storage/__tests__/base.gun.spec.ts:81
    import { GunStorageProxy } from '../src/layer2storage';
    ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

yup, also tried that 😕

Does this issue still appear if you run with --no-cache?

…at the end I decided to do one right now, will post t here when done

Hi,

Can you provide a minimum repo to reproduce your problem ? That would help a lot for us to debug 😃