Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0rted committed Feb 3, 2020
1 parent a716419 commit 8e71d54
Show file tree
Hide file tree
Showing 9 changed files with 4,701 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/stylelint-problem-matcher.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"pattern": [
{
"regexp": "^([^\\s].*)$",
"file": "1"
"file": 1
},
{
"regexp": "^\\s+((\\d+):(\\d+))?\\s+(✖|×)\\s+(.*)\\s{3,}(.*)$",
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:

- run: npm ci

- run: npm run lint

- run: npm test

- run: npm run build
18 changes: 18 additions & 0 deletions __data__/stylelintMatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { problemMatcher } from "../.github/stylelint-problem-matcher.json";

export interface ProblemMatcher {
owner: string;
pattern: ProblemMatcherPattern[];
};

export interface ProblemMatcherPattern {
regexp: string;
file?: number;
line?: number;
column?: number;
message?: number;
code?: number;
loop?: boolean;
}

export const stylelintMatcher: ProblemMatcher = problemMatcher[0];
4 changes: 4 additions & 0 deletions __helpers__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function matchResults(report: string[], regexp: RegExp): (RegExpExecArray | null)[] {
return report.map(line => regexp.exec(line))
.filter(match => match);
}
66 changes: 66 additions & 0 deletions __tests__/problemMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { matchResults } from "../__helpers__/utils";
import { stylelintMatcher, ProblemMatcherPattern } from "../__data__/stylelintMatcher";

describe("problemMatcher", () => {
it("has two patterns", () => {
expect(stylelintMatcher.pattern.length).toEqual(2);
});

describe("file pattern", () => {
let pattern: ProblemMatcherPattern;
let regexp: RegExp;

beforeEach(() => {
pattern = stylelintMatcher.pattern[0];
regexp = new RegExp(pattern.regexp);
});

it("matches file path", () => {
const reportOutput = [
"scss/_test.scss",
" 11:16 × Unexpected unit length-zero-no-unit",
" 11:28 × Unexpected unit length-zero-no-unit",
];

const results = matchResults(reportOutput, regexp);

expect(results.length).toEqual(1);
expect(results[0][pattern.file]).toEqual("scss/_test.scss");
});
});

describe("violation pattern", () => {
let pattern: ProblemMatcherPattern;
let regexp: RegExp;

beforeEach(() => {
pattern = stylelintMatcher.pattern[1];
regexp = new RegExp(pattern.regexp);
});

it("matches violations", () => {
const reportOutput = [
"scss/_test.scss",
" 11:16 × Unexpected unit length-zero-no-unit",
" 11:28 × Unexpected unit length-zero-no-unit",
];

const results = matchResults(reportOutput, regexp);

expect(results.length).toEqual(2);
});

it("matches violations without line numbers", () => {
const reportOutput = [
"scss/_test.scss",
" × Unexpected Unicode BOM unicode-bom",
" 11:16 × Unexpected unit length-zero-no-unit",
" 11:28 × Unexpected unit length-zero-no-unit",
];

const results = matchResults(reportOutput, regexp);

expect(results.length).toEqual(3);
});
});
});
31 changes: 31 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: [
"**/*.ts",
"!lib/**",
"!**/node_modules/**",
],
coverageDirectory: "./coverage/",
globals: {
"ts-jest": {
diagnostics: false,
}
},
moduleFileExtensions: ["js", "ts"],
testEnvironment: "node",
testMatch: ["**/*.test.ts"],
testRunner: "jest-circus/runner",
transform: {
"^.+\\.ts$": "ts-jest",
},
verbose: true,
};

const processStdoutWrite = process.stdout.write.bind(process.stdout);

process.stdout.write = (str, encoding, cb) => {
if (!str.match(/^::/)) {
return processStdoutWrite(str, encoding, cb);
}
};
Loading

0 comments on commit 8e71d54

Please sign in to comment.