Code Monkey home page Code Monkey logo

ts-use-exports's Introduction

ts-use-exports

CircleCI npm

A TypeScript custom transformer that redirects function reference to the exports object.

When targeting CommonJS as the module target, the TypeScript compiler attach all exported functions to the module.exports object. However, any reference to those functions are not modified, causing unexpected results when one tries to stub these functions in unit-tests. Consider this module:

/** foobar.ts */

export function foo() {
  return "foo";
}

export function bar() {
  return foo() + "bar";
}

which transpiles to:

/** foobar.js */

function foo() {
  return "foo";
}
exports.foo = foo;
function bar() {
  return foo() + "bar";
}
exports.bar = bar;

Let's unit test this module. If foo is a function that triggers network activity or some other operation that we do not wish to exercise, it is common to stub it with a fake function.

/** test_foobar.js */

assert.strictEqual(foobar.bar(), "foobar"); // Pass

// Use SinonJS to stub out the foo function
sinon.replace(foobar, 'foo', sinon.fake.returns("bar"));

assert.strictEqual(foobar.bar(), "barbar"); // AssertionError! Actual: foobar

Sinon works by overwriting the reference to a value on an object. The second assert failed because the foo() call in bar is referencing the said function directly rather than through the module.exports object. This package provides a custom TypeScript transformer that emits the proper function call (ie exports.foo()) so that function stubbing works as expected.

Usage

First install the package

yarn add ts-use-exports
# or
npm install ts-use-exports

Depending on your toolchain, see one of the example setups below on using the custom transformer.

ts-node

ts-node by default does not provide a program instance to custom transformers. We will use ttypescript (install with yarn add ttypescript) to work around this limitation.

/** tsconfig.json */
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-use-exports" }
    ]
  }
}

Then invoke the ts-node binary with the ttypescipt compiler

ts-node --compiler ttypescript

Alternatively, if you use ts-node programmatically,

require('ts-node').register({
  project: 'tsconfig.json',
  compiler: 'ttypescript',
});

Webpack (with ts-loader / awesome-typescript-loader)

/** webpack.config.js */
const tsUseExports = require('ts-use-exports');

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader', // or 'awesome-typescript-loader'
        options: {
          getCustomTransformers: program => ({
            before: [tsUseExports(program)],
          }),
        },
      },
    ],
  },
};

Note

  • The module target must be CommonJS (either set explicity or implied by ES3/ES5 target).

  • The transformer currently does not produce the correct output if you alias a function name. See issue #3.

ts-use-exports's People

Contributors

tommyip avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

ts-use-exports's Issues

Function reference not calling the aliased function name.

function foo() {
    return "actually baz";
}

export function bar() {
    return foo() + "bar";
}

export { foo as baz };

transpile to

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function foo() {
    return "actually baz";
}
exports.baz = foo;
function bar() {
    return exports.foo() + "bar";
}
exports.bar = bar;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.