File size: 3,995 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import * as shell from 'shelljs';

import * as util from '../utils/fixture';

shell.config.silent = true;

const testDir = 'e2e';
const stageName = 'stage-lint';

const lintDir = `test/${testDir}/fixtures/lint`;

describe('tsdx lint', () => {
  it('should fail to lint a ts file with errors', () => {
    const testFile = `${lintDir}/file-with-lint-errors.ts`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(1);
    expect(output.stdout.includes('Parsing error:')).toBe(true);
  });

  it('should succeed linting a ts file without errors', () => {
    const testFile = `${lintDir}/file-without-lint-error.ts`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(0);
  });

  it('should fail to lint a ts file with prettier errors', () => {
    const testFile = `${lintDir}/file-with-prettier-lint-errors.ts`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(1);
    expect(output.stdout.includes('prettier/prettier')).toBe(true);
  });

  it('should fail to lint a tsx file with errors', () => {
    const testFile = `${lintDir}/react-file-with-lint-errors.tsx`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(1);
    expect(output.stdout.includes('Parsing error:')).toBe(true);
  });

  it('should succeed linting a tsx file without errors', () => {
    const testFile = `${lintDir}/react-file-without-lint-error.tsx`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(0);
  });

  it('should succeed linting a ts file with warnings when --max-warnings is not used', () => {
    const testFile = `${lintDir}/file-with-lint-warnings.ts`;
    const output = shell.exec(`node dist/index.js lint ${testFile}`);
    expect(output.code).toBe(0);
    expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
      true
    );
  });

  it('should succeed linting a ts file with fewer warnings than --max-warnings', () => {
    const testFile = `${lintDir}/file-with-lint-warnings.ts`;
    const output = shell.exec(
      `node dist/index.js lint ${testFile} --max-warnings 4`
    );
    expect(output.code).toBe(0);
    expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
      true
    );
  });

  it('should succeed linting a ts file with same number of warnings as --max-warnings', () => {
    const testFile = `${lintDir}/file-with-lint-warnings.ts`;
    const output = shell.exec(
      `node dist/index.js lint ${testFile} --max-warnings 3`
    );
    expect(output.code).toBe(0);
    expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
      true
    );
  });

  it('should fail to lint a ts file with more warnings than --max-warnings', () => {
    const testFile = `${lintDir}/file-with-lint-warnings.ts`;
    const output = shell.exec(
      `node dist/index.js lint ${testFile} --max-warnings 2`
    );
    expect(output.code).toBe(1);
    expect(output.stdout.includes('@typescript-eslint/no-unused-vars')).toBe(
      true
    );
  });

  it('should not lint', () => {
    const output = shell.exec(`node dist/index.js lint`);
    expect(output.code).toBe(1);
    expect(output.toString()).toContain('Defaulting to "tsdx lint src test"');
    expect(output.toString()).toContain(
      'You can override this in the package.json scripts, like "lint": "tsdx lint src otherDir"'
    );
  });

  describe('when --write-file is used', () => {
    beforeEach(() => {
      util.teardownStage(stageName);
      util.setupStageWithFixture(testDir, stageName, 'build-default');
    });

    it('should create the file', () => {
      const output = shell.exec(`node ../dist/index.js lint --write-file`);
      expect(shell.test('-f', '.eslintrc.js')).toBeTruthy();
      expect(output.code).toBe(0);
    });

    afterAll(() => {
      util.teardownStage(stageName);
    });
  });
});