Giter VIP home page Giter VIP logo

ts-mocha's Introduction

Latest Stable Version NPM Downloads NPM Downloads

Codeship Status for piotrwitek/ts-mocha Greenkeeper badge Dependency Status peerDependency Status

TS-Mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity. All Mocha features are available without any limitation.

Why?

To setup Mocha with TypeScript you have to figure out how to integrate them to work together, it's not an easy task and require some advanced knowledge. This package handles for you this complexity and let you use ts-mocha just as regular mocha that will handle TypeScript .ts and .tsx files. Also added some useful options, you can find them below.

How?

TS-Mocha has one only dependency - ts-node, which is used as a TypeScript runtime to execute tests that can import and run imported TypeScript source files as well. It is as a thin wrapper that run local mocha package and set up ts-node environment to handle .ts and .tsx files. To speed up TypeScript tests execution type-checking is disabled, using only transpile module.

NOTE: This package does not include Mocha - I have set Mocha as peer dependency on purpose, so I don't lock consumer to a specific Mocha version but most importantly, I don't have to update this package when Mocha is updated, and all the new features will be available automatically from your local Mocha package. Also integration with your existing Mocha setup is non-invasive.

PRO TIP: To make your developer experience better I recommend to run type-checking in a separate process by starting TSC compiler (preferably in watch mode) in you terminal with --noEmit and --project flags.

Installation

# remember to install mocha if you don't have it already (npm i -D mocha)

npm i -D ts-mocha

# install recent Mocha and Expect @types packages for best DX
npm i -D @types/mocha @types/expect

Usage

- CLI Usage:

CLI options consist of all the options of regular Mocha plus extra options below:

-p, --project <value> - relative or absolute path to a tsconfig.json file (equivalent of tsc -p <value>) [default: "./tsconfig.json"]

Example:

ts-mocha -p src/tsconfig.json src/**/*.spec.ts

--paths - feature toggle flag to enable tsconfig-paths integration [default: false]

tsconfig-paths is an optional dependency, make sure to install it locally in your project

When using path mapping via the paths compiler option in tsconfig.json this library utilizes the tsconfig-paths package, allowing for automatic resolution of aliased modules locations during test execution.

Check our test suite for a reference implementation: Link

Example:

ts-mocha --paths -p src/ src/**/*.spec.ts

Watch Mode:

If you want your tests to be automatically rerun when your code changes, add both the -w flag and the --watch-extensions flag telling it to watch for typescript files.

Example:

ts-mocha --paths -p src/ src/**/*.spec.ts -w --watch-extensions ts

- Programmatic usage:

In code you can use ts-mocha by adding a single require at the beginning of your script:

// set env variable to the `tsconfig.json` path before loading mocha (default: './tsconfig.json')
process.env.TS_NODE_PROJECT = './src/tsconfig.json'

// Optional: set env variable to enable `tsconfig-paths` integration
process.env.TS_CONFIG_PATHS = true;

// register mocha wrapper
require('ts-mocha');

For example:

process.env.TS_NODE_PROJECT = './src/tsconfig.json';
require('ts-mocha');
const Mocha = require('mocha');

const mocha = new Mocha();
mocha.addFile(`./src/file.spec.ts`);
mocha.run((failures) => {
  process.on('exit', () => {
    process.exit(failures); // exit with non-zero status if there were failures
  });
});

ts-mocha's People

Contributors

piotrwitek avatar greenkeeper[bot] avatar strantr avatar joelharkes avatar jmfederico avatar gfranco93 avatar jimlynchcodes avatar

Watchers

James Cloos avatar

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.