Repository: stoplightio/spectral
Pull request #1025 (2020-03-23 13:00:20)

test(jest): nanoid mock

**Checklist**

- [x] Tests added / updated
- [x] Docs added / updated

**Does this PR introduce a breaking change?**

- [ ] Yes
- [x] No




Patch:
diff --git a/__mocks__/nanoid/non-secure.ts b/__mocks__/nanoid/non-secure.ts
new file mode 100644
index 00000000..3c280b83
--- /dev/null
+++ b/__mocks__/nanoid/non-secure.ts
@@ -0,0 +1,7 @@
+let seed = 0;
+
+beforeEach(() => {
+  seed = 0;
+});
+
+module.exports = jest.fn(() => `random-id-${seed++}`);
diff --git a/package.json b/package.json
index 6559da12..c1609cee 100644
--- a/package.json
+++ b/package.json
@@ -75,7 +75,7 @@
     "fast-glob": "^3.1.0",
     "jsonpath-plus": "~2.0",
     "lodash": ">=4.17.5",
-    "nanoid": "^2.1.6",
+    "nanoid": "^2.1.11",
     "node-fetch": "^2.6",
     "proxy-agent": "^3.1.1",
     "strip-ansi": "^6.0",
diff --git a/yarn.lock b/yarn.lock
index bd265f19..6d816945 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5004,10 +5004,10 @@ nan@^2.14.0:
   resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
   integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
 
-nanoid@^2.1.6:
-  version "2.1.8"
-  resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.1.8.tgz#2dbb0224231b246e3b4c819de7bfea6384dabf08"
-  integrity sha512-g1z+n5s26w0TGKh7gjn7HCqurNKMZWzH08elXzh/gM/csQHd/UqDV6uxMghQYg9IvqRPm1QpeMk50YMofHvEjQ==
+nanoid@^2.1.11:
+  version "2.1.11"
+  resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.1.11.tgz#ec24b8a758d591561531b4176a01e3ab4f0f0280"
+  integrity sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA==
 
 nanomatch@^1.2.9:
   version "1.2.13"


Test patch:
diff --git a/src/rulesets/__tests__/reader.jest.test.ts b/src/rulesets/__tests__/reader.jest.test.ts
index a6273743..9da6665f 100644
--- a/src/rulesets/__tests__/reader.jest.test.ts
+++ b/src/rulesets/__tests__/reader.jest.test.ts
@@ -6,10 +6,9 @@ import * as nock from 'nock';
 import { Spectral } from '../../spectral';
 import { IRule, RuleType } from '../../types';
 import { readRuleset } from '../reader';
-const nanoid = require('nanoid/non-secure');
 
-jest.mock('nanoid/non-secure');
 jest.mock('fs');
+jest.mock('nanoid/non-secure');
 
 const validFlatRuleset = path.join(__dirname, './__fixtures__/valid-flat-ruleset.json');
 const validRequireInfo = path.join(__dirname, './__fixtures__/valid-require-info-ruleset.yaml');
@@ -42,11 +41,6 @@ const oasRulesetRules: Dictionary<IRule, string> = oasRuleset.rules;
 jest.setTimeout(10000);
 
 describe('Rulesets reader', () => {
-  beforeEach(() => {
-    let seed = 0;
-    (nanoid as jest.Mock).mockImplementation(() => `random-id-${seed++}`);
-  });
-
   afterEach(() => {
     nock.cleanAll();
     nock.enableNetConnect();
diff --git a/src/rulesets/mergers/__tests__/functions.jest.test.ts b/src/rulesets/mergers/__tests__/functions.jest.test.ts
index 93a92bce..20b72bb5 100644
--- a/src/rulesets/mergers/__tests__/functions.jest.test.ts
+++ b/src/rulesets/mergers/__tests__/functions.jest.test.ts
@@ -1,16 +1,10 @@
 import { RuleCollection } from '../../../types';
 import { RulesetFunctionCollection } from '../../../types/ruleset';
 import { mergeFunctions } from '../functions';
-const nanoid = require('nanoid/non-secure');
 
 jest.mock('nanoid/non-secure');
 
 describe('Ruleset functions merging', () => {
-  beforeEach(() => {
-    let seed = 0;
-    (nanoid as jest.Mock).mockImplementation(() => `random-id-${seed++}`);
-  });
-
   it('re-writes function names', () => {
     const target = {};
     const sources: RulesetFunctionCollection = {