UNPKG

6.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslint_1 = require("tslint");
4var typescript_1 = require("typescript");
5var ts = require("typescript");
6var util_1 = require("util");
7var lint_factory_1 = require("./lint-factory");
8describe('lint factory', function () {
9 describe('createProgram()', function () {
10 it('should create a TS Program', function () {
11 var context = { rootDir: '' };
12 var program = lint_factory_1.createProgram(context, '');
13 var fns = [
14 'getSourceFiles',
15 'getTypeChecker'
16 ];
17 expect(util_1.isObject(program)).toBeTruthy();
18 for (var _i = 0, fns_1 = fns; _i < fns_1.length; _i++) {
19 var fn = fns_1[_i];
20 expect(typeof program[fn]).toEqual('function');
21 }
22 });
23 });
24 describe('getTsLintConfig()', function () {
25 it('should fetch the TSLint configuration from file path', function () {
26 var tsConfigFilePath = 'tsconfig.json';
27 var mockConfig = { rulesDirectory: ['node_modules/@ionic'] };
28 spyOn(tslint_1.Configuration, tslint_1.Configuration.loadConfigurationFromPath.name).and.returnValue(mockConfig);
29 var config = lint_factory_1.getTsLintConfig(tsConfigFilePath);
30 expect(util_1.isObject(config)).toBeTruthy();
31 expect(tslint_1.Configuration.loadConfigurationFromPath).toHaveBeenLastCalledWith(tsConfigFilePath);
32 expect(config).toEqual(mockConfig);
33 });
34 it('should extend configuration with {linterOptions} if provided', function () {
35 var tsConfigFilePath = 'tsconfig.json';
36 var mockConfig = { rulesDirectory: ['node_modules/@ionic'] };
37 spyOn(tslint_1.Configuration, tslint_1.Configuration.loadConfigurationFromPath.name).and.returnValue(mockConfig);
38 var config = lint_factory_1.getTsLintConfig(tsConfigFilePath, {
39 typeCheck: true
40 });
41 expect(config.linterOptions).toEqual({
42 typeCheck: true
43 });
44 });
45 });
46 describe('createLinter()', function () {
47 it('should create a Linter', function () {
48 var context = { rootDir: '' };
49 var program = lint_factory_1.createProgram(context, '');
50 var linter = lint_factory_1.createLinter(context, program);
51 expect(linter instanceof tslint_1.Linter).toBeTruthy();
52 });
53 });
54 describe('getFileNames()', function () {
55 it('should get the file names referenced in the tsconfig.json', function () {
56 var context = { rootDir: '' };
57 var program = lint_factory_1.createProgram(context, '');
58 var mockFiles = ['test.ts'];
59 spyOn(tslint_1.Linter, 'getFileNames').and.returnValue(mockFiles);
60 var files = lint_factory_1.getFileNames(context, program);
61 expect(Array.isArray(files)).toBeTruthy();
62 expect(files).toEqual(mockFiles);
63 });
64 });
65 describe('typeCheck()', function () {
66 it('should not be called if {typeCheck} is false', function (done) {
67 var context = { rootDir: '' };
68 var program = lint_factory_1.createProgram(context, '');
69 spyOn(ts, ts.getPreEmitDiagnostics.name).and.returnValue([]);
70 lint_factory_1.typeCheck(context, program, { typeCheck: false })
71 .then(function (result) {
72 expect(ts.getPreEmitDiagnostics).toHaveBeenCalledTimes(0);
73 expect(result).toEqual([]);
74 done();
75 });
76 });
77 it('should type check if {typeCheck} is true', function (done) {
78 var context = { rootDir: '' };
79 var program = lint_factory_1.createProgram(context, '');
80 var diagnostics = [{
81 file: {},
82 start: 2,
83 length: 10,
84 messageText: 'Oops',
85 category: typescript_1.DiagnosticCategory.Warning,
86 code: 120
87 }];
88 spyOn(ts, ts.getPreEmitDiagnostics.name).and.returnValue(diagnostics);
89 lint_factory_1.typeCheck(context, program, { typeCheck: true })
90 .then(function (result) {
91 expect(ts.getPreEmitDiagnostics).toHaveBeenCalledWith(program);
92 expect(result).toEqual(diagnostics);
93 done();
94 });
95 });
96 });
97 describe('lint()', function () {
98 it('should lint a file', function () {
99 var context = { rootDir: '' };
100 var program = lint_factory_1.createProgram(context, '');
101 var linter = lint_factory_1.createLinter(context, program);
102 spyOn(linter, 'lint').and.returnValue(undefined);
103 var config = {};
104 var filePath = 'test.ts';
105 var fileContents = 'const test = true;';
106 lint_factory_1.lint(linter, config, filePath, fileContents);
107 expect(linter.lint).toHaveBeenCalledWith(filePath, fileContents, config);
108 });
109 });
110 describe('getLintResult()', function () {
111 it('should get the lint results after linting a file', function () {
112 var context = { rootDir: '' };
113 var program = lint_factory_1.createProgram(context, '');
114 var linter = lint_factory_1.createLinter(context, program);
115 spyOn(linter, 'lint').and.returnValue(undefined);
116 var mockResult = {};
117 spyOn(linter, 'getResult').and.returnValue(mockResult);
118 var config = {
119 jsRules: new Map(),
120 rules: new Map()
121 };
122 var filePath = 'test.ts';
123 var fileContents = 'const test = true;';
124 lint_factory_1.lint(linter, config, filePath, fileContents);
125 var result = lint_factory_1.getLintResult(linter);
126 expect(util_1.isObject(result)).toBeTruthy();
127 expect(result).toEqual(mockResult);
128 });
129 });
130});