From 052ca736d2df34370d81ce2d24dba23e556f032e Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Wed, 2 Feb 2022 07:46:24 -0600 Subject: [PATCH] fix: more ES imports, some corresponding import changes, reset mock FS after every test --- src/app.ts | 3 ++- src/config/args.ts | 3 ++- src/rule/RuleVisitor.ts | 6 ++++-- src/rule/SchemaRule.ts | 5 ++++- src/rule/index.ts | 6 ++++-- src/source.ts | 6 ++++++ test/TestApp.ts | 6 +++++- test/config/TestConfig.ts | 12 ++++++------ test/parser/TestYamlParser.ts | 6 +++--- test/rule/TestLoadRule.ts | 8 ++++---- test/rule/TestRule.ts | 10 +++++----- test/rule/TestRuleVisitor.ts | 6 +++--- test/rule/TestSchemaRule.ts | 8 ++++---- test/visitor/TestContext.ts | 4 ++-- 14 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/app.ts b/src/app.ts index 020da3f..ae3c197 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,6 @@ import { createLogger } from 'bunyan'; -import { showCompletionScript } from 'yargs'; +import yargs from 'yargs'; +const { showCompletionScript } = yargs; import { loadConfig } from './config/index.js'; import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs } from './config/args.js'; diff --git a/src/config/args.ts b/src/config/args.ts index ac42387..b17669b 100644 --- a/src/config/args.ts +++ b/src/config/args.ts @@ -1,4 +1,5 @@ -import { Options, usage } from 'yargs'; +import yargs, { Options } from 'yargs'; +const { usage } = yargs; import { RuleSelector, RuleSources } from '../rule/index.js'; import { VERSION_INFO } from '../version.js'; diff --git a/src/rule/RuleVisitor.ts b/src/rule/RuleVisitor.ts index 4e12575..c7acc1f 100644 --- a/src/rule/RuleVisitor.ts +++ b/src/rule/RuleVisitor.ts @@ -1,6 +1,8 @@ import { hasItems } from '@apextoaster/js-utils'; -import { applyDiff, diff } from 'deep-diff'; -import { cloneDeep } from 'lodash'; +import deepDiff from 'deep-diff'; +const { applyDiff, diff } = deepDiff; +import lodash from 'lodash'; +const { cloneDeep } = lodash; import { Rule } from './index.js'; import { Visitor } from '../visitor/index.js'; diff --git a/src/rule/SchemaRule.ts b/src/rule/SchemaRule.ts index 34bc202..79cd69f 100644 --- a/src/rule/SchemaRule.ts +++ b/src/rule/SchemaRule.ts @@ -1,12 +1,15 @@ import { doesExist, hasItems } from '@apextoaster/js-utils'; import { ErrorObject, ValidateFunction } from 'ajv'; -import { cloneDeep, defaultTo } from 'lodash'; import { LogLevel } from 'noicejs'; import { Rule, RuleData } from './index.js'; import { Visitor, VisitorError, VisitorResult } from '../visitor/index.js'; import { VisitorContext } from '../visitor/VisitorContext.js'; +import lodash from 'lodash'; +const { cloneDeep, defaultTo } = lodash; + + /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/strict-boolean-expressions */ const DEFAULT_FILTER = () => true; diff --git a/src/rule/index.ts b/src/rule/index.ts index 2cde16c..ab5cecd 100644 --- a/src/rule/index.ts +++ b/src/rule/index.ts @@ -1,7 +1,9 @@ import { doesExist, ensureArray } from '@apextoaster/js-utils'; import { ValidateFunction } from 'ajv'; -import { intersection } from 'lodash'; -import { Minimatch } from 'minimatch'; +import lodash from 'lodash'; +const { intersection } = lodash; +import minimatch from 'minimatch'; +const { Minimatch } = minimatch; import { LogLevel } from 'noicejs'; // import ruleSchemaData from '../../rules/salty-dog.yml'; diff --git a/src/source.ts b/src/source.ts index e3bba36..a5b9f34 100644 --- a/src/source.ts +++ b/src/source.ts @@ -28,6 +28,12 @@ export function setFs(fs: Filesystem) { }; } +export function resetFs() { + readdir = promises.readdir; + readFile = promises.readFile; + writeFile = promises.writeFile; +} + export async function listFiles(path: string): Promise> { const dirs: Array = [path]; const files: Array = []; diff --git a/test/TestApp.ts b/test/TestApp.ts index 8bb253c..560ff92 100644 --- a/test/TestApp.ts +++ b/test/TestApp.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import { vol } from 'memfs'; import { main, STATUS_ERROR, STATUS_SUCCESS } from '../src/app.js'; -import { Filesystem, readSource, setFs } from '../src/source.js'; +import { Filesystem, readSource, resetFs, setFs } from '../src/source.js'; const TEST_ARGS_PRE = ['node', 'test']; const TEST_ARGS_CONFIG = ['--config-path', 'docs', '--config-name', 'config.yml']; @@ -39,6 +39,10 @@ describe('main app', async () => { vol.reset(); }); + afterEach(() => { + resetFs(); + }); + it('completion should succeed', async () => { const status = await main(['node', 'test', 'complete']); expect(status).to.equal(STATUS_SUCCESS); diff --git a/test/config/TestConfig.ts b/test/config/TestConfig.ts index aaf4282..33656c5 100644 --- a/test/config/TestConfig.ts +++ b/test/config/TestConfig.ts @@ -2,10 +2,10 @@ import { NotFoundError } from '@apextoaster/js-utils'; import { expect } from 'chai'; import { join } from 'path'; -import { loadConfig, readConfig } from '../../src/config'; +import { loadConfig, readConfig } from '../../src/config/index.js'; describe('load config helper', async () => { - it('should load an existing config', () => + it('should load an existing config', async () => expect(loadConfig('config-stderr.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({ data: { logger: { @@ -17,11 +17,11 @@ describe('load config helper', async () => { }) ); - it('should throw when config is missing', () => + it('should throw when config is missing', async () => expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError) ); - it('should load included config', () => + it('should load included config', async () => expect(loadConfig('config-include.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({ data: { include: { @@ -33,11 +33,11 @@ describe('load config helper', async () => { }); describe('read config helper', async () => { - it('should consume enoent errors', () => + it('should consume enoent errors', async () => expect(readConfig(join('docs', 'missing.yml'))).to.eventually.equal(undefined) ); - it('should rethrow unknown errors', () => + it('should rethrow unknown errors', async () => expect(readConfig('test')).to.eventually.be.rejectedWith(Error) ); }); diff --git a/test/parser/TestYamlParser.ts b/test/parser/TestYamlParser.ts index ac5557d..70ae2a3 100644 --- a/test/parser/TestYamlParser.ts +++ b/test/parser/TestYamlParser.ts @@ -1,10 +1,10 @@ import { expect } from 'chai'; -import { YamlParser } from '../../src/parser/YamlParser'; +import { YamlParser } from '../../src/parser/YamlParser.js'; describe('yaml parser', () => { describe('dump documents', () => { - it('should dump multiple documents', () => { + it('should dump multiple documents', async () => { const parser = new YamlParser(); const data = parser.dump({}, {}); @@ -13,7 +13,7 @@ describe('yaml parser', () => { }); describe('parse documents', () => { - it('should parse multiple documents', () => { + it('should parse multiple documents', async () => { const parser = new YamlParser(); const data = parser.parse(` foo: {} diff --git a/test/rule/TestLoadRule.ts b/test/rule/TestLoadRule.ts index 54b88c7..cd0b382 100644 --- a/test/rule/TestLoadRule.ts +++ b/test/rule/TestLoadRule.ts @@ -3,10 +3,10 @@ import { vol } from 'memfs'; import { LogLevel, NullLogger } from 'noicejs'; import { spy, stub } from 'sinon'; -import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule'; -import { SchemaRule } from '../../src/rule/SchemaRule'; -import { Filesystem, setFs } from '../../src/source'; -import { VisitorContext } from '../../src/visitor/VisitorContext'; +import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule/index.js'; +import { SchemaRule } from '../../src/rule/SchemaRule.js'; +import { Filesystem, setFs } from '../../src/source.js'; +import { VisitorContext } from '../../src/visitor/VisitorContext.js'; const EXAMPLE_EMPTY = '{name: foo, definitions: {}, rules: []}'; const EXAMPLE_RULES = `{ diff --git a/test/rule/TestRule.ts b/test/rule/TestRule.ts index a2fb947..0d8a152 100644 --- a/test/rule/TestRule.ts +++ b/test/rule/TestRule.ts @@ -1,9 +1,9 @@ import { expect } from 'chai'; import { ConsoleLogger, LogLevel, NullLogger } from 'noicejs'; -import { createRuleSelector, createRuleSources, resolveRules, validateRules } from '../../src/rule'; -import { SchemaRule } from '../../src/rule/SchemaRule'; -import { VisitorContext } from '../../src/visitor/VisitorContext'; +import { createRuleSelector, createRuleSources, resolveRules, validateRules } from '../../src/rule/index.js'; +import { SchemaRule } from '../../src/rule/SchemaRule.js'; +import { VisitorContext } from '../../src/visitor/VisitorContext.js'; const TEST_RULES = [new SchemaRule({ check: {}, @@ -118,7 +118,7 @@ describe('rule resolver', async () => { }); describe('create rule sources helper', () => { - it('should ensure every field is an array', () => { + it('should ensure every field is an array', async () => { const sources = createRuleSources({}); expect(sources).to.have.deep.property('ruleFile', []); @@ -128,7 +128,7 @@ describe('create rule sources helper', () => { }); describe('create rule selector helper', () => { - it('should ensure every field is an array', () => { + it('should ensure every field is an array', async () => { const sources = createRuleSelector({}); expect(sources).to.have.deep.property('excludeLevel', []); diff --git a/test/rule/TestRuleVisitor.ts b/test/rule/TestRuleVisitor.ts index ab27f89..1e9ce98 100644 --- a/test/rule/TestRuleVisitor.ts +++ b/test/rule/TestRuleVisitor.ts @@ -2,9 +2,9 @@ import { expect } from 'chai'; import { LogLevel, NullLogger } from 'noicejs'; import { mock, spy, stub } from 'sinon'; -import { RuleVisitor } from '../../src/rule/RuleVisitor'; -import { SchemaRule } from '../../src/rule/SchemaRule'; -import { VisitorContext } from '../../src/visitor/VisitorContext'; +import { RuleVisitor } from '../../src/rule/RuleVisitor.js'; +import { SchemaRule } from '../../src/rule/SchemaRule.js'; +import { VisitorContext } from '../../src/visitor/VisitorContext.js'; describe('rule visitor', async () => { it('should only call visit for selected items', async () => { diff --git a/test/rule/TestSchemaRule.ts b/test/rule/TestSchemaRule.ts index 257a038..c50cb51 100644 --- a/test/rule/TestSchemaRule.ts +++ b/test/rule/TestSchemaRule.ts @@ -2,8 +2,8 @@ import { expect } from 'chai'; import { LogLevel, NullLogger } from 'noicejs'; import { stub } from 'sinon'; -import { friendlyError, SchemaRule } from '../../src/rule/SchemaRule'; -import { VisitorContext } from '../../src/visitor/VisitorContext'; +import { friendlyError, SchemaRule } from '../../src/rule/SchemaRule.js'; +import { VisitorContext } from '../../src/visitor/VisitorContext.js'; /* eslint-disable @typescript-eslint/unbound-method */ @@ -208,7 +208,7 @@ function createErrorContext() { } describe('friendly errors', () => { - it('should have a message', () => { + it('should have a message', async () => { const { ctx } = createErrorContext(); const err = friendlyError(ctx, { instancePath: 'test-path', @@ -219,7 +219,7 @@ describe('friendly errors', () => { expect(err.msg).to.include(TEST_NAME); }); - it('should handle errors with an existing message', () => { + it('should handle errors with an existing message', async () => { const { ctx } = createErrorContext(); const TEST_MESSAGE = 'test-message'; const err = friendlyError(ctx, { diff --git a/test/visitor/TestContext.ts b/test/visitor/TestContext.ts index a30adf3..d4e8993 100644 --- a/test/visitor/TestContext.ts +++ b/test/visitor/TestContext.ts @@ -1,10 +1,10 @@ import { expect } from 'chai'; import { LogLevel, NullLogger } from 'noicejs'; -import { VisitorContext } from '../../src/visitor/VisitorContext'; +import { VisitorContext } from '../../src/visitor/VisitorContext.js'; describe('visitor context', () => { - it('should merge results', () => { + it('should merge results', async () => { const firstCtx = new VisitorContext({ logger: NullLogger.global, schemaOptions: {