npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-transform-test-compiler

v1.2.0

Published

Compiler API for testing TypeScript transformers

Downloads

154

Readme

npm package License Build Status Coverage Status Issues

ts-transform-test-compiler - Compiler API for testing TypeScript transformers

This package provides a simple API to make some basic TypeScript compilation using a given transformer. The purpose of this package is not to ease the use of transformers, but only to be included in a transformer test suite. For usage examples, you can look at:

Language/langue

Documents, messages, code (including variable names and comments), are in English.

Anyway, because Slune is French firm, all documents and important messages must also be provided in French. Other translations are welcome.

:fr: Une version française de ce document se trouve ici.

Installation

Installation is done using npm install command:

$ npm install --save-dev ts-transform-test-compiler

Usage

First thing to do is to create a compiler object. This compiler might be shared across the test suite as it only holds static configuration.

Then use this compiler object to set some options, if needed, and finally compile tested classes with the transformer.

import Compiler from 'ts-transform-test-compiler'
import myTransform from '.'

describe('My test suite', function () {
  const compiler = new Compiler(myTransform, 'dist/__test__').setRootDir('__test__')

  it('should do what I expect', function () {
    const transformParams = { astring: 'hello', anumber: 12 }
    const result = compiler.setSourceFiles('test1/').compile('test1', transformParams)
    expect(result.succeeded).to.be.true
    expect(result.requireContent()).to.equal('Working!')
  })
})

API

Compiler

constructor(transformer: Transformer, outDir: string, compilerOptions?: CompilerOptions)

Create a compiler for the given transformer.

The outDir parameter is the place where compiled files will be emitted. Beware that actually, each compilation is emitting files in its own directory, created under outDir and with the name of the current test as given in the compile() method.

The compiler options are optional. A sensible default set is used if none provided. The rootDir and outDir parameters are calculated at each compilation. Setting a value in the options for those parameters is then useless.

setTransformerHook(hook: keyof CustomTransformers): this

Call this method to set the hook to use for the transformer (i.e. the phase at which the transformer is called). If this method is not called, the default hook used is before.

This method modifies the compiler object. You may call it once (e.g. at compiler creation time) or many times (e.g. before each test).

This method returns this, allowing it to be chained.

setSourceFiles(sourceFiles: string, ignoredFiles?: string | string[]): this

Call this method to set the files to be used as sources. You may only specify entry points as the compiler will automatically require other needed files. All provided files must reside under the project root directory, which by default is the current directory.

The sourceFiles can either be a directory (ending with a / and not containing any glob magic character) or a glob pattern. If it is a directory, **/*.ts will be appended. In both cases, the sourceFiles value will be prefixed by the project root directory. Default value is / which takes all .ts files under the project root directory.

If the provided glob pattern will take unwanted files, you can remove them by setting ignoredFiles. The value provided here will be directly given to the ignore option of glob.

This method modifies the compiler object. You may call it once (e.g. at compiler creation time) or many times (e.g. before each test).

This method returns this, allowing it to be chained.

setRootDir(rootDir: string | undefined): this

Set (or remove, if no parameter given) the root directory of the project. If no root directory is set, the compiler will use the current directory.

This method modifies the compiler object. You may call it once (e.g. at compiler creation time) or many times (e.g. before each test).

This method returns this, allowing it to be chained.

clearGlobCaches(): this

This method clears the cache used by glob to search for input files. You should only call it if you have problems.

This method returns this, allowing it to be chained.

compile(name: string, configuration: any): CompilationResult

Compile the files using the transformer. The name given to the compilation will be used to create a sub-directory under the outDir to emit the files. It will prevent all tests to emit files in the same place. You may, of course, give the same name to many tests, although not recommended.

The configuration parameter is the configuration to give to the transformer.

The compilation returns a compilation result object.

CompilationResult

succeeded: boolean

Indicate if compilation succeeded, i.e. processed without error.

print(): void

Print the diagnostic messages on standard output.

requireContent(file?: string, variable?: string): any

Require the content of a compiled file.

If file is not specified, the method will directly require the output directory, so there should be an index file there.

If variable is provided, then the method will return the content of this exported variable, instead of the default export.

defaultTsConfig

The default configuration used by the compiler is exported as an immutable object named defaultTsConfig so that you may override it instead of fully rewriting it.

Contributing

Even though we cannot guarantee a response time, please feel free to file an issue if you have any question or problem using the package.

Pull Requests are welcome. You can, of course, submit corrections or improvements for code, but do not hesitate to also improve documentation, even for small spell or grammar errors.