Skip to content

Commit

Permalink
Add linting for ES Modules
Browse files Browse the repository at this point in the history
Also fixes the linting issues and some feedback from the PR.
  • Loading branch information
fatso83 committed Mar 5, 2018
1 parent 6959188 commit 51cdafe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -24,7 +24,9 @@
"test": "run-s test-node test-headless test-webworker test-es-module",
"check-dependencies": "dependency-check package.json --unused --no-dev --ignore-module coveralls --ignore-module @std/esm",
"build": "node ./build.js",
"lint": "eslint .",
"lint": "run-p lint-js lint-mjs lint-markdown",
"lint-js": "eslint .",
"lint-mjs": "eslint --ext mjs --parser-options=sourceType:module test/es2015",
"lint-markdown": "find docs -type f -name '*.md' ! -name 'changelog.md' | xargs markdownlint",
"precommit": "lint-staged",
"pretest-webworker": "npm run build",
Expand All @@ -35,6 +37,7 @@
},
"lint-staged": {
"*.js": "eslint",
"*.mjs": "eslint --ext mjs --parser-options=sourceType:module",
"docs/**/*.md": "markdownlint"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion test/es2015/a-function-module.mjs
@@ -1 +1 @@
export default function (){ return 42; }
export default function () { return 42; }
4 changes: 2 additions & 2 deletions test/es2015/a-module-with-default.mjs
@@ -1,3 +1,3 @@
export default {
anExport(){ return 42; }
}
anExport() { return 42; }
};
2 changes: 1 addition & 1 deletion test/es2015/a-module.mjs
@@ -1 +1 @@
export function anExport(){ return 42; }
export function anExport() { return 42; }
44 changes: 23 additions & 21 deletions test/es2015/module-support-assessment-test.mjs
@@ -1,55 +1,57 @@
"use strict";

import referee from "referee";
import sinon from "../../lib/sinon";
import * as aModule from "./a-module";
import aModuleWithDefaultExport from "./a-module-with-default";

// Usually one would import the default module, but one can make a form of wrapper like this
/* eslint-disable no-unused-vars */
import functionModule, * as functionModuleAlternative from "./a-function-module";
const {assert, refute} = referee;

function createTestSuite(action){
function createTestSuite(action) {
var stub;
var errorRegEx = /TypeError: ES Modules cannot be (stubbed|spied)/;

afterEach(function() {
stub && stub.restore && stub.restore();
});
describe("sinon." + action + "()", function () {

describe("sinon." + action + "()", function(){
describe("Modules with objects as their default export", function() {
afterEach(function() {
stub && stub.restore && stub.restore();
});
it("should NOT result in error", function() {
refute.exception(function() {
afterEach(function () {
if (stub && stub.restore) { stub.restore(); }
});

describe("Modules with objects as their default export", function () {

it("should NOT result in error", function () {
refute.exception(function () {
stub = sinon[action](aModuleWithDefaultExport, "anExport");
});
});

it("should spy/stub an exported function", function() {
it("should spy/stub an exported function", function () {
stub = sinon[action](aModuleWithDefaultExport, "anExport");
aModuleWithDefaultExport.anExport();
aModuleWithDefaultExport.anExport();
assert(stub.callCount === 2);
});
});

describe("Modules without default export", function() {
it("should give a proper error message", function() {
assert.exception(function() {
describe("Modules without default export", function () {
it("should give a proper error message", function () {
assert.exception(function () {
sinon[action](aModule, "anExport");
}, errorRegEx);
});
});

describe("Modules that exports a function as their default export", function() {
it("should not be possible to spy/stub the default export using a wrapper for the exports", function() {
assert.exception(function() {
describe("Modules that exports a function as their default export", function () {
it("should not be possible to spy/stub the default export using a wrapper for the exports", function () {
assert.exception(function () {
stub = sinon[action](functionModuleAlternative, "anExport");
}, errorRegEx);
});
});
});
}

createTestSuite('stub');
createTestSuite('spy');
createTestSuite("stub");
createTestSuite("spy");

0 comments on commit 51cdafe

Please sign in to comment.