Lab Transform TypeScript
lab-transform-typescript is a utility that enables the `lab` testing framework to directly execute TypeScript test files without requiring a separate pre-compilation step. This allows for a more streamlined `npm test` workflow for projects using TypeScript with `lab`. The current stable version is 3.0.1, which includes inlined source maps for better error reporting. The package's release cadence is not fixed, typically driven by updates to the `lab` framework or `typescript` itself. Key differentiators include its tight integration with `lab`, support for source maps, and the provision of optional type definitions for `lab` and `code` (the assertion library used with `lab`), enhancing the development experience for TypeScript users within the `lab` ecosystem. It leverages TypeScript's official configuration loader and parser since version 3.0.0.
Common errors
-
Error: Cannot find module 'typescript' from 'node_modules/lab-transform-typescript'
cause `typescript` is a peer dependency and was not installed in the consuming project.fix`npm install typescript --save-dev` to add `typescript` to your project's development dependencies. -
SyntaxError: Unexpected token 'import' (or 'export') when running tests.
cause This typically occurs if `lab` is trying to run a TypeScript file directly without the transform, or if the transform is misconfigured.fixEnsure `lab --transform node_modules/lab-transform-typescript` is correctly specified in your test command and that the path to the transform is accurate. Also, ensure `lab` itself is configured to process the correct file extensions (e.g., `.ts`). -
TSCONFIG environment variable ignored or tsconfig.json not found.
cause The `TSCONFIG` environment variable might be incorrectly set, or TypeScript's default `tsconfig.json` search (which the transform uses since v3.0.0) isn't finding the desired file.fixDouble-check the path provided to `TSCONFIG`. Ensure the `tsconfig.json` file exists at the specified path or in a location where TypeScript's default search mechanism would find it (e.g., project root). Ensure you are on `lab-transform-typescript` v3 or newer for this feature.
Warnings
- breaking Version 3.0.0 switched to TypeScript's official configuration loader and parser. This might affect how `tsconfig.json` files are resolved and parsed, especially in complex project setups.
- breaking Version 2.0.0 moved `typescript` from `dependencies` to `peerDependencies`. This means `typescript` is no longer automatically installed by `lab-transform-typescript` and must be explicitly installed by the consuming project.
- gotcha When using `lab --coverage` with `lab-transform-typescript`, the coverage report may display transpiled JavaScript code instead of the original TypeScript source, making it harder to interpret results in terms of the original source.
- gotcha The version of `typescript` used by the transform depends on how `lab-transform-typescript` itself is installed. If linked via `npm link`, it uses its own `node_modules` `typescript` version; otherwise, it uses the `typescript` from the project's `node_modules`.
Install
-
npm install lab-transform-typescript -
yarn add lab-transform-typescript -
pnpm add lab-transform-typescript
Imports
- transformFunction
import transform from 'lab-transform-typescript';
const transform = require('lab-transform-typescript'); - LabCLIUsage
/* No JS import; used via CLI: */ lab --sourcemaps --transform node_modules/lab-transform-typescript
- TypeScriptTypes
import { LabTypings } from 'lab-transform-typescript/typings-local/lab.d.ts'/* No direct JS import for these types; they are installed separately */
Quickstart
npm install --save-dev lab-transform-typescript typescript
// In a TypeScript test file (e.g., test/example.test.ts):
// import { expect } from '@hapi/code';
// import { describe, it } from '@hapi/lab';
// describe('My Test Suite', () => {
// it('should pass', () => {
// expect(1 + 1).to.equal(2);
// });
// });
// To run tests, execute in your terminal:
// (Ensure 'lab' is installed globally or in node_modules/.bin)
process.env.TSCONFIG = process.env.TSCONFIG ?? './tsconfig.json'; // Optional: specify tsconfig path
console.log(`Running tests with lab transform and TSCONFIG: ${process.env.TSCONFIG}`);
// Execute this command via package.json 'test' script or directly if lab is global:
// lab --sourcemaps --transform node_modules/lab-transform-typescript --verbose
// To make it runnable in a script:
// const { spawn } = require('child_process');
// const lab = spawn('npx', [
// 'lab',
// '--sourcemaps',
// '--transform',
// 'node_modules/lab-transform-typescript',
// '--verbose',
// 'test/**/*.ts' // Example glob for test files
// ], { stdio: 'inherit' });
// lab.on('close', (code) => process.exit(code));