├── .circleci └── config.yml ├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── .husky └── pre-commit ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── docs ├── _config.yml └── index.md ├── package-dist.json ├── package.json ├── scripts └── pack.js ├── source ├── ava │ └── index.ts ├── compiler-spec.ts ├── compiler.ts ├── expect.ts ├── expecter-spec.ts ├── expecter.ts ├── index.ts ├── placeholders-spec.ts ├── placeholders.ts ├── snippet-spec.ts ├── snippet.ts ├── tape │ └── index.ts └── timeout-spec.ts ├── tsconfig-dist-cjs.json ├── tsconfig-dist-es2015.json ├── tsconfig-dist-es5.json ├── tsconfig-dist.json ├── tsconfig.json ├── tslint.json └── yarn.lock /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | # https://circleci.com/docs/2.0/docker-image-tags.json 6 | - image: circleci/node:current 7 | steps: 8 | - checkout 9 | - run: 10 | name: Install Greenkeeper Lockfile 11 | command: | 12 | echo 'export PATH=$(yarn global bin):$PATH' >> $BASH_ENV 13 | source $BASH_ENV 14 | yarn global add greenkeeper-lockfile@1 15 | - run: 16 | name: Update Greenkeeper Lockfile 17 | command: "greenkeeper-lockfile-update" 18 | - restore_cache: 19 | name: Restore Yarn Package Cache 20 | keys: 21 | - yarn-packages-{{ checksum "yarn.lock" }} 22 | - run: 23 | name: Install Packages 24 | command: yarn install 25 | - save_cache: 26 | name: Save Yarn Package Cache 27 | key: yarn-packages-{{ checksum "yarn.lock" }} 28 | paths: 29 | - ~/.cache/yarn 30 | - run: 31 | name: Test 32 | command: yarn test 33 | - run: 34 | name: Upload Greenkeeper Lockfile 35 | command: "greenkeeper-lockfile-upload" 36 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_style = space 8 | indent_size = 2 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | indent_size = 4 14 | insert_final_newline = false 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [cartant] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /dist 2 | /node_modules 3 | /temp 4 | yarn-error.log 5 | -------------------------------------------------------------------------------- /.husky/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . "$(dirname "$0")/_/husky.sh" 3 | 4 | yarn lint-staged 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## [5.0.2](https://github.com/cartant/ts-snippet/compare/v5.0.1...v5.0.2) (2020-12-03) 3 | 4 | ### Changes 5 | 6 | * Indicate the absence of errors for failure expectations. ([70754b3](https://github.com/cartant/ts-snippet/commit/70754b3)) 7 | 8 | 9 | ## [5.0.1](https://github.com/cartant/ts-snippet/compare/v5.0.0...v5.0.1) (2020-11-21) 10 | 11 | ### Changes 12 | 13 | * Include the actual error(s) when a failure expectation's regular expression does not match. ([55778a5](https://github.com/cartant/ts-snippet/commit/55778a5)) 14 | 15 | 16 | ## [5.0.0](https://github.com/cartant/ts-snippet/compare/v4.3.0...v5.0.0) (2020-10-28) 17 | 18 | ### Breaking Changes 19 | 20 | * `infer`/`toInfer` implies `succeed`/`toSucceed` and will throw if there are compilation errors. ([54e0819](https://github.com/cartant/ts-snippet/commit/54e0819)) 21 | 22 | 23 | ## [4.3.0](https://github.com/cartant/ts-snippet/compare/v4.2.0...v4.3.0) (2020-08-28) 24 | 25 | ### Changes 26 | 27 | * Widen TypeScript peer range. ([df3ef4e](https://github.com/cartant/ts-snippet/commit/df3ef4e)) 28 | 29 | 30 | ## [4.2.0](https://github.com/cartant/ts-snippet/compare/v4.1.1...v4.2.0) (2019-04-22) 31 | 32 | ### Fixes 33 | 34 | * Add a `Compiler` signature to `expecter` to facilitate faster tests. ([a3d9058](https://github.com/cartant/ts-snippet/commit/a3d9058)) 35 | 36 | 37 | ## [4.1.1](https://github.com/cartant/ts-snippet/compare/v4.1.0...v4.1.1) (2019-04-22) 38 | 39 | ### Fixes 40 | 41 | * Don't throw a TypeScript `Diagnostic` for options-related errors. ([0467d0e](https://github.com/cartant/ts-snippet/commit/0467d0e)) 42 | 43 | 44 | ## [4.1.0](https://github.com/cartant/ts-snippet/compare/v4.0.0...v4.1.0) (2019-03-28) 45 | 46 | ### Features 47 | 48 | * Added a `rootDirectory` option. ([b8c6411](https://github.com/cartant/ts-snippet/commit/b8c6411)) 49 | 50 | 51 | ## [4.0.0](https://github.com/cartant/ts-snippet/compare/v3.1.2...v4.0.0) (2018-12-16) 52 | 53 | ### Breaking Changes 54 | 55 | * Upgrade to AVA 1.0. ([3299ee3](https://github.com/cartant/ts-snippet/commit/3299ee3)) 56 | 57 | 58 | ## [3.1.2](https://github.com/cartant/ts-snippet/compare/v3.1.1...v3.1.2) (2018-07-31) 59 | 60 | ### Build 61 | 62 | * Widen TypeScript peer semver to allow for version 3.0. ([c036f9e](https://github.com/cartant/ts-snippet/commit/c036f9e)) 63 | 64 | 65 | ## [3.1.1](https://github.com/cartant/ts-snippet/compare/v3.1.0...v3.1.1) (2018-04-28) 66 | 67 | ### Fixes 68 | 69 | * **placeholders**: Add `T0` to `placeholders.ts`, etc. ([b54ba02](https://github.com/cartant/ts-snippet/commit/b54ba02)) 70 | 71 | 72 | ## [3.1.0](https://github.com/cartant/ts-snippet/compare/v3.0.0...v3.1.0) (2018-04-28) 73 | 74 | ### Features 75 | 76 | * **placeholders**: Add `placeholders.ts` so that snippets can import pre-declared placeholder types, constants and variables. ([a95f9a0](https://github.com/cartant/ts-snippet/commit/a95f9a0)) 77 | 78 | 79 | ## [3.0.0](https://github.com/cartant/ts-snippet/compare/v2.1.0...v3.0.0) (2018-04-01) 80 | 81 | ### Breaking Changes 82 | 83 | * **TypeScript:** Drop support for TypeScript 2.0. ([8f19247](https://github.com/cartant/ts-snippet/commit/8f19247)) 84 | * **expecter:** Rename `reuseCompiler` to `expecter`. ([0df6415](https://github.com/cartant/ts-snippet/commit/0df6415)) 85 | 86 | 87 | ## [2.1.0](https://github.com/cartant/ts-snippet/compare/v2.0.1...v2.1.0) (2018-03-30) 88 | 89 | ### Features 90 | 91 | * Add `reuseCompiler` to simplify use. ([90a6bce](https://github.com/cartant/ts-snippet/commit/90a6bce)) 92 | 93 | 94 | ## [2.0.1](https://github.com/cartant/ts-snippet/compare/v2.0.0...v2.0.1) (2018-01-31) 95 | 96 | ### Fixes 97 | 98 | * The `infer` expectation now supports variables in nested scopes. ([fa054bd](https://github.com/cartant/ts-snippet/commit/fa054bd)) 99 | 100 | ### Changes 101 | 102 | * The distribution now includes CommonJS, ES5 and ES2015 files. 103 | 104 | 105 | ## [2.0.0](https://github.com/cartant/ts-snippet/compare/v1.0.2...v2.0.0) (2017-11-03) 106 | 107 | ### Breaking Changes 108 | 109 | * **Compiler:** The compiler now takes JSON options rather than options that use TypeScript's enums, etc. ([71d93a4](https://github.com/cartant/ts-snippet/commit/71d93a4)) -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | There's nothing here, yet. Thanks for looking. I'll eventually get around to filling this out. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017-2018 Nicholas Jamieson and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ts-snippet 2 | 3 | [![GitHub License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cartant/ts-snippet/blob/master/LICENSE) 4 | [![NPM version](https://img.shields.io/npm/v/ts-snippet.svg)](https://www.npmjs.com/package/ts-snippet) 5 | [![Build status](https://img.shields.io/travis/cartant/ts-snippet.svg)](http://travis-ci.org/cartant/ts-snippet) 6 | [![dependency status](https://img.shields.io/david/cartant/ts-snippet.svg)](https://david-dm.org/cartant/ts-snippet) 7 | [![devDependency Status](https://img.shields.io/david/dev/cartant/ts-snippet.svg)](https://david-dm.org/cartant/ts-snippet#info=devDependencies) 8 | [![peerDependency Status](https://img.shields.io/david/peer/cartant/ts-snippet.svg)](https://david-dm.org/cartant/ts-snippet#info=peerDependencies) 9 | [![Greenkeeper badge](https://badges.greenkeeper.io/cartant/ts-snippet.svg)](https://greenkeeper.io/) 10 | 11 | ### What is it? 12 | 13 | `ts-snippet` is a TypeScript snippet compiler for any test framework. 14 | 15 | It does not run the compiled snippets. Instead, it provides assertion methods that can be used to test the TypeScript programs compiled from the snippets. However, if you've not yet started writing tests for your TypeScript types, you should look at using [`tsd`](https://github.com/SamVerschueren/tsd) instead. 16 | 17 | ### Why might you need it? 18 | 19 | I created the `ts-snippet` package out of the need to test overloaded TypeScript functions that have many overload signatures. 20 | 21 | The order in which overload signatures are specified is critical and the most specific overloads need to be placed first - as TypeScript will match the first compatible overload signature. 22 | 23 | Without using `ts-snippet`, it's simple to write tests that establish whether or not TypeScript code compiles, but it's more difficult to write tests that establish whether type inferences are correct (especially when `any` is involved) or whether types are intentionally incompatible (and generate compilation errors). 24 | 25 | `ts-snippet` includes assertions that will verify whether inferred types are what's expected and whether compilation succeeds or fails. 26 | 27 | If you need to perform similar assertions, you might find `ts-snippet` useful. 28 | 29 | For an example of how `ts-snippet` can be used to write tests, checkout the [`research-spec.ts`](https://github.com/cartant/ts-action/blob/v2.0.2/source/research-spec.ts) file in my `ts-action` repo. 30 | 31 | ## Install 32 | 33 | Install the package using npm: 34 | 35 | ``` 36 | npm install ts-snippet --save-dev 37 | ``` 38 | 39 | ## Usage 40 | 41 | This simplest way to use `ts-snippet` is to create a snippet expectation function using `expecter`: 42 | 43 | ```ts 44 | import { expecter } from "ts-snippet"; 45 | 46 | const expectSnippet = expecter(); 47 | 48 | describe("observables", () => { 49 | it("should infer the source's type", () => { 50 | expectSnippet(` 51 | import * as Rx from "rxjs"; 52 | const source = Rx.Observable.of(1); 53 | `).toInfer("source", "Observable"); 54 | }); 55 | }); 56 | ``` 57 | 58 | `expecter` can be passed a factory so that common imports can be specified in just one place. For example: 59 | 60 | ```ts 61 | import { expecter } from "ts-snippet"; 62 | 63 | const expectSnippet = expecter(code => ` 64 | import * as Rx from "rxjs"; 65 | ${code} 66 | `); 67 | 68 | describe("observables", () => { 69 | it("should infer the source's type", () => { 70 | expectSnippet(` 71 | const source = Rx.Observable.of(1); 72 | `).toInfer("source", "Observable"); 73 | }); 74 | }); 75 | ``` 76 | 77 | Alternatively, the package exports a `snippet` function that returns a `Snippet` instance, upon which assertions can be made. 78 | 79 | The `snippet` function takes an object containing one or more files - with the keys representing the file names and the values the file content (as strings). The function also takes an optional `Compiler` instance - if not specified, a `Compiler` instance is created within the `snippet` call. With snippets that import large packages (such as RxJS) re-using the compiler can effect significant performance gains. 80 | 81 | Using Mocha, the tests look something like this: 82 | 83 | ```ts 84 | import { Compiler, snippet } from "ts-snippet"; 85 | 86 | describe("observables", () => { 87 | 88 | let compiler: Compiler; 89 | 90 | before(() => { 91 | compiler = new Compiler(); 92 | }); 93 | 94 | it("should infer the source's type", () => { 95 | const s = snippet({ 96 | "snippet.ts": ` 97 | import * as Rx from "rxjs"; 98 | const source = Rx.Observable.of(1); 99 | ` 100 | }, compiler); 101 | s.expect("snippet.ts").toInfer("source", "Observable"); 102 | }); 103 | 104 | it("should infer the mapped type", () => { 105 | const s = snippet({ 106 | "snippet.ts": ` 107 | import * as Rx from "rxjs"; 108 | const source = Rx.Observable.of(1); 109 | const mapped = source.map(x => x.toString()); 110 | ` 111 | }, compiler); 112 | s.expect("snippet.ts").toInfer("mapped", "Observable"); 113 | }); 114 | }); 115 | ``` 116 | 117 | Compiler can be passed the TypeScript `compilerOptions` JSON configuration and root directory for relative path module resolution (defaults to `process.cwd()`). 118 | ```ts 119 | new Compiler({ 120 | strictNullChecks: true 121 | }, __dirname); // Now module paths will be relative to the directory where the test file is located. 122 | ``` 123 | 124 | If the BDD-style expectations are not to your liking, there are alternate methods that are more terse. 125 | 126 | When using `ts-snippet` with AVA or tape, the import should specify the specific subdirectory so that the appropriate assertions are configured and the assertions count towards the test runner's plan. 127 | 128 | Using the tape-specific import and terse assertions, tests would look something like this: 129 | 130 | ```ts 131 | import * as tape from "tape"; 132 | import { snippet } from "ts-snippet/tape"; 133 | 134 | tape("should infer Observable", (t) => { 135 | t.plan(1); 136 | const s = snippet(t, { 137 | "snippet.ts": ` 138 | import * as Rx from "rxjs"; 139 | const source = Rx.Observable.from([0, 1]); 140 | ` 141 | }); 142 | s.infer("snippet.ts", "source", "Observable"); 143 | }); 144 | ``` 145 | 146 | For an example of how `ts-snippet` can be used, have a look at [these tests](https://github.com/cartant/ts-action/blob/master/source/research-spec.ts) in `ts-action`. 147 | 148 | ## API 149 | 150 | ```ts 151 | function expecter( 152 | factory: (code: string) => string = code => code, 153 | compilerOptions?: object, 154 | rootDirectory?: string 155 | ): (code: string) => Expect; 156 | 157 | function snippet( 158 | files: { [fileName: string]: string }, 159 | compiler?: Compiler 160 | ): Snippet; 161 | ``` 162 | 163 | ```ts 164 | interface Snippet { 165 | fail(fileName: string, expectedMessage?: RegExp): void; 166 | expect(fileName: string): Expect; 167 | infer(fileName: string, variableName: string, expectedType: string): void; 168 | succeed(fileName: string): void; 169 | } 170 | 171 | interface Expect { 172 | toFail(expectedMessage?: RegExp): void; 173 | toInfer(variableName: string, expectedType: string): void; 174 | toSucceed(): void; 175 | } 176 | ``` 177 | -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | `ts-snippet` is a TypeScript snippet compiler for any test framework. It exposes an expectation-based API so that a snippet's compilation can be tested for failure or success and so that inferred types can be tested, too. 2 | 3 | It can be used with [AVA](https://github.com/avajs/ava), [Jasmine](https://github.com/jasmine/jasmine), [Jest](https://facebook.github.io/jest/), [Mocha](https://github.com/mochajs/mocha) or [Tape](https://github.com/substack/tape). 4 | 5 | Using Jasmine or Mocha, the tests look something like this: 6 | 7 | ```ts 8 | import { expecter } from "ts-snippet"; 9 | 10 | const expectSnippet = expecter(); 11 | 12 | describe("observables", () => { 13 | 14 | it("should infer the source's type", () => { 15 | expectSnippet(` 16 | import * as Rx from "rxjs"; 17 | const source = Rx.Observable.of(1); 18 | `).toInfer("source", "Observable"); 19 | }); 20 | 21 | it("should infer the mapped type", () => { 22 | expectSnippet(` 23 | import * as Rx from "rxjs"; 24 | const source = Rx.Observable.of(1); 25 | const mapped = source.map(x => x.toString()); 26 | `).toInfer("mapped", "Observable"); 27 | }); 28 | }); 29 | ``` 30 | 31 | -------------------------------------------------------------------------------- /package-dist.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": {}, 3 | "es2015": "./esm2015/index.js", 4 | "main": "./index.js", 5 | "module": "./esm5/index.js", 6 | "private": false, 7 | "scripts": {}, 8 | "types": "./index.d.ts" 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Nicholas Jamieson ", 3 | "bugs": { 4 | "url": "https://github.com/cartant/ts-snippet/issues" 5 | }, 6 | "dependencies": { 7 | "tsutils": "^3.0.0" 8 | }, 9 | "description": "A TypeScript snippet testing library for any test framework", 10 | "devDependencies": { 11 | "@cartant/tslint-config": "^2.0.0", 12 | "@cartant/tslint-config-etc": "^2.0.0", 13 | "@types/chai": "^4.0.0", 14 | "@types/mocha": "^9.0.0", 15 | "@types/node": "^16.0.0", 16 | "@types/tape": "^4.2.30", 17 | "ava": "^2.0.0", 18 | "chai": "^4.0.0", 19 | "cpy-cli": "^3.0.0", 20 | "husky": "^7.0.0", 21 | "lint-staged": "^12.0.0", 22 | "mkdirp": "^1.0.0", 23 | "mocha": "^9.0.0", 24 | "prettier": "^2.0.0", 25 | "rimraf": "^3.0.0", 26 | "tape": "^5.0.0", 27 | "ts-node": "^10.0.0", 28 | "tslint": "^6.0.0", 29 | "tslint-etc": "^1.5.3", 30 | "tsutils-etc": "^1.1.0", 31 | "typescript": "~4.4.2" 32 | }, 33 | "es2015": "./dist/esm2015/index.js", 34 | "homepage": "https://github.com/cartant/ts-snippet", 35 | "keywords": [ 36 | "snippet", 37 | "test", 38 | "testing", 39 | "typescript" 40 | ], 41 | "license": "MIT", 42 | "lint-staged": { 43 | "*.{js,jsx,ts,tsx}": "prettier --write" 44 | }, 45 | "main": "./dist/index.js", 46 | "module": "./dist/esm5/index.js", 47 | "name": "ts-snippet", 48 | "optionalDependencies": {}, 49 | "peerDependencies": { 50 | "typescript": "^2.1.0 || ^3.0.0 || ^4.0.0" 51 | }, 52 | "private": true, 53 | "repository": { 54 | "type": "git", 55 | "url": "https://github.com/cartant/ts-snippet.git" 56 | }, 57 | "scripts": { 58 | "dist": "yarn run dist:clean && yarn run dist:build:cjs && yarn run dist:build:es2015 && yarn run dist:build:es5 && yarn run dist:copy", 59 | "dist:build:cjs": "tsc -p tsconfig-dist-cjs.json", 60 | "dist:build:es2015": "tsc -p tsconfig-dist-es2015.json", 61 | "dist:build:es5": "tsc -p tsconfig-dist-es5.json", 62 | "dist:clean": "rimraf dist", 63 | "dist:copy": "node scripts/pack.js && cpy CHANGELOG.md LICENSE README.md dist/", 64 | "prepare": "husky install", 65 | "prettier": "prettier --write \"./source/**/*.{js,json,ts}\"", 66 | "prettier:ci": "prettier --check \"./source/**/*.{js,json,ts}\"", 67 | "lint": "tslint --project tsconfig.json source/**/*.ts", 68 | "test": "yarn run lint && mocha -r ts-node/register ./source/*-spec.ts" 69 | }, 70 | "types": "./dist/index.d.ts", 71 | "version": "5.0.2" 72 | } 73 | -------------------------------------------------------------------------------- /scripts/pack.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | "use strict"; 7 | 8 | const fs = require("fs"); 9 | 10 | const content = Object.assign( 11 | {}, 12 | JSON.parse(fs.readFileSync("./package.json")), 13 | JSON.parse(fs.readFileSync("./package-dist.json")) 14 | ); 15 | if (content.publishConfig && content.publishConfig.tag !== "latest") { 16 | console.warn( 17 | `\n\nWARNING: package.json contains publishConfig.tag = ${ 18 | content.publishConfig.tag 19 | }\n\n` 20 | ); 21 | } 22 | fs.writeFileSync("./dist/package.json", JSON.stringify(content, null, 2)); 23 | -------------------------------------------------------------------------------- /source/ava/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | import { ExecutionContext } from "ava"; 7 | import { Compiler } from "../compiler"; 8 | import { Expect } from "../expect"; 9 | import { Snippet, snippet as _snippet } from "../snippet"; 10 | 11 | export { Compiler }; 12 | export { Expect }; 13 | 14 | export function expecter( 15 | factory: (code: string) => string, 16 | compiler: Compiler 17 | ): (context: ExecutionContext, code: string) => Expect; 18 | export function expecter( 19 | factory?: (code: string) => string, 20 | compilerOptions?: object, 21 | rootDirectory?: string 22 | ): (context: ExecutionContext, code: string) => Expect; 23 | export function expecter( 24 | factory: (code: string) => string = (code) => code, 25 | compilerOrOptions?: Compiler | object, 26 | rootDirectory?: string 27 | ): (context: ExecutionContext, code: string) => Expect { 28 | const compiler = 29 | compilerOrOptions instanceof Compiler 30 | ? compilerOrOptions 31 | : new Compiler(compilerOrOptions, rootDirectory); 32 | return (context: ExecutionContext, code: string) => 33 | snippet( 34 | context, 35 | { 36 | "snippet.ts": factory(code), 37 | }, 38 | compiler 39 | ).expect("snippet.ts"); 40 | } 41 | 42 | export function snippet( 43 | context: ExecutionContext, 44 | files: { [fileName: string]: string }, 45 | compiler?: Compiler 46 | ): Snippet { 47 | const s = _snippet(files, compiler); 48 | s.assertFail = (message: string) => context.fail(message); 49 | s.assertPass = () => context.pass(); 50 | return s; 51 | } 52 | -------------------------------------------------------------------------------- /source/compiler-spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | /*tslint:disable:no-invalid-this no-unused-expression*/ 6 | 7 | import { expect } from "chai"; 8 | import { Compiler } from "./compiler"; 9 | import { timeout } from "./timeout-spec"; 10 | 11 | describe("Compiler", function (): void { 12 | this.timeout(timeout); 13 | 14 | describe("compile", () => { 15 | it("should compile the snippet", () => { 16 | const compiler = new Compiler(); 17 | const program = compiler.compile({ 18 | "snippet.ts": ` 19 | import * as Lint from "tslint"; 20 | const pi: number = 3.14159265359; 21 | `, 22 | }); 23 | 24 | expect(program).to.be.an("object"); 25 | expect(program.getSourceFile("snippet.ts")).to.be.an("object"); 26 | expect(compiler.getDiagnostics("snippet.ts")).to.be.empty; 27 | }); 28 | 29 | it("should compile multiple snippet files", () => { 30 | const compiler = new Compiler(); 31 | const program = compiler.compile({ 32 | "other.ts": ` 33 | export const other = "other"; 34 | `, 35 | "snippet.ts": ` 36 | import { other } from "./other"; 37 | console.log(other); 38 | `, 39 | }); 40 | 41 | expect(program).to.be.an("object"); 42 | expect(program.getSourceFile("other.ts")).to.be.an("object"); 43 | expect(program.getSourceFile("snippet.ts")).to.be.an("object"); 44 | expect(compiler.getDiagnostics("other.ts")).to.be.empty; 45 | expect(compiler.getDiagnostics("snippet.ts")).to.be.empty; 46 | }); 47 | 48 | it("should support recompiling snippets", () => { 49 | console.time("1"); 50 | 51 | const compiler = new Compiler(); 52 | let program = compiler.compile({ 53 | "snippet.ts": ` 54 | const pi: string = 3.14159265359; 55 | `, 56 | }); 57 | 58 | expect(program).to.be.an("object"); 59 | expect(program.getSourceFile("snippet.ts")).to.be.an("object"); 60 | expect(compiler.getDiagnostics("snippet.ts")).to.not.be.empty; 61 | 62 | console.timeEnd("1"); 63 | console.time("2"); 64 | 65 | program = compiler.compile({ 66 | "snippet.ts": ` 67 | const pi: number = 3.14159265359; 68 | `, 69 | }); 70 | 71 | expect(program).to.be.an("object"); 72 | expect(program.getSourceFile("snippet.ts")).to.be.an("object"); 73 | expect(compiler.getDiagnostics("snippet.ts")).to.be.empty; 74 | 75 | console.timeEnd("2"); 76 | }); 77 | 78 | it("should support options", () => { 79 | const compiler = new Compiler({ 80 | moduleResolution: "node", 81 | target: "es2015", 82 | }); 83 | const program = compiler.compile({ 84 | "snippet.ts": ` 85 | const person = Object.assign({}, { name: "alice" }); 86 | `, 87 | }); 88 | 89 | expect(program).to.be.an("object"); 90 | expect(program.getSourceFile("snippet.ts")).to.be.an("object"); 91 | expect(compiler.getDiagnostics("snippet.ts")).to.be.empty; 92 | }); 93 | }); 94 | }); 95 | -------------------------------------------------------------------------------- /source/compiler.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | import * as ts from "typescript"; 7 | 8 | // There is an example that uses the LanguageService here: 9 | // https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services 10 | 11 | export class Compiler { 12 | private _compilerOptions: ts.CompilerOptions; 13 | private _files: ts.MapLike<{ content: string; version: number }>; 14 | private _languageService: ts.LanguageService; 15 | 16 | constructor( 17 | compilerOptions: object = {}, 18 | rootDirectory: string = process.cwd() 19 | ) { 20 | function normalize(path: string): string { 21 | return path.replace(/\\/g, "/"); 22 | } 23 | 24 | const { errors, options } = ts.convertCompilerOptionsFromJson( 25 | { 26 | moduleResolution: "node", 27 | skipLibCheck: true, 28 | target: "es2017", 29 | ...compilerOptions, 30 | }, 31 | normalize(rootDirectory) 32 | ); 33 | const [error] = errors; 34 | if (error) { 35 | throw new Error(this.formatDiagnostic(error)); 36 | } 37 | 38 | this._compilerOptions = options; 39 | this._files = {}; 40 | 41 | const languageServiceHost: ts.LanguageServiceHost = { 42 | directoryExists: ts.sys.directoryExists, 43 | fileExists: ts.sys.fileExists, 44 | getCompilationSettings: () => this._compilerOptions, 45 | getCurrentDirectory: () => normalize(rootDirectory), 46 | getDefaultLibFileName: (options: ts.CompilerOptions) => 47 | ts.getDefaultLibFilePath(options), 48 | getScriptFileNames: () => Object.keys(this._files), 49 | 50 | getScriptSnapshot: (fileName: string) => { 51 | if (this._files[fileName]) { 52 | return ts.ScriptSnapshot.fromString(this._files[fileName].content); 53 | } else if (ts.sys.fileExists(fileName)) { 54 | return ts.ScriptSnapshot.fromString( 55 | ts.sys.readFile(fileName)!.toString() 56 | ); 57 | } 58 | return undefined; 59 | }, 60 | 61 | getScriptVersion: (fileName: string) => { 62 | return ( 63 | this._files[fileName] && this._files[fileName].version.toString() 64 | ); 65 | }, 66 | 67 | readDirectory: ts.sys.readDirectory, 68 | readFile: ts.sys.readFile, 69 | }; 70 | this._languageService = ts.createLanguageService( 71 | languageServiceHost, 72 | ts.createDocumentRegistry() 73 | ); 74 | } 75 | 76 | compile(files: { [fileName: string]: string }): ts.Program { 77 | Object.keys(files).forEach((fileName) => { 78 | if (!this._files[fileName]) { 79 | this._files[fileName] = { content: "", version: 0 }; 80 | } 81 | this._files[fileName].content = files[fileName]; 82 | this._files[fileName].version++; 83 | }); 84 | const program = this._languageService.getProgram(); 85 | if (!program) { 86 | throw new Error("No program."); 87 | } 88 | return program; 89 | } 90 | 91 | formatDiagnostic(diagnostic: ts.Diagnostic): string { 92 | const message = ts.flattenDiagnosticMessageText( 93 | diagnostic.messageText, 94 | "\n" 95 | ); 96 | if (diagnostic.file) { 97 | const { line, character } = diagnostic.file.getLineAndCharacterOfPosition( 98 | diagnostic.start! 99 | ); 100 | return `Error ${diagnostic.file.fileName} (${line + 1},${ 101 | character + 1 102 | }): ${message}`; 103 | } 104 | return `Error: ${message}`; 105 | } 106 | 107 | getDiagnostics(fileName: string): ts.Diagnostic[] { 108 | return this._languageService 109 | .getCompilerOptionsDiagnostics() 110 | .concat(this._languageService.getSyntacticDiagnostics(fileName)) 111 | .concat(this._languageService.getSemanticDiagnostics(fileName)); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /source/expect.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | export class Expect { 7 | constructor( 8 | public toFail: (expectedMessage?: RegExp) => void, 9 | public toInfer: (variableName: string, expectedType: string) => void, 10 | public toSucceed: () => void 11 | ) {} 12 | } 13 | -------------------------------------------------------------------------------- /source/expecter-spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | /*tslint:disable:no-invalid-this no-unused-expression*/ 6 | 7 | import { Compiler } from "./compiler"; 8 | import { expecter } from "./expecter"; 9 | import { timeout } from "./timeout-spec"; 10 | 11 | describe("expecter", function (): void { 12 | this.timeout(timeout); 13 | 14 | describe("default", () => { 15 | const expectSnippet = expecter(); 16 | 17 | it("should support snippet expectations", () => { 18 | expectSnippet(` 19 | import { expect } from "chai"; 20 | const n: number = expect; 21 | `).toFail(/not assignable to type 'number'/); 22 | }); 23 | }); 24 | 25 | describe("with factory", () => { 26 | const expectSnippet = expecter( 27 | (code) => `import { expect } from "chai"; ${code}` 28 | ); 29 | 30 | it("should support snippet expectations", () => { 31 | expectSnippet(` 32 | const n: number = expect; 33 | `).toFail(/not assignable to type 'number'/); 34 | }); 35 | }); 36 | 37 | describe("with compiler", () => { 38 | const compiler = new Compiler(); 39 | const expectSnippet = expecter( 40 | (code) => `import { expect } from "chai"; ${code}`, 41 | compiler 42 | ); 43 | 44 | it("should support snippet expectations", () => { 45 | expectSnippet(` 46 | const n: number = expect; 47 | `).toFail(/not assignable to type 'number'/); 48 | }); 49 | }); 50 | }); 51 | -------------------------------------------------------------------------------- /source/expecter.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | import { Compiler } from "./compiler"; 7 | import { Expect } from "./expect"; 8 | import { snippet } from "./snippet"; 9 | 10 | export function expecter( 11 | factory: (code: string) => string, 12 | compiler: Compiler 13 | ): (code: string) => Expect; 14 | export function expecter( 15 | factory?: (code: string) => string, 16 | compilerOptions?: object, 17 | rootDirectory?: string 18 | ): (code: string) => Expect; 19 | export function expecter( 20 | factory: (code: string) => string = (code) => code, 21 | compilerOrOptions?: Compiler | object, 22 | rootDirectory?: string 23 | ): (code: string) => Expect { 24 | const compiler = 25 | compilerOrOptions instanceof Compiler 26 | ? compilerOrOptions 27 | : new Compiler(compilerOrOptions, rootDirectory); 28 | return (code: string) => 29 | snippet( 30 | { 31 | "snippet.ts": factory(code), 32 | }, 33 | compiler 34 | ).expect("snippet.ts"); 35 | } 36 | -------------------------------------------------------------------------------- /source/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | export { Compiler } from "./compiler"; 7 | export { Expect } from "./expect"; 8 | export { expecter } from "./expecter"; 9 | export { snippet, Snippet } from "./snippet"; 10 | -------------------------------------------------------------------------------- /source/placeholders-spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | /*tslint:disable:no-unused-expression*/ 6 | 7 | import { expecter } from "./expecter"; 8 | 9 | describe("placeholders", () => { 10 | const expectSnippet = expecter( 11 | (code) => ` 12 | import * as placeholders from "./source/placeholders"; 13 | ${code} 14 | ` 15 | ); 16 | 17 | it("should be importable into a snippet", () => { 18 | const expect = expectSnippet(` 19 | const assignedConstant = placeholders.c1; 20 | let assignedVariable = placeholders.v2; 21 | declare const declaredConstant: placeholders.T3; 22 | declare let declaredVariable: placeholders.T4; 23 | `); 24 | expect.toInfer("assignedConstant", "T1"); 25 | expect.toInfer("assignedVariable", "T2"); 26 | expect.toInfer("declaredConstant", "T3"); 27 | expect.toInfer("declaredVariable", "T4"); 28 | }); 29 | 30 | it("should have incompatible types", () => { 31 | const expect = expectSnippet(` 32 | let v: placeholders.T1 = placeholders.v2; 33 | `); 34 | expect.toFail(/type 'T2' is not assignable to type 'T1'/i); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /source/placeholders.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | export interface T0 { 7 | kind: "T0"; 8 | } 9 | export interface T1 { 10 | kind: "T1"; 11 | } 12 | export interface T2 { 13 | kind: "T2"; 14 | } 15 | export interface T3 { 16 | kind: "T3"; 17 | } 18 | export interface T4 { 19 | kind: "T4"; 20 | } 21 | export interface T5 { 22 | kind: "T5"; 23 | } 24 | export interface T6 { 25 | kind: "T6"; 26 | } 27 | export interface T7 { 28 | kind: "T7"; 29 | } 30 | export interface T8 { 31 | kind: "T8"; 32 | } 33 | export interface T9 { 34 | kind: "T9"; 35 | } 36 | export interface T10 { 37 | kind: "T10"; 38 | } 39 | export interface T11 { 40 | kind: "T11"; 41 | } 42 | export interface T12 { 43 | kind: "T12"; 44 | } 45 | 46 | export declare const c0: T0; 47 | export declare const c1: T1; 48 | export declare const c2: T2; 49 | export declare const c3: T3; 50 | export declare const c4: T4; 51 | export declare const c5: T5; 52 | export declare const c6: T6; 53 | export declare const c7: T7; 54 | export declare const c8: T8; 55 | export declare const c9: T9; 56 | export declare const c10: T10; 57 | export declare const c11: T11; 58 | export declare const c12: T12; 59 | 60 | export declare let v0: T0; 61 | export declare let v1: T1; 62 | export declare let v2: T2; 63 | export declare let v3: T3; 64 | export declare let v4: T4; 65 | export declare let v5: T5; 66 | export declare let v6: T6; 67 | export declare let v7: T7; 68 | export declare let v8: T8; 69 | export declare let v9: T9; 70 | export declare let v10: T10; 71 | export declare let v11: T11; 72 | export declare let v12: T12; 73 | -------------------------------------------------------------------------------- /source/snippet-spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | /*tslint:disable:no-invalid-this no-unused-expression*/ 6 | 7 | import { expect } from "chai"; 8 | import { Compiler } from "./compiler"; 9 | import { 10 | areEquivalentTypeStrings, 11 | getVariables, 12 | snippet, 13 | Snippet, 14 | } from "./snippet"; 15 | import { timeout } from "./timeout-spec"; 16 | 17 | describe("Snippet", function (): void { 18 | this.timeout(timeout); 19 | 20 | describe("areEquivalentTypeStrings", () => { 21 | it("should compare simple types", () => { 22 | expect(areEquivalentTypeStrings("number", "string")).to.be.false; 23 | expect(areEquivalentTypeStrings("number", "number")).to.be.true; 24 | }); 25 | 26 | it("should ignore whitespace differences", () => { 27 | expect( 28 | areEquivalentTypeStrings("Observable", "Observable< number >") 29 | ).to.be.true; 30 | }); 31 | 32 | it("should ignore leading whitespace", () => { 33 | expect( 34 | areEquivalentTypeStrings( 35 | "Observable", 36 | ` 37 | Observable` 38 | ) 39 | ).to.be.true; 40 | }); 41 | 42 | it("should ignore trailing whitespace", () => { 43 | expect( 44 | areEquivalentTypeStrings( 45 | "Observable", 46 | `Observable 47 | ` 48 | ) 49 | ).to.be.true; 50 | }); 51 | }); 52 | 53 | describe("getVariables", () => { 54 | it("should get the variables", () => { 55 | const compiler = new Compiler(); 56 | const program = compiler.compile({ 57 | "snippet.ts": ` 58 | let a = 1; 59 | let b = "two"; 60 | let c = [3]; 61 | `, 62 | }); 63 | const sourceFile = program.getSourceFile("snippet.ts"); 64 | const variables = getVariables(program, sourceFile!); 65 | 66 | expect(variables).to.deep.equal({ 67 | a: "number", 68 | b: "string", 69 | c: "number[]", 70 | }); 71 | }); 72 | 73 | it("should also get nested variables", () => { 74 | const compiler = new Compiler(); 75 | const program = compiler.compile({ 76 | "snippet.ts": ` 77 | let a = 1; 78 | let b = "two"; 79 | let c = [3]; 80 | if (true) { 81 | let d = "four"; 82 | } 83 | `, 84 | }); 85 | const sourceFile = program.getSourceFile("snippet.ts"); 86 | const variables = getVariables(program, sourceFile!); 87 | 88 | expect(variables).to.deep.equal({ 89 | a: "number", 90 | b: "string", 91 | c: "number[]", 92 | d: "string", 93 | }); 94 | }); 95 | }); 96 | 97 | describe("snippet", () => { 98 | describe("fail", () => { 99 | it("should not throw if an error occurs", () => { 100 | const snip = snippet({ 101 | "a.ts": "let a: string = 1;", 102 | }); 103 | 104 | expect(() => snip.fail("a.ts")).to.not.throw(); 105 | }); 106 | 107 | it("should not throw if a matching error occurs", () => { 108 | const snip = snippet({ 109 | "a.ts": "let a: string = 1;", 110 | }); 111 | 112 | expect(() => 113 | snip.fail("a.ts", /is not assignable to type 'string'/) 114 | ).to.not.throw(); 115 | }); 116 | 117 | it("should throw if a non-matching error occurs", () => { 118 | const snip = snippet({ 119 | "a.ts": "let a: string = 1;", 120 | }); 121 | 122 | expect(() => 123 | snip.fail("a.ts", /is not assignable to type 'number'/) 124 | ).to.throw(); 125 | }); 126 | 127 | it("should throw if no error occurs", () => { 128 | const snip = snippet({ 129 | "a.ts": "let a: number = 1;", 130 | }); 131 | 132 | expect(() => snip.fail("a.ts")).to.throw(); 133 | }); 134 | }); 135 | 136 | describe("expect", () => { 137 | describe("toFail", () => { 138 | it("should not throw if an error occurs", () => { 139 | const snip = snippet({ 140 | "a.ts": "let a: string = 1;", 141 | }); 142 | 143 | expect(() => snip.expect("a.ts").toFail()).to.not.throw(); 144 | }); 145 | 146 | it("should not throw if a matching error occurs", () => { 147 | const snip = snippet({ 148 | "a.ts": "let a: string = 1;", 149 | }); 150 | 151 | expect(() => 152 | snip.expect("a.ts").toFail(/is not assignable to type 'string'/) 153 | ).to.not.throw(); 154 | }); 155 | 156 | it("should throw if a non-matching error occurs", () => { 157 | const snip = snippet({ 158 | "a.ts": "let a: string = 1;", 159 | }); 160 | 161 | expect(() => 162 | snip.expect("a.ts").toFail(/is not assignable to type 'number'/) 163 | ).to.throw(); 164 | }); 165 | 166 | it("should throw if no error occurs", () => { 167 | const snip = snippet({ 168 | "a.ts": "let a: number = 1;", 169 | }); 170 | 171 | expect(() => snip.expect("a.ts").toFail()).to.throw(); 172 | }); 173 | }); 174 | 175 | describe("toInfer", () => { 176 | let snip: Snippet; 177 | 178 | beforeEach(() => { 179 | snip = snippet({ 180 | "a.ts": "let a = 1;", 181 | "b.ts": "let b = 2;", 182 | }); 183 | }); 184 | 185 | it("should throw if a variable is not found", () => { 186 | expect(() => snip.expect("a.ts").toInfer("x", "number")).to.throw( 187 | /variable 'x' not found/i 188 | ); 189 | expect(() => snip.expect("b.ts").toInfer("x", "number")).to.throw( 190 | /variable 'x' not found/i 191 | ); 192 | }); 193 | 194 | it("should throw if a variable has an unexpected type", () => { 195 | expect(() => snip.expect("a.ts").toInfer("a", "string")).to.throw( 196 | /expected 'a: number' to be 'string'/i 197 | ); 198 | expect(() => snip.expect("b.ts").toInfer("b", "string")).to.throw( 199 | /expected 'b: number' to be 'string'/i 200 | ); 201 | }); 202 | 203 | it("should not throw if a variable has the expected type", () => { 204 | expect(() => 205 | snip.expect("a.ts").toInfer("a", "number") 206 | ).to.not.throw(); 207 | expect(() => 208 | snip.expect("b.ts").toInfer("b", "number") 209 | ).to.not.throw(); 210 | }); 211 | 212 | it("should throw if an error occurs", () => { 213 | const snipWithError = snippet({ 214 | "a.ts": "let a: string = 1;", 215 | "b.ts": "let b = 2;", 216 | }); 217 | 218 | expect(() => 219 | snipWithError.expect("b.ts").toInfer("b", "number") 220 | ).to.throw(); 221 | }); 222 | }); 223 | 224 | describe("toSucceed", () => { 225 | it("should not throw if no error occurs", () => { 226 | const snip = snippet({ 227 | "a.ts": "let a: number = 1;", 228 | }); 229 | 230 | expect(() => snip.expect("a.ts").toSucceed()).to.not.throw(); 231 | }); 232 | 233 | it("should throw if an error occurs", () => { 234 | const snip = snippet({ 235 | "a.ts": "let a: string = 1;", 236 | }); 237 | 238 | expect(() => snip.expect("a.ts").toSucceed()).to.throw(); 239 | }); 240 | }); 241 | }); 242 | 243 | describe("infer", () => { 244 | let snip: Snippet; 245 | 246 | beforeEach(() => { 247 | snip = snippet({ 248 | "a.ts": "let a = 1;", 249 | "b.ts": "let b = 2;", 250 | }); 251 | }); 252 | 253 | it("should throw if a variable is not found", () => { 254 | expect(() => snip.infer("a.ts", "x", "number")).to.throw( 255 | /variable 'x' not found/i 256 | ); 257 | expect(() => snip.infer("b.ts", "x", "number")).to.throw( 258 | /variable 'x' not found/i 259 | ); 260 | }); 261 | 262 | it("should throw if a variable has an unexpected type", () => { 263 | expect(() => snip.infer("a.ts", "a", "string")).to.throw( 264 | /expected 'a: number' to be 'string'/i 265 | ); 266 | expect(() => snip.infer("b.ts", "b", "string")).to.throw( 267 | /expected 'b: number' to be 'string'/i 268 | ); 269 | }); 270 | 271 | it("should not throw if a variable has the expected type", () => { 272 | expect(() => snip.infer("a.ts", "a", "number")).to.not.throw(); 273 | expect(() => snip.infer("b.ts", "b", "number")).to.not.throw(); 274 | }); 275 | }); 276 | 277 | describe("succeed", () => { 278 | it("should not throw if no error occurs", () => { 279 | const snip = snippet({ 280 | "a.ts": "let a: number = 1;", 281 | }); 282 | 283 | expect(() => snip.succeed("a.ts")).to.not.throw(); 284 | }); 285 | 286 | it("should throw if an error occurs", () => { 287 | const snip = snippet({ 288 | "a.ts": "let a: string = 1;", 289 | }); 290 | 291 | expect(() => snip.succeed("a.ts")).to.throw(); 292 | }); 293 | }); 294 | }); 295 | }); 296 | -------------------------------------------------------------------------------- /source/snippet.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | /*tslint:disable:member-ordering*/ 6 | 7 | import * as tsutils from "tsutils"; 8 | import * as ts from "typescript"; 9 | import { Compiler } from "./compiler"; 10 | import { Expect } from "./expect"; 11 | 12 | export class Snippet { 13 | private _program: ts.Program; 14 | public assertFail: (message: string) => void = (message: string) => { 15 | throw new Error(message); 16 | }; 17 | public assertPass: () => void = () => {}; 18 | 19 | constructor( 20 | private _files: { [fileName: string]: string }, 21 | private _compiler: Compiler 22 | ) { 23 | this._program = _compiler.compile(_files); 24 | } 25 | 26 | expect(fileName: string): Expect { 27 | return new Expect( 28 | this.fail.bind(this, fileName), 29 | this.infer.bind(this, fileName), 30 | this.succeed.bind(this, fileName) 31 | ); 32 | } 33 | 34 | fail(fileName: string, expectedMessage?: RegExp): void { 35 | const diagnostics = this._getDiagnostics(fileName); 36 | const messages = diagnostics.map(this._compiler.formatDiagnostic); 37 | const matched = messages.some((message) => 38 | expectedMessage ? expectedMessage.test(message) : true 39 | ); 40 | if (!matched) { 41 | const receivedMessages = new Set(messages); 42 | 43 | this.assertFail( 44 | expectedMessage && receivedMessages.size > 0 45 | ? `Expected an error matching: 46 | ${expectedMessage} 47 | but received: 48 | ${[...receivedMessages].join("\n")}` 49 | : "Expected an error" 50 | ); 51 | } else { 52 | this.assertPass(); 53 | } 54 | } 55 | 56 | infer(fileName: string, variableName: string, expectedType: string): void { 57 | this.succeed(fileName); 58 | const sourceFile = this._program.getSourceFile(fileName)!; 59 | const variables = getVariables(this._program, sourceFile); 60 | const actualType = variables[variableName]; 61 | if (!actualType) { 62 | this.assertFail(`Variable '${variableName}' not found`); 63 | } else if (!areEquivalentTypeStrings(expectedType, actualType)) { 64 | this.assertFail( 65 | `Expected '${variableName}: ${actualType}' to be '${expectedType}'` 66 | ); 67 | } else { 68 | this.assertPass(); 69 | } 70 | } 71 | 72 | succeed(fileName: string): void { 73 | const diagnostics = this._getDiagnostics(fileName); 74 | if (diagnostics.length) { 75 | const [diagnostic] = diagnostics; 76 | this.assertFail(this._compiler.formatDiagnostic(diagnostic)); 77 | } else { 78 | this.assertPass(); 79 | } 80 | } 81 | 82 | private _getDiagnostics(fileName: string): ts.Diagnostic[] { 83 | return this._program 84 | .getSemanticDiagnostics() 85 | .concat(this._compiler.getDiagnostics(fileName)); 86 | } 87 | } 88 | 89 | export function areEquivalentTypeStrings(a: string, b: string): boolean { 90 | const spaces = /\s/g; 91 | return a.replace(spaces, "") === b.replace(spaces, ""); 92 | } 93 | 94 | export function getVariables( 95 | program: ts.Program, 96 | sourceFile: ts.SourceFile 97 | ): { [variableName: string]: string } { 98 | const typeChecker = program.getTypeChecker(); 99 | const variables: { [name: string]: string } = {}; 100 | 101 | const visitNode = (node: ts.Node) => { 102 | if (tsutils.isVariableStatement(node)) { 103 | tsutils.forEachDeclaredVariable(node.declarationList, (node) => { 104 | variables[node.name.getText()] = typeChecker.typeToString( 105 | typeChecker.getTypeAtLocation(node) 106 | ); 107 | }); 108 | } else { 109 | node.forEachChild(visitNode); 110 | } 111 | }; 112 | 113 | sourceFile.forEachChild(visitNode); 114 | return variables; 115 | } 116 | 117 | export function snippet( 118 | files: { [fileName: string]: string }, 119 | compiler?: Compiler 120 | ): Snippet { 121 | return new Snippet(files, compiler || new Compiler()); 122 | } 123 | -------------------------------------------------------------------------------- /source/tape/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | import * as tape from "tape"; 7 | import { Compiler } from "../compiler"; 8 | import { Expect } from "../expect"; 9 | import { Snippet, snippet as _snippet } from "../snippet"; 10 | 11 | export { Compiler }; 12 | export { Expect }; 13 | 14 | export function expecter( 15 | factory: (code: string) => string, 16 | compiler: Compiler 17 | ): (context: tape.Test, code: string) => Expect; 18 | export function expecter( 19 | factory?: (code: string) => string, 20 | compilerOptions?: object, 21 | rootDirectory?: string 22 | ): (context: tape.Test, code: string) => Expect; 23 | export function expecter( 24 | factory: (code: string) => string = (code) => code, 25 | compilerOrOptions?: object, 26 | rootDirectory?: string 27 | ): (context: tape.Test, code: string) => Expect { 28 | const compiler = 29 | compilerOrOptions instanceof Compiler 30 | ? compilerOrOptions 31 | : new Compiler(compilerOrOptions, rootDirectory); 32 | return (context: tape.Test, code: string) => 33 | snippet( 34 | context, 35 | { 36 | "snippet.ts": factory(code), 37 | }, 38 | compiler 39 | ).expect("snippet.ts"); 40 | } 41 | 42 | export function snippet( 43 | context: tape.Test, 44 | files: { [fileName: string]: string }, 45 | compiler?: Compiler 46 | ): Snippet { 47 | const s = _snippet(files, compiler); 48 | s.assertFail = (message: string) => context.fail(message); 49 | s.assertPass = () => context.pass(); 50 | return s; 51 | } 52 | -------------------------------------------------------------------------------- /source/timeout-spec.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license Use of this source code is governed by an MIT-style license that 3 | * can be found in the LICENSE file at https://github.com/cartant/ts-snippet 4 | */ 5 | 6 | export const timeout = 5000; 7 | -------------------------------------------------------------------------------- /tsconfig-dist-cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "module": "commonjs", 5 | "outDir": "dist", 6 | "target": "es5" 7 | }, 8 | "extends": "./tsconfig-dist.json" 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig-dist-es2015.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "es2015", 4 | "outDir": "dist/esm2015", 5 | "target": "es2015" 6 | }, 7 | "extends": "./tsconfig-dist.json" 8 | } 9 | -------------------------------------------------------------------------------- /tsconfig-dist-es5.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "es2015", 4 | "outDir": "dist/esm5", 5 | "target": "es5" 6 | }, 7 | "extends": "./tsconfig-dist.json" 8 | } 9 | -------------------------------------------------------------------------------- /tsconfig-dist.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "noEmit": false 4 | }, 5 | "exclude": [ 6 | "source/**/*-spec.ts" 7 | ], 8 | "extends": "./tsconfig.json" 9 | } 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "downlevelIteration": true, 4 | "lib": ["es2015"], 5 | "module": "commonjs", 6 | "moduleResolution": "node", 7 | "noEmit": true, 8 | "noImplicitAny": true, 9 | "removeComments": true, 10 | "skipLibCheck": true, 11 | "strict": true, 12 | "suppressImplicitAnyIndexErrors": true, 13 | "target": "es5" 14 | }, 15 | "exclude": [], 16 | "include": ["source/**/*.ts"] 17 | } 18 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": [ 4 | "@cartant/tslint-config", 5 | "@cartant/tslint-config-etc" 6 | ], 7 | "rules": { 8 | "semicolon": { "severity": "off" }, 9 | "typedef": { 10 | "options": [ 11 | "parameter", 12 | "property-declaration" 13 | ], 14 | "severity": "error" 15 | } 16 | } 17 | } 18 | --------------------------------------------------------------------------------