1.0.5 • Published 2 years ago

@appngo-sk/mocha-chai v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Mocha - Chai

Dependencies for testing typescript modules.

Installation

Install with npm

npm install --save-dev @appngo-sk/mocha-chai

or with yarn

yarn add -D @appngo-sk/mocha-chai

Run script

nyc mocha --checkLeaks \
    -r tsconfig-paths/register \
    -r ts-node/register \
    -r source-map-support/register \
    "test/**/*.test.ts"

Configuration

Create a configuration file .nycrc for nyc package inside of the project root directory with any of following extensions

File nameFile association
.nycrcJSON
.nycrc.jsonJSON
.nycrc.yamlYAML
.nycrc.ymlYAML
nyc.config.jsCommonJS export

The content of the file should extend this module.

{
  "extends": "@appngo-sk/mocha-chai"
}

Dependencies

{
  "@istanbuljs/nyc-config-typescript": "^1.0.1",
  "@types/chai": "^4.2.22",
  "@types/mocha": "^9.0.0",
  "@types/node": "^16.11.7",
  "chai": "^4.3.4",
  "mocha": "^9.1.3",
  "nyc": "^15.1.0",
  "source-map-support": "^0.5.20",
  "ts-node": "^10.4.0",
  "tsconfig-paths": "^3.11.0",
  "typescript": "^4.4.4"
}

Typescript


TypeScript is a language for application-scale JavaScript. TypeScript adds optional types to JavaScript that support tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to readable, standards-based JavaScript.

Mocha


Simple, flexible, fun JavaScript test framework for Node.js & The Browser.

Used to run tests

{
  "scripts": {
    "test": "mocha"
  }
}

Types

Used with Typescript.

Chai


Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework.

Types

Used with typescript.

nyc


JavaScript test coverage made simple. Istanbul's state of the art command line interface.

{
  "scripts": {
    "test": "mocha",
    "coverage": "nyc npm run test"
  }
}

Configuring nyc

The main export is a configuration json file for nyc.

{
  "extends": "@istanbuljs/nyc-config-typescript",
  "all": true,
  "check-coverage": true,
  "reporter": [
    "text",
    "html"
  ]
}
TypeScript projects

Please start with the pre-configured @istanbuljs/nyc-config-typescript preset.

Configuration options
Option nameDescriptionValueDefault
allWhether or not to instrument all files (not just the ones touched by your test suite)truefalse
check-coverageCheck whether coverage is within thresholds, fail if nottruefalse
reporterCoverage reporters to usetext, htmltext

nyc config typescript


Handy default configuration for instrumenting your TypeScript-backed project with test coverage using nyc.

.nycrc

{
  "extends": "@istanbuljs/nyc-config-typescript"
}

Source map support


This module provides source map support for stack traces in node via the V8 stack trace API. It uses the source-map module to replace the paths and line numbers of source-mapped files with their original paths and line numbers. The output mimics node's stack trace format with the goal of making every compile-to-JS language more of a first-class citizen. Source maps are completely general (not specific to any one language) so you can use source maps with multiple compile-to-JS languages in the same node process.

ts-node


ts-node is a TypeScript execution engine and REPL for Node.js.

It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling. This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.js tools and libraries.

Use with mocha

mocha -r ts-node/register

Types

Types for node.

tsconfig-paths


Use this to load modules whose location is specified in the paths section of tsconfig.json. Both loading at run-time and via API are supported.

Typescript by default mimics the Node.js runtime resolution strategy of modules. But it also allows the use of path mapping, which allows arbitrary module paths (that doesn't start with "/" or ".") to be specified and mapped to physical paths in the filesystem. The typescript compiler can resolve these paths from tsconfig so it will compile OK. But if you then try to execute the compiled files with node (or ts-node), it will only look in the node_modules folders all the way up to the root of the filesystem and thus will not find the modules specified by paths in tsconfig.

If you require this package's tsconfig-paths/register module it will read the paths from tsconfig.json and convert node's module loading calls into to physical file paths that node can load.

Use with mocha and ts-node

You also have to specify a glob that includes .ts files because mocha looks after files with .js extension by default.

mocha -r ts-node/register -r tsconfig-paths/register "test/**/*.ts"