Giter Club home page Giter Club logo

tsc-test's Introduction

Build Status

tsc-test

Testing TypeScript compilation (should succeed or should fail)

Basic usage

  1. put tsconfig.json into test directory.

    "noEmit" is recommended.

    {
        "compilerOptions": {
            "target": "es5",
            "module": "commonjs",
            "noEmit": true,
            "strictNullChecks": true
        },
        "filesGlob": [
            "*.ts"
        ],
        "exclude": [
            "node_modules"
        ]
    }
  2. put files to be tested into test directory.

    // should-succeed.ts
    
    interface Foo { foo: string; }
    
    export function test(foo: Foo) {
        console.log(foo.foo);
    }

    When compilation errors should be occurred, write error code inline after 4 slashes(////).

    // should-fail-1.ts
    
    interface Foo { foo: string; }
    
    export function test(foo: Foo) {
        console.log(foo.bar);       //// TS2339
    }

    You can write part of error message after error code. (with colon)

    // should-fail-2.ts
    
    interface Foo { foo: string; }
    
    export function test(foo: Foo) {
        console.log(foo.bar);       //// TS2339: Property 'bar' does not exist on
    }

    Or regular expression.

    // should-fail-3.ts (but this code is not wrong acturally ...)
    
    interface Foo { foo: string; }
    
    export function test(foo: Foo) {
        console.log(foo.foo);       //// TS2339: /property .* does not exist/i
    }
  3. run tsc-test command

    $ tsc-test -p test/tsconfig.json
    

    And you will get output like below:

    OK: test/should-fail-1.ts
    OK: test/should-fail-2.ts
    NG: test/should-fail-3.ts
    OK: test/should-succeed.ts
    
    test/should-fail-3.ts:6
      expected: TS2339: /property .* does not exist/i
      bat was:  <no error>
    

Run with test runner

ava

// runner.ts

import test from "ava";
import { Tester, formatFailureMessage } from "tsc-test";

const tester = Tester.fromConfig("<path to tsconfig.json>");
tester.sources.forEach(fileName => {
    test(fileName, t => {
        const failures = tester.test(fileName);
        if (failures.length > 0) {
            t.fail(formatFailureMessage(...failures));
        }
    })
});

License

MIT

tsc-test's People

Contributors

dependabot[bot] avatar wonderful-panda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.