Skip to content

Commit

Permalink
Merge pull request #2 from codewars/add-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk authored Jul 12, 2020
2 parents 5096e8d + 15d261c commit ddbabdc
Show file tree
Hide file tree
Showing 22 changed files with 2,098 additions and 331 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.x"
registry-url: https://registry.npmjs.org/
- uses: actions/cache@v1
with:
path: node_modules
key: yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
yarn-
- run: yarn install --frozen-lockfile
- run: yarn test
16 changes: 16 additions & 0 deletions fixtures/group-passing-failing.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<DESCRIBE::>group 1

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>1

<IT::>test 2

<PASSED::>Test Passed

<COMPLETEDIN::>0

<COMPLETEDIN::>
8 changes: 8 additions & 0 deletions fixtures/group-passing-failing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe("group 1", () => {
test("test 1", () => {
expect(1).toBe(1);
});
test("test 2", () => {
expect(1).toBe(1);
});
});
20 changes: 20 additions & 0 deletions fixtures/multiple-groups.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<DESCRIBE::>group 1

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>1

<COMPLETEDIN::>

<DESCRIBE::>group 2

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>1

<COMPLETEDIN::>
11 changes: 11 additions & 0 deletions fixtures/multiple-groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe("group 1", () => {
test("test 1", () => {
expect(1).toBe(1);
});
});

describe("group 2", () => {
test("test 1", () => {
expect(1).toBe(1);
});
});
14 changes: 14 additions & 0 deletions fixtures/nested-groups.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<DESCRIBE::>group 1

<DESCRIBE::>group 1 1

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>1

<COMPLETEDIN::>

<COMPLETEDIN::>
7 changes: 7 additions & 0 deletions fixtures/nested-groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("group 1", () => {
describe("group 1 1", () => {
test("test 1", () => {
expect(1).toBe(1);
});
});
});
14 changes: 14 additions & 0 deletions fixtures/single-group-failing.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

<DESCRIBE::>group 1

<IT::>test 1

<FAILED::>Test Failed

<LOG:HTML:Failure><pre class="ansi"><code>Error: expect(received).toBe(expected) // Object.is equality<:LF:><:LF:>Expected: 2<:LF:>Received: 1</code></pre>

<COMPLETEDIN::>3

<COMPLETEDIN::>

<LOG:HTML:Failures><pre class="ansi"><code>● group 1 › test 1<:LF:><:LF:> expect(received).toBe(expected) // Object.is equality<:LF:><:LF:> Expected: 2<:LF:> Received: 1<:LF:><:LF:> 1 | describe("group 1", () =&gt; {<:LF:> 2 | test("test 1", () =&gt; {<:LF:> &gt; 3 | expect(1).toBe(2);<:LF:> | ^<:LF:> 4 | });<:LF:> 5 | });<:LF:> 6 | <:LF:><:LF:> at Object.&lt;anonymous&gt; (fixtures/single-group-failing.js:3:15)</code></pre>
5 changes: 5 additions & 0 deletions fixtures/single-group-failing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("group 1", () => {
test("test 1", () => {
expect(1).toBe(2);
});
});
10 changes: 10 additions & 0 deletions fixtures/single-group-passing.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<DESCRIBE::>group 1

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>2

<COMPLETEDIN::>
5 changes: 5 additions & 0 deletions fixtures/single-group-passing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("group 1", () => {
test("test 1", () => {
expect(1).toBe(1);
});
});
10 changes: 10 additions & 0 deletions fixtures/toplevel-test-failing.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<IT::>test 1

<FAILED::>Test Failed

<LOG:HTML:Failure><pre class="ansi"><code>Error: expect(received).toBe(expected) // Object.is equality<:LF:><:LF:>Expected: 2<:LF:>Received: 1</code></pre>

<COMPLETEDIN::>3

<LOG:HTML:Failures><pre class="ansi"><code>● test 1<:LF:><:LF:> expect(received).toBe(expected) // Object.is equality<:LF:><:LF:> Expected: 2<:LF:> Received: 1<:LF:><:LF:> 1 | test("test 1", () =&gt; {<:LF:> &gt; 2 | expect(1).toBe(2);<:LF:> | ^<:LF:> 3 | });<:LF:> 4 | <:LF:><:LF:> at Object.&lt;anonymous&gt; (fixtures/toplevel-test-failing.js:2:13)</code></pre>
3 changes: 3 additions & 0 deletions fixtures/toplevel-test-failing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("test 1", () => {
expect(1).toBe(2);
});
6 changes: 6 additions & 0 deletions fixtures/toplevel-test-passing.expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

<IT::>test 1

<PASSED::>Test Passed

<COMPLETEDIN::>2
3 changes: 3 additions & 0 deletions fixtures/toplevel-test-passing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test("test 1", () => {
expect(1).toBe(1);
});
7 changes: 7 additions & 0 deletions gen-fixture.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# ./gen-fixture.sh fixtures/example.js
# ./gen-fixture.sh fixtures/example.js expected
# ./gen-fixture.sh fixtures/example.js sample
# for f in $(ls fixtures/*.js); do ./gen-fixture.sh "$f"; done

./node_modules/.bin/jest $1 --reporters=./lib/codewars-reporter.js --testMatch='**/fixtures/*.js' > "${1%.js}.${2:-expected}.txt"
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
26 changes: 17 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
{
"name": "@codewars/jest-reporter",
"version": "1.0.1",
"version": "1.0.2",
"description": "Codewars reporter for Jest",
"homepage": "https://github.com/codewars/jest-reporter#readme",
"repository": "github:codewars/jest-reporter",
"bugs": {
"url": "https://github.com/codewars/jest-reporter/issues"
},
"files": [
"lib/",
"src/"
"lib/codewars-reporter.js",
"src/codewars-reporter.ts"
],
"main": "lib/jest-reporter.js",
"main": "lib/codewars-reporter.js",
"scripts": {
"prepare": "tsc"
"prepare": "tsc",
"watch": "tsc --watch",
"test": "jest"
},
"license": "MIT",
"dependencies": {
"anser": "^1.4.9"
},
"devDependencies": {
"@jest/console": "^25.5.0",
"@jest/reporters": "^25.5.1",
"@jest/test-result": "^25.5.0",
"@jest/console": "^26.1.0",
"@jest/reporters": "^26.1.0",
"@jest/test-result": "^26.1.0",
"@types/jest": "^26.0.4",
"escape-string-regexp": "^4.0.0",
"execa": "^4.0.3",
"husky": "^4.2.5",
"jest": "^26.1.0",
"jest-cli": "^26.1.0",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"ts-jest": "^26.1.1",
"typescript": "^3.8.3"
},
"peerDependencies": {
"jest": "25.x"
"jest": ">=25.x"
},
"husky": {
"hooks": {
Expand Down
72 changes: 72 additions & 0 deletions src/codewars-reporter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import fs from "fs";
import path from "path";
import { promisify } from "util";

import execa from "execa";
import escapeRegExp from "escape-string-regexp";

const readFile = promisify(fs.readFile);
const access = promisify(fs.access);
const exists = async (path: string) => {
try {
await access(path);
return true;
} catch {
return false;
}
};

const structureMatches = (a: string, b: string) =>
expect(
a.match(/<(?:DESCRIBE|IT|PASSED|FAILED|ERROR|COMPLETEDIN)::>/g)!.join("")
).toStrictEqual(
b.match(/<(?:DESCRIBE|IT|PASSED|FAILED|ERROR|COMPLETEDIN)::>/g)!.join("")
);

describe("CodewarsReporter", () => {
const cwd = path.join(__dirname, "..");
const fixturesDir = path.join(__dirname, "..", "fixtures");
const files = fs.readdirSync(fixturesDir).filter((f) => f.endsWith(".js"));
for (const file of files) {
it(file.replace(/\.js$/, ""), async () => {
const { stdout } = await execa(
"jest",
[
path.join(fixturesDir, file),
"--reporters=./lib/codewars-reporter.js",
"--testMatch='**/fixtures/*.js'",
],
{
cwd,
preferLocal: true,
stripFinalNewline: false,
reject: false,
}
);
const expectedFile = path.join(
fixturesDir,
file.replace(/\.js$/, ".expected.txt")
);

if (await exists(expectedFile)) {
const expected = await readFile(expectedFile, {
encoding: "utf-8",
});
// Allow duration to change.
const expectedPattern = new RegExp(
escapeRegExp(expected).replace(/(?<=<COMPLETEDIN::>)\d+/g, "\\d+")
);
expect(stdout).toMatch(expectedPattern);
} else {
const samplePath = path.join(
fixturesDir,
file.replace(/\.js$/, ".sample.txt")
);
const sample = await readFile(samplePath, {
encoding: "utf-8",
});
structureMatches(stdout, sample);
}
});
}
});
2 changes: 1 addition & 1 deletion src/jest-reporter.ts → src/codewars-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function _logTest(test: AssertionResult) {
console.log(`\n<LOG::>Test TODO`);
break;
}
const time = test.duration ? test.duration.toFixed(0) : "";
const time = test.duration ? test.duration.toFixed(0) : "0";
console.log(`\n<COMPLETEDIN::>${time}`);
}

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"declaration": false,
"sourceMap": true,
"removeComments": true
}
},
"exclude": ["node_modules", "**/*.spec.ts"]
}
Loading

0 comments on commit ddbabdc

Please sign in to comment.