node-tdd 3.4.3 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -176,6 +176,13 @@ loaded from `envVarsFile` (if allowed).
176
176
 
177
177
  To allow overwriting of environment variables, prefix the name of the environment variable with `^`.
178
178
 
179
+ #### clearCache
180
+
181
+ Type: `boolean`<br>
182
+ Default: `true`
183
+
184
+ Known accessed caches will be cleared after test has executed when set to `true`.
185
+
179
186
  #### timestamp
180
187
 
181
188
  Type: `number|string`<br>
@@ -0,0 +1,57 @@
1
+ import assert from 'assert';
2
+ import LRU from 'lru-cache-ext';
3
+
4
+ const getFns = (obj) => {
5
+ const result = [];
6
+ const properties = [];
7
+ let o = obj;
8
+ while (o instanceof Object) {
9
+ properties.push(...Object.getOwnPropertyNames(o));
10
+ o = Object.getPrototypeOf(o);
11
+ }
12
+ for (let i = 0; i < properties.length; i += 1) {
13
+ const key = properties[i];
14
+ const value = LRU.prototype[key];
15
+ if (typeof value !== 'function') {
16
+ // eslint-disable-next-line no-continue
17
+ continue;
18
+ }
19
+ result.push({ obj, key, value });
20
+ }
21
+ return result;
22
+ };
23
+
24
+ export default () => {
25
+ let injected = false;
26
+ const fns = [
27
+ ...getFns(LRU.prototype)
28
+ ];
29
+ const caches = [];
30
+
31
+ return {
32
+ inject: () => {
33
+ assert(injected === false);
34
+ fns.forEach(({ obj, key, value }) => {
35
+ try {
36
+ // eslint-disable-next-line no-param-reassign,func-names
37
+ obj[key] = function (...args) {
38
+ caches.push(this);
39
+ return value.call(this, ...args);
40
+ };
41
+ } catch (e) { /* ignored */ }
42
+ });
43
+ injected = true;
44
+ },
45
+ release: () => {
46
+ assert(injected === true);
47
+ fns.forEach(({ obj, key, value }) => {
48
+ try {
49
+ // eslint-disable-next-line no-param-reassign
50
+ obj[key] = value;
51
+ } catch (e) { /* ignored */ }
52
+ });
53
+ caches.splice(0).forEach((c) => c.clear());
54
+ injected = false;
55
+ }
56
+ };
57
+ };
package/lib/util/desc.js CHANGED
@@ -12,6 +12,7 @@ import EnvManager from '../modules/env-manager.js';
12
12
  import TimeKeeper from '../modules/time-keeper.js';
13
13
  import LogRecorder from '../modules/log-recorder.js';
14
14
  import RandomSeeder from '../modules/random-seeder.js';
15
+ import CacheClearer from '../modules/cache-clearer.js';
15
16
  import { getParents, genCassetteName } from './mocha-test.js';
16
17
 
17
18
  const mocha = {
@@ -53,6 +54,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
53
54
  fixtureFolder: Joi.string().optional(),
54
55
  envVarsFile: Joi.string().optional(),
55
56
  envVars: Joi.object().optional().unknown(true).pattern(Joi.string(), Joi.string()),
57
+ clearCache: Joi.boolean().optional(),
56
58
  timestamp: Joi.alternatives(
57
59
  Joi.number().integer().min(0),
58
60
  Joi.date().iso()
@@ -75,6 +77,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
75
77
  const fixtureFolder = resolve(get(opts, 'fixtureFolder', '$FILENAME__fixtures'));
76
78
  const envVarsFile = resolve(get(opts, 'envVarsFile', '$FILENAME.env.yml'));
77
79
  const envVars = get(opts, 'envVars', null);
80
+ const clearCache = get(opts, 'clearCache', true);
78
81
  const timestamp = get(opts, 'timestamp', null);
79
82
  const record = get(opts, 'record', false);
80
83
  const cryptoSeed = get(opts, 'cryptoSeed', null);
@@ -84,6 +87,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
84
87
 
85
88
  let dir = null;
86
89
  let requestRecorder = null;
90
+ let cacheClearer = null;
87
91
  let envManagerFile = null;
88
92
  let envManagerDesc = null;
89
93
  let timeKeeper = null;
@@ -130,6 +134,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
130
134
  // eslint-disable-next-line func-names
131
135
  mocha.before(function () {
132
136
  return (async () => {
137
+ if (clearCache !== false) {
138
+ cacheClearer = CacheClearer();
139
+ }
133
140
  if (getParents(this.test).length === 3 && fs.existsSync(envVarsFile)) {
134
141
  envManagerFile = EnvManager({ envVars: fs.smartRead(envVarsFile), allowOverwrite: false });
135
142
  envManagerFile.apply();
@@ -190,6 +197,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
190
197
  // eslint-disable-next-line func-names
191
198
  mocha.beforeEach(function () {
192
199
  return (async () => {
200
+ if (cacheClearer !== null) {
201
+ cacheClearer.inject();
202
+ }
193
203
  if (useTmpDir === true) {
194
204
  tmp.setGracefulCleanup();
195
205
  dir = tmp.dirSync({ keep: false, unsafeCleanup: true }).name;
@@ -222,6 +232,9 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
222
232
  if (dir !== null) {
223
233
  dir = null;
224
234
  }
235
+ if (cacheClearer !== null) {
236
+ cacheClearer.release();
237
+ }
225
238
  })();
226
239
  });
227
240
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-tdd",
3
3
  "type": "module",
4
- "version": "3.4.3",
4
+ "version": "3.5.0",
5
5
  "description": "Drop in extension for mocha to abstract commonly used test setups",
6
6
  "main": "lib/index.js",
7
7
  "scripts": {
@@ -81,6 +81,7 @@
81
81
  "joi-strict": "2.0.1",
82
82
  "lodash.clonedeep": "4.5.0",
83
83
  "lodash.get": "4.4.2",
84
+ "lru-cache-ext": "3.0.2",
84
85
  "minimist": "1.2.6",
85
86
  "nock": "13.2.4",
86
87
  "object-scan": "18.0.1",