espower-typescript
espower-typescript is an instrumentation library that integrates `power-assert` with TypeScript projects, enabling rich, detailed assertion messages for test failures. It functions as a `mocha` `--require` hook, transforming TypeScript test files (`.ts`, `.tsx`) on the fly to inject power-assert's expressive assertions. The current stable version is 10.0.1. It aims to maintain compatibility with specific TypeScript versions (v2.7+ for v10.x) and Node.js environments (v10+), typically updating to align with major `ts-node` or `typescript` releases rather than a fixed cadence. Its key differentiators include a zero-config mode, automatic `tsconfig.json` detection, internal reliance on `ts-node` for compilation, and support for JSX/React and `allowJs` configurations.
Common errors
-
Cannot find module 'typescript'
cause `typescript` is a peer dependency but is not installed in the project or its version is incompatible.fixInstall `typescript` as a devDependency: `npm install -D typescript`. -
TS2307: Cannot find module '@types/node'
cause Type checking is enabled by default, and `@types/node` (or other required `@types` packages) is missing.fixInstall the missing type definitions: `npm install -D @types/node`. -
AssertionError: Expected true to be false
cause This is a generic assertion error, indicating that `power-assert` instrumentation failed. A common cause is using `import assert from 'assert'` instead of `import assert = require('assert')`.fixEnsure you are using `import assert = require('assert')` for the `assert` module in your test files. -
Error: The 'mocha' command failed with exit code 1. You may need to update 'mocha' or 'espower-typescript'.
cause This can occur due to Node.js version incompatibility, especially with older Node.js versions no longer supported by `espower-typescript`.fixVerify your Node.js version is >=10.x. Update Node.js if necessary.
Warnings
- breaking espower-typescript v10.x and newer requires Node.js v10.x or higher. Older Node.js versions are no longer supported.
- breaking espower-typescript v10.x and newer requires TypeScript v2.7 or higher. Ensure your project's `typescript` peer dependency meets this requirement.
- breaking Since v9.0.0, `typescript` must be installed as a local dependency in your project (e.g., `npm install -D typescript`), rather than relying on global installations or internal transitive dependencies.
- breaking From v9.0.0, type checking is enabled by default internally via `ts-node`. This requires `@types/node` and `@types/mocha` (or similar `@types` packages for other test runners) to be installed as devDependencies.
- gotcha You MUST use `import assert = require('assert')` for Node.js's built-in `assert` module. Using `import assert from 'assert'` will prevent `power-assert` instrumentation from working correctly, resulting in generic assertion errors.
- breaking The internal `ts-node` dependency was updated from v8 to v9 in v10.0.0. Users relying on specific `ts-node` behaviors or configurations might need to review `ts-node`'s v9 release notes for compatibility.
Install
-
npm install espower-typescript -
yarn add espower-typescript -
pnpm add espower-typescript
Imports
- assert (Node.js built-in module)
import assert from 'assert';
import assert = require('assert'); - espower-typescript/guess (Mocha hook)
import { guess } from 'espower-typescript';mocha --require espower-typescript/guess
- @types/node
npm install -D @types/node
- @types/mocha
npm install -D @types/mocha
Quickstart
npm install -D espower-typescript power-assert mocha typescript @types/node @types/mocha
// test/test.ts
import assert = require('assert');
describe('Array#join', () => {
it('joins all elements into a string with separator', () => {
// This assertion is intentionally designed to fail to demonstrate power-assert's output.
assert(['a', 'b', 'c'].join(':') === 'a:b:c:');
});
});
// To run tests from your terminal:
// ./node_modules/.bin/mocha --require espower-typescript/guess "test/**/*.ts"
// Expected (failing) output will include detailed power-assert instrumentation:
// AssertionError [ERR_ASSERTION]: # test.ts:6
//
// assert(['a','b','c'].join(':') === 'a:b:c:')
// | | |
// ["a","b","c"] "a:b:c" false