Skip to content

Commit 0ca2ee0

Browse files
committed
Add tests that it works with commonjs as well
1 parent 2d63195 commit 0ca2ee0

8 files changed

Lines changed: 98 additions & 2 deletions

File tree

.mocharc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"timeout": 10000,
33
"reporter": "spec",
44
"recursive": true,
5+
"ignore": ["test/commonjs/**"],
56
"ui": "mocha-cakes-2",
67
"require": ["./test/helpers/setup.mjs"],
78
"exit": true

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config from "./index.js";
22

33
export default [
4-
{ ignores: [ "test/data/**/*", "test/helpers/**/*" ] },
4+
{ ignores: [ "test/data/**/*", "test/helpers/**/*", "test/commonjs/**/*" ] },
55
...config,
66
];

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"type": "module",
66
"main": "index.js",
77
"scripts": {
8-
"test": "mocha && eslint ."
8+
"test-esm": "mocha",
9+
"test-cjs": "cd test/commonjs && mocha",
10+
"test": "npm run test-esm && npm run test-cjs && eslint ."
911
},
1012
"repository": {
1113
"type": "git",

test/commonjs/.mocharc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"timeout": 10000,
3+
"reporter": "spec",
4+
"recursive": true,
5+
"spec": "feature/**/*.mjs",
6+
"ui": "mocha-cakes-2",
7+
"require": ["../helpers/setup.mjs"],
8+
"exit": true
9+
}

test/commonjs/data/js/extends.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
class Foo {
3+
constructor(bar) {
4+
this.bar = bar;
5+
}
6+
}
7+
8+
class Bar extends Foo {
9+
foo(foo) {
10+
this.bar = foo;
11+
}
12+
}
13+
14+
function fooer() {
15+
return new Bar("test");
16+
}
17+
18+
fooer();

test/commonjs/data/js/noerrors.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
function add(a, b) {
4+
return a + b;
5+
}
6+
7+
add(2, 3);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { ESLint } from "eslint";
2+
3+
Feature("linting js files (commonjs)", () => {
4+
Scenario("js file passing lint rules", () => {
5+
let eslint;
6+
Given("we have an eslint instance with the base config", () => {
7+
eslint = new ESLint({
8+
overrideConfigFile: "../../js.js",
9+
ignore: false,
10+
});
11+
});
12+
13+
let results;
14+
When("we lint a folder", async () => {
15+
results = await eslint.lintFiles([ "data/js/noerrors.js" ]);
16+
});
17+
18+
Then("we should have linted the correct number of files", () => {
19+
expect(results.length).to.eql(1);
20+
});
21+
22+
And("one file should have no messages", () => {
23+
expect(results[0].messages.length).to.eql(0, JSON.stringify(results[0].messages));
24+
});
25+
});
26+
27+
Scenario("js file does not pass, tries to extend class", () => {
28+
let eslint;
29+
Given("we have an eslint instance with the base config", () => {
30+
eslint = new ESLint({
31+
overrideConfigFile: "../../js.js",
32+
ignore: false,
33+
});
34+
});
35+
36+
let results;
37+
When("we lint a file which extends a class", async () => {
38+
results = await eslint.lintFiles([ "data/js/extends.js" ]);
39+
});
40+
41+
Then("we should have linted the correct number of files", () => {
42+
expect(results.length).to.eql(1);
43+
});
44+
45+
And("one file should have errors", () => {
46+
expect(results[0].messages.length).to.be.eql(1, JSON.stringify(results[0].messages));
47+
});
48+
49+
And("one error should be because it extends a class", () => {
50+
const error = results[0].messages.find(({ ruleId }) => ruleId === "@bonniernews/typescript-rules/disallow-class-extends");
51+
expect(error).to.not.be.undefined;
52+
expect(error.message).to.have.string("Extending");
53+
});
54+
});
55+
});

test/commonjs/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "commonjs-test",
3+
"private": true
4+
}

0 commit comments

Comments
 (0)