Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

A way to migrate from older typescript compilers (3.7) to newer.

Notifications You must be signed in to change notification settings

snyk/babel-plugin-mutable-imports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-mutable-imports

Convert un-mockable exports back into mockable exports.

Typescript 3.9 broke this functionality on purpose, and have no intention of fixing it; people on older compilers can just stay there until they have switched to a different style of mocking.

Usage

You can use this inside ts-jest:

module.exports = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      babelConfig: {
        // in real code, use '@snyk/babel-plugin-mutable-imports'
        plugins: ['../..']
      }
    }
  }
};

What does it actually do?

Replace:

Object.defineProperty(exports, "foo", {
  enumerable: true,
  get: function () { return b_1.foo; }
});

...with:

exports.foo = b_1.foo;

(actually exports["foo"] because of laziness)

What does it fix?

TypeError: Cannot set property foo of [object Object] which has only a getter

Some Typescript constructs, such as export const and export { foo } from './bar', now generate immutable exports, for which you get a runtime error (or silent failure) when trying to mock.

A broken project, with as any as the mocking framework, is provided in examples/:

  • ts38: the code works on Typescript 3.8.
  • ts39: the code fails on Typescript 3.9.
  • ts39-fixed: this plugin fixes it on typescript 3.9.

About

A way to migrate from older typescript compilers (3.7) to newer.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published