Skip to content

Instantly share code, notes, and snippets.

@hoodunit
Created June 4, 2019 13:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hoodunit/ee681233da70a51720644f112bbde430 to your computer and use it in GitHub Desktop.
TypeScript TSLint test (as normal unit test)
import * as ts from 'typescript'
import { Configuration, ILinterOptions, Linter, LintResult } from 'tslint'
interface TestConfig {
lint: string
projectDir: string
ts: string
}
const testConfig = {
lint: 'tslint.json',
projectDir: './',
ts: 'tsconfig.json'
}
const linterOptions: ILinterOptions = {
fix: false,
formatter: 'stylish'
}
describe('TSLint', () => {
it('passes', function(this: any) {
this.timeout = 20000
const results = lintFiles(testConfig)
if (results.errorCount > 0) {
throw new Error(results.output)
}
})
})
function lintFiles(config: TestConfig): LintResult {
const program = Linter.createProgram(config.ts, config.projectDir)
const linter = new Linter(linterOptions, program)
const files = Linter.getFileNames(program)
files.forEach((file) => lintFile(program, linter, file))
return linter.getResult()
}
const lintFile = (program: ts.Program, linter: Linter, file: string) => {
const fileContents = program.getSourceFile(file)!.getFullText()
const configLoad = Configuration.findConfiguration(testConfig.lint, file)
return linter.lint(file, fileContents, configLoad.results)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment