Skip to content

Commit

Permalink
Merge pull request #225 from yowainwright/init-add-more-tests
Browse files Browse the repository at this point in the history
Init add more tests
  • Loading branch information
yowainwright authored Jan 3, 2025
2 parents b634521 + 2e16103 commit a9ba05e
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 4 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Ensuring that JavaScript files can pass ES Check is important in a [modular and

## Version 8 🎉

**ES Check** version 8 is a major release update that can enforce actual ES version specific features checks; no longer just that a files are syntatically correct to the es version. To enable this feature, just pass the `--checkFeatures` flag. This feature will become default in version 9. Besides this, there are minor feature updates based on user feedback—glob matching updates and some optional fixes.
**ES Check** version 8 is a major release update that can enforce actual ES version specific features checks; no longer just that a files are syntatically correct to the es version. To enable this feature, just pass the `--checkFeatures` flag. This feature will become default in version 9. Besides this, there are minor feature updates based on user feedback—glob matching updates and some optional fixes. This should not break any existing scripts. Please report any issues!

```sh
es-check es6 './dist/**/*.js' --checkFeatures
Expand All @@ -39,7 +39,6 @@ es-check es6 './dist/**/*.js' --checkFeatures
<a href="#debugging">Debugging</a>&nbsp;&nbsp;
<a href="#contributing">Contributing</a>&nbsp;&nbsp;
<a href="/issues">Issues</a>
<a href="#roadmap">Roadmap</a>
</p>

---
Expand Down Expand Up @@ -73,6 +72,10 @@ es-check es5 './vendor/js/*.js' './dist/**/*.js'

In modern JavaScript builds, files are bundled up so they can be served in an optimized manner in the browsers. It is assumed by developers that future JavaScript—like ES8 will be transpiled (changed from future JavaScript to current JavaScript) appropriately by a tool like Babel. Sometimes there is an issue where files are not transpiled. There was no efficient way to test for files that weren't transpiled—until now. That's what ES Check does.

## What features does ES Check check for?

ES Check checks syntax out of the box—to protect against breaking errors going to production. Additionally, by adding the `--checkFeatures` flag, ES Check will check for actual ES version specific features. This ensures that your code is syntactically correct and only using features that are available in the specified ES version. Look here to view/add [features](./constants.js) that ES Check checks for with the `--checkFeatures` flag!

---

## Walk through
Expand All @@ -99,7 +102,7 @@ Fail

# USAGE

index.js es-check <ecmaVersion> [files...]
es-check <ecmaVersion> [files...]

```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"release": "release-it",
"report:coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
"setup": "pnpm install --reporter=silent",
"test": "nyc mocha test.js --timeout 10s --coverage",
"test": "nyc mocha test.js utils.test.js --timeout 10s",
"update": "codependence --update"
},
"repository": {
Expand Down
129 changes: 129 additions & 0 deletions utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// test/checkFunctions.spec.js

const assert = require('assert');
const {
checkVarKindMatch,
checkCalleeMatch,
checkOperatorMatch,
checkDefault,
checkMap,
} = require('./utils');

describe('Check Functions', function () {
describe('checkVarKindMatch', function () {
it('should return false if astInfo.kind is not provided', function () {
const node = { kind: 'const' };
const astInfo = {}; // no "kind"
assert.strictEqual(checkVarKindMatch(node, astInfo), false);
});

it('should return true if node.kind equals astInfo.kind', function () {
const node = { kind: 'const' };
const astInfo = { kind: 'const' };
assert.strictEqual(checkVarKindMatch(node, astInfo), true);
});

it('should return false if node.kind does not match astInfo.kind', function () {
const node = { kind: 'let' };
const astInfo = { kind: 'const' };
assert.strictEqual(checkVarKindMatch(node, astInfo), false);
});
});

describe('checkCalleeMatch', function () {
it('should return false if astInfo.callee is not provided', function () {
const node = {
callee: { type: 'Identifier', name: 'Promise' },
};
const astInfo = {}; // no "callee"
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});

it('should return false if node.callee is missing', function () {
const node = {};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});

it('should return false if node.callee.type !== "Identifier"', function () {
const node = {
callee: { type: 'MemberExpression', name: 'Promise' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});

it('should return false if the callee name does not match astInfo.callee', function () {
const node = {
callee: { type: 'Identifier', name: 'WeakRef' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), false);
});

it('should return true if callee name matches astInfo.callee', function () {
const node = {
callee: { type: 'Identifier', name: 'Promise' },
};
const astInfo = { callee: 'Promise' };
assert.strictEqual(checkCalleeMatch(node, astInfo), true);
});
});

describe('checkOperatorMatch', function () {
it('should return false if astInfo.operator is not provided', function () {
const node = { operator: '??' };
const astInfo = {}; // no "operator"
assert.strictEqual(checkOperatorMatch(node, astInfo), false);
});

it('should return false if node.operator does not match astInfo.operator', function () {
const node = { operator: '**' };
const astInfo = { operator: '??' };
assert.strictEqual(checkOperatorMatch(node, astInfo), false);
});

it('should return true if node.operator matches astInfo.operator', function () {
const node = { operator: '??' };
const astInfo = { operator: '??' };
assert.strictEqual(checkOperatorMatch(node, astInfo), true);
});
});

describe('checkDefault', function () {
it('should always return true', function () {
assert.strictEqual(checkDefault(), true);
});
});

describe('checkMap usage examples', function () {
it('checkMap.VariableDeclaration should call checkVarKindMatch internally and return correct boolean', function () {
const node = { kind: 'const' };
const astInfo = { kind: 'const' };
const result = checkMap.VariableDeclaration(node, astInfo);
assert.strictEqual(result, true);

const nodeMismatch = { kind: 'let' };
const result2 = checkMap.VariableDeclaration(nodeMismatch, astInfo);
assert.strictEqual(result2, false);
});

it('checkMap.LogicalExpression should call checkOperatorMatch internally', function () {
const node = { operator: '??' };
const astInfo = { operator: '??' };
const result = checkMap.LogicalExpression(node, astInfo);
assert.strictEqual(result, true);
});

it('checkMap.ArrowFunctionExpression should call checkDefault internally', function () {
const result = checkMap.ArrowFunctionExpression();
assert.strictEqual(result, true);
});

it('checkMap.default should return false if node type is unsupported', function () {
const result = checkMap.default();
assert.strictEqual(result, false);
});
});

});

0 comments on commit a9ba05e

Please sign in to comment.