From 013b1d7dfb2ac59beace332152a151b0e38aa7e9 Mon Sep 17 00:00:00 2001 From: ssube Date: Mon, 28 Oct 2019 22:23:10 -0500 Subject: [PATCH] fix(tests): begin testing rules --- package.json | 4 +++ test/TestRule.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++++- test/harness.ts | 21 +++++++++++++++- yarn.lock | 31 ++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index dd1a8a7..b437ac4 100644 --- a/package.json +++ b/package.json @@ -35,16 +35,19 @@ "@istanbuljs/nyc-config-typescript": "0.1.3", "@types/bunyan": "1.8.6", "@types/chai": "4.2.4", + "@types/chai-as-promised": "^7.1.2", "@types/deep-diff": "1.0.0", "@types/js-yaml": "3.12.1", "@types/lodash": "4.14.144", "@types/mocha": "5.2.7", "@types/sinon": "7.5.0", + "@types/sinon-chai": "^3.2.3", "@types/source-map-support": "0.5.0", "@types/yargs": "13.0.3", "ajv": "6.10.2", "bunyan": "1.8.12", "chai": "4.2.0", + "chai-as-promised": "^7.1.1", "deep-diff": "1.0.2", "js-yaml": "3.13.1", "jsonpath-plus": "1.1.0", @@ -61,6 +64,7 @@ "rollup-plugin-tslint": "0.2.2", "rollup-plugin-typescript2": "0.24.3", "sinon": "7.5.0", + "sinon-chai": "^3.3.0", "source-map-support": "0.5.15", "standard-version": "7.0.0", "tslint": "5.20.0", diff --git a/test/TestRule.ts b/test/TestRule.ts index 52f7aa3..21b2b7d 100644 --- a/test/TestRule.ts +++ b/test/TestRule.ts @@ -1,6 +1,6 @@ import { expect } from 'chai'; import { ConsoleLogger } from 'noicejs'; -import { mock } from 'sinon'; +import { mock, spy } from 'sinon'; import { makeSelector, resolveRules, Rule, visitRules } from '../src/rule'; import { VisitorContext } from '../src/visitor/VisitorContext'; @@ -167,4 +167,65 @@ describeLeaks('rule visitor', async () => { mockRule.verify(); expect(ctx.errors.length).to.equal(0); }); + + itLeaks('should pick items from the scope', async () => { + const ctx = new VisitorContext({ + innerOptions: { + coerce: false, + defaults: false, + mutate: false, + }, + logger: new ConsoleLogger(), + }); + const data = { + foo: 3, + }; + const rule = new Rule({ + check: {}, + desc: '', + level: 'info', + name: 'foo', + select: '$.foo', + tags: [], + }); + const results = await rule.pick(ctx, data); + + expect(results).to.deep.equal([data.foo]); + }); + + itLeaks('should filter out items', async () => { + const ctx = new VisitorContext({ + innerOptions: { + coerce: false, + defaults: false, + mutate: false, + }, + logger: new ConsoleLogger(), + }); + const errorSpy = spy(ctx, 'error'); + + const data = { + foo: 3, + }; + const rule = new Rule({ + check: {}, + desc: '', + filter: { + properties: { + foo: { + type: 'number', + }, + }, + type: 'object', + }, + level: 'info', + name: 'foo', + select: '$.foo', + tags: [], + }); + + const results = await rule.visit(ctx, data); + expect(results.errors.length).to.equal(0); + expect(errorSpy).to.have.callCount(0); + }); }); diff --git a/test/harness.ts b/test/harness.ts index 0b06326..d7fd151 100644 --- a/test/harness.ts +++ b/test/harness.ts @@ -1,3 +1,22 @@ +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import sinonChai from 'sinon-chai'; import sourceMapSupport from 'source-map-support'; -sourceMapSupport.install(); +sourceMapSupport.install({ + environment: 'node', + handleUncaughtExceptions: true, + hookRequire: true, +}); + +/** + * This will break the whole test run if any test leaks an unhandled rejection. + */ +process.on('unhandledRejection', (reason, promise) => { + // tslint:disable-next-line:no-console + console.error('unhandled error during tests', reason); + process.exit(1); +}); + +chai.use(chaiAsPromised); +chai.use(sinonChai); diff --git a/yarn.lock b/yarn.lock index 955a27f..88767c3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -130,7 +130,14 @@ dependencies: "@types/node" "*" -"@types/chai@4.2.4": +"@types/chai-as-promised@^7.1.2": + version "7.1.2" + resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/chai-as-promised/-/chai-as-promised-7.1.2.tgz#2f564420e81eaf8650169e5a3a6b93e096e5068b" + integrity sha512-PO2gcfR3Oxa+u0QvECLe1xKXOqYTzCmWf0FhLhjREoW3fPAVamjihL7v1MOVLJLsnAMdLcjkfrs01yvDMwVK4Q== + dependencies: + "@types/chai" "*" + +"@types/chai@*", "@types/chai@4.2.4": version "4.2.4" resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/chai/-/chai-4.2.4.tgz#8936cffad3c96ec470a2dc26a38c3ba8b9b6f619" integrity sha512-7qvf9F9tMTzo0akeswHPGqgUx/gIaJqrOEET/FCD8CFRkSUHlygQiM5yB6OvjrtdxBVLSyw7COJubsFYs0683g== @@ -172,7 +179,15 @@ dependencies: "@types/node" "*" -"@types/sinon@7.5.0": +"@types/sinon-chai@^3.2.3": + version "3.2.3" + resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/sinon-chai/-/sinon-chai-3.2.3.tgz#afe392303dda95cc8069685d1e537ff434fa506e" + integrity sha512-TOUFS6vqS0PVL1I8NGVSNcFaNJtFoyZPXZ5zur+qlhDfOmQECZZM4H4kKgca6O8L+QceX/ymODZASfUfn+y4yQ== + dependencies: + "@types/chai" "*" + "@types/sinon" "*" + +"@types/sinon@*", "@types/sinon@7.5.0": version "7.5.0" resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/sinon/-/sinon-7.5.0.tgz#f5a10c27175465a0b001b68d8b9f761582967cc6" integrity sha512-NyzhuSBy97B/zE58cDw4NyGvByQbAHNP9069KVSgnXt/sc0T6MFRh0InKAeBVHJWdSXG1S3+PxgVIgKo9mTHbw== @@ -380,6 +395,13 @@ camelcase@^5.0.0: resolved "https://artifacts.apextoaster.com/repository/group-npm/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320" integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg== +chai-as-promised@^7.1.1: + version "7.1.1" + resolved "https://artifacts.apextoaster.com/repository/group-npm/chai-as-promised/-/chai-as-promised-7.1.1.tgz#08645d825deb8696ee61725dbf590c012eb00ca0" + integrity sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA== + dependencies: + check-error "^1.0.2" + chai@4.2.0: version "4.2.0" resolved "https://artifacts.apextoaster.com/repository/group-npm/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5" @@ -2307,6 +2329,11 @@ signal-exit@^3.0.0, signal-exit@^3.0.2: resolved "https://artifacts.apextoaster.com/repository/group-npm/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" integrity sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0= +sinon-chai@^3.3.0: + version "3.3.0" + resolved "https://artifacts.apextoaster.com/repository/group-npm/sinon-chai/-/sinon-chai-3.3.0.tgz#8084ff99451064910fbe2c2cb8ab540c00b740ea" + integrity sha512-r2JhDY7gbbmh5z3Q62pNbrjxZdOAjpsqW/8yxAZRSqLZqowmfGZPGUZPFf3UX36NLis0cv8VEM5IJh9HgkSOAA== + sinon@7.5.0: version "7.5.0" resolved "https://artifacts.apextoaster.com/repository/group-npm/sinon/-/sinon-7.5.0.tgz#e9488ea466070ea908fd44a3d6478fd4923c67ec"