node-tdd 3.3.0 → 3.3.3

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/lib/util/desc.js CHANGED
@@ -1,35 +1,18 @@
1
- "use strict";
2
-
3
- const assert = require('assert');
4
-
5
- const path = require('path');
6
-
7
- const fs = require('smart-fs');
8
-
9
- const callsites = require('callsites');
10
-
11
- const get = require('lodash.get');
12
-
13
- const minimist = require('minimist');
14
-
15
- const tmp = require('tmp');
16
-
17
- const Joi = require('joi-strict');
18
-
19
- const RequestRecorder = require('../modules/request-recorder');
20
-
21
- const EnvManager = require('../modules/env-manager');
22
-
23
- const TimeKeeper = require('../modules/time-keeper');
24
-
25
- const LogRecorder = require('../modules/log-recorder');
26
-
27
- const RandomSeeder = require('../modules/random-seeder');
28
-
29
- const {
30
- getParents,
31
- genCassetteName
32
- } = require('./mocha-test');
1
+ import assert from 'assert';
2
+ import path from 'path';
3
+ import fs from 'smart-fs';
4
+ import callsites from 'callsites';
5
+ import get from 'lodash.get';
6
+ import { fileURLToPath } from 'url';
7
+ import minimist from 'minimist';
8
+ import tmp from 'tmp';
9
+ import Joi from 'joi-strict';
10
+ import RequestRecorder from '../modules/request-recorder.js';
11
+ import EnvManager from '../modules/env-manager.js';
12
+ import TimeKeeper from '../modules/time-keeper.js';
13
+ import LogRecorder from '../modules/log-recorder.js';
14
+ import RandomSeeder from '../modules/random-seeder.js';
15
+ import { getParents, genCassetteName } from './mocha-test.js';
33
16
 
34
17
  const mocha = {
35
18
  it,
@@ -45,9 +28,19 @@ const mocha = {
45
28
  const desc = (suiteName, optsOrTests, testsOrNull = null) => {
46
29
  const opts = testsOrNull === null ? {} : optsOrTests;
47
30
  const tests = testsOrNull === null ? optsOrTests : testsOrNull;
48
- const testFile = path.resolve(callsites()[1].getFileName());
49
31
 
50
- const resolve = name => path.join(path.dirname(testFile), name.replace(/\$FILENAME/g, path.basename(testFile)));
32
+ const filenameOrUrl = callsites()[1].getFileName();
33
+ const testFile = path.resolve(
34
+ filenameOrUrl.startsWith('file://')
35
+ ? fileURLToPath(filenameOrUrl)
36
+ // eslint-disable-next-line @blackflux/rules/c8-prevent-ignore
37
+ /* c8 ignore next */
38
+ : filenameOrUrl
39
+ );
40
+ const resolve = (name) => path.join(
41
+ path.dirname(testFile),
42
+ name.replace(/\$FILENAME/g, path.basename(testFile))
43
+ );
51
44
 
52
45
  Joi.assert(opts, Joi.object().keys({
53
46
  useTmpDir: Joi.boolean().optional(),
@@ -55,10 +48,15 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
55
48
  nockFolder: Joi.string().optional(),
56
49
  nockModifiers: Joi.object().optional().pattern(Joi.string(), Joi.function()),
57
50
  nockStripHeaders: Joi.boolean().optional(),
51
+ nockReqHeaderOverwrite: Joi.object().optional()
52
+ .pattern(Joi.string(), Joi.alternatives(Joi.function(), Joi.string())),
58
53
  fixtureFolder: Joi.string().optional(),
59
54
  envVarsFile: Joi.string().optional(),
60
55
  envVars: Joi.object().optional().unknown(true).pattern(Joi.string(), Joi.string()),
61
- timestamp: Joi.alternatives(Joi.number().integer().min(0), Joi.date().iso()).optional(),
56
+ timestamp: Joi.alternatives(
57
+ Joi.number().integer().min(0),
58
+ Joi.date().iso()
59
+ ).optional(),
62
60
  record: Joi.any().optional(),
63
61
  cryptoSeed: Joi.string().optional(),
64
62
  cryptoSeedReseed: Joi.when('cryptoSeed', {
@@ -83,6 +81,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
83
81
  const cryptoSeedReseed = get(opts, 'cryptoSeedReseed', false);
84
82
  const timeout = get(opts, 'timeout', null);
85
83
  const nockHeal = get(minimist(process.argv.slice(2)), 'nock-heal', false);
84
+
86
85
  let dir = null;
87
86
  let requestRecorder = null;
88
87
  let envManagerFile = null;
@@ -92,31 +91,22 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
92
91
  let randomSeeder = null;
93
92
 
94
93
  const getArgs = () => ({
95
- capture: async fn => {
94
+ capture: async (fn) => {
96
95
  try {
97
96
  await fn();
98
97
  } catch (e) {
99
98
  return e;
100
99
  }
101
-
102
- throw new assert.AssertionError({
103
- message: 'expected [Function] to throw an error'
104
- });
100
+ throw new assert.AssertionError({ message: 'expected [Function] to throw an error' });
105
101
  },
106
- fixture: name => {
102
+ fixture: (name) => {
107
103
  const filepath = fs.guessFile(path.join(fixtureFolder, name));
108
-
109
104
  if (filepath === null) {
110
- throw new assert.AssertionError({
111
- message: `fixture "${name}" not found or ambiguous`
112
- });
105
+ throw new assert.AssertionError({ message: `fixture "${name}" not found or ambiguous` });
113
106
  }
114
-
115
107
  return fs.smartRead(filepath);
116
108
  },
117
- ...(dir === null ? {} : {
118
- dir
119
- }),
109
+ ...(dir === null ? {} : { dir }),
120
110
  ...(logRecorder === null ? {} : {
121
111
  recorder: {
122
112
  verbose: logRecorder.verbose,
@@ -125,56 +115,37 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
125
115
  }
126
116
  })
127
117
  });
128
-
129
118
  let beforeCb = () => {};
130
-
131
119
  let afterCb = () => {};
132
-
133
120
  let beforeEachCb = () => {};
121
+ let afterEachCb = () => {};
134
122
 
135
- let afterEachCb = () => {}; // eslint-disable-next-line func-names
136
-
137
-
123
+ // eslint-disable-next-line func-names
138
124
  mocha.describe(suiteName, function () {
139
125
  return (async () => {
140
126
  if (timeout !== null) {
141
127
  this.timeout(timeout);
142
- } // eslint-disable-next-line func-names
143
-
128
+ }
144
129
 
130
+ // eslint-disable-next-line func-names
145
131
  mocha.before(function () {
146
132
  return (async () => {
147
133
  if (getParents(this.test).length === 3 && fs.existsSync(envVarsFile)) {
148
- envManagerFile = EnvManager({
149
- envVars: fs.smartRead(envVarsFile),
150
- allowOverwrite: false
151
- });
134
+ envManagerFile = EnvManager({ envVars: fs.smartRead(envVarsFile), allowOverwrite: false });
152
135
  envManagerFile.apply();
153
136
  }
154
-
155
137
  if (envVars !== null) {
156
- envManagerDesc = EnvManager({
157
- envVars,
158
- allowOverwrite: false
159
- });
138
+ envManagerDesc = EnvManager({ envVars, allowOverwrite: false });
160
139
  envManagerDesc.apply();
161
140
  }
162
-
163
141
  if (timestamp !== null) {
164
- timeKeeper = TimeKeeper({
165
- timestamp
166
- });
142
+ timeKeeper = TimeKeeper({ timestamp });
167
143
  timeKeeper.inject();
168
144
  }
169
-
170
145
  if (cryptoSeed !== null) {
171
- randomSeeder = RandomSeeder({
172
- seed: cryptoSeed,
173
- reseed: cryptoSeedReseed
174
- });
146
+ randomSeeder = RandomSeeder({ seed: cryptoSeed, reseed: cryptoSeedReseed });
175
147
  randomSeeder.inject();
176
148
  }
177
-
178
149
  if (useNock === true) {
179
150
  requestRecorder = RequestRecorder({
180
151
  cassetteFolder: `${nockFolder}/`,
@@ -185,56 +156,47 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
185
156
  modifiers: nockModifiers
186
157
  });
187
158
  }
188
-
189
159
  await beforeCb.call(this);
190
160
  })();
191
- }); // eslint-disable-next-line func-names
161
+ });
192
162
 
163
+ // eslint-disable-next-line func-names
193
164
  mocha.after(function () {
194
165
  return (async () => {
195
166
  if (requestRecorder !== null) {
196
167
  requestRecorder.shutdown();
197
168
  requestRecorder = null;
198
169
  }
199
-
200
170
  if (randomSeeder !== null) {
201
171
  randomSeeder.release();
202
172
  randomSeeder = null;
203
173
  }
204
-
205
174
  if (timeKeeper !== null) {
206
175
  timeKeeper.release();
207
176
  timeKeeper = null;
208
177
  }
209
-
210
178
  if (envManagerDesc !== null) {
211
179
  envManagerDesc.unapply();
212
180
  envManagerDesc = null;
213
181
  }
214
-
215
182
  if (envManagerFile !== null) {
216
183
  envManagerFile.unapply();
217
184
  envManagerFile = null;
218
185
  }
219
-
220
186
  await afterCb.call(this);
221
187
  })();
222
- }); // eslint-disable-next-line func-names
188
+ });
223
189
 
190
+ // eslint-disable-next-line func-names
224
191
  mocha.beforeEach(function () {
225
192
  return (async () => {
226
193
  if (useTmpDir === true) {
227
194
  tmp.setGracefulCleanup();
228
- dir = tmp.dirSync({
229
- keep: false,
230
- unsafeCleanup: true
231
- }).name;
195
+ dir = tmp.dirSync({ keep: false, unsafeCleanup: true }).name;
232
196
  }
233
-
234
197
  if (useNock === true) {
235
198
  await requestRecorder.inject(genCassetteName(this.currentTest));
236
199
  }
237
-
238
200
  if (record !== false) {
239
201
  logRecorder = LogRecorder({
240
202
  verbose: process.argv.slice(2).includes('--verbose'),
@@ -242,61 +204,56 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
242
204
  });
243
205
  logRecorder.inject();
244
206
  }
245
-
246
207
  await beforeEachCb.call(this, getArgs());
247
208
  })();
248
- }); // eslint-disable-next-line func-names
209
+ });
249
210
 
211
+ // eslint-disable-next-line func-names
250
212
  mocha.afterEach(function () {
251
213
  return (async () => {
252
214
  await afterEachCb.call(this, getArgs());
253
-
254
215
  if (logRecorder !== null) {
255
216
  logRecorder.release();
256
217
  logRecorder = null;
257
218
  }
258
-
259
219
  if (requestRecorder !== null) {
260
220
  await requestRecorder.release();
261
221
  }
262
-
263
222
  if (dir !== null) {
264
223
  dir = null;
265
224
  }
266
225
  })();
267
226
  });
268
- const globalsPrev = Object.keys(mocha).reduce((p, key) => Object.assign(p, {
269
- [key]: global[key]
270
- }));
271
-
272
- global.it = (testName, fn) => mocha.it(testName, fn.length === 0 || /^[^(=]*\({/.test(fn.toString()) // eslint-disable-next-line func-names
273
- ? function () {
274
- return fn.call(this, getArgs());
275
- } // eslint-disable-next-line func-names
276
- : function (done) {
277
- return fn.call(this, done);
278
- });
279
227
 
228
+ const globalsPrev = Object.keys(mocha)
229
+ .reduce((p, key) => Object.assign(p, { [key]: global[key] }));
230
+ global.it = (testName, fn) => mocha.it(
231
+ testName,
232
+ fn.length === 0 || /^[^(=]*\({/.test(fn.toString())
233
+ // eslint-disable-next-line func-names
234
+ ? function () {
235
+ return fn.call(this, getArgs());
236
+ }
237
+ // eslint-disable-next-line func-names
238
+ : function (done) {
239
+ return fn.call(this, done);
240
+ }
241
+ );
280
242
  global.specify = global.it;
281
243
  global.describe = desc;
282
244
  global.context = global.describe;
283
-
284
- global.before = fn => {
245
+ global.before = (fn) => {
285
246
  beforeCb = fn;
286
247
  };
287
-
288
- global.after = fn => {
248
+ global.after = (fn) => {
289
249
  afterCb = fn;
290
250
  };
291
-
292
- global.beforeEach = fn => {
251
+ global.beforeEach = (fn) => {
293
252
  beforeEachCb = fn;
294
253
  };
295
-
296
- global.afterEach = fn => {
254
+ global.afterEach = (fn) => {
297
255
  afterEachCb = fn;
298
256
  };
299
-
300
257
  await tests.call(this);
301
258
  Object.entries(globalsPrev).forEach(([k, v]) => {
302
259
  global[k] = v;
@@ -304,5 +261,4 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
304
261
  })();
305
262
  });
306
263
  };
307
-
308
- module.exports = desc;
264
+ export default desc;
@@ -1,19 +1,19 @@
1
- "use strict";
2
-
3
- const getParents = test => {
1
+ export const getParents = (test) => {
4
2
  const names = [];
5
3
  let cTest = test;
6
-
7
4
  while (cTest !== undefined) {
8
5
  names.splice(0, 0, cTest.title);
9
6
  cTest = cTest.parent;
10
7
  }
11
-
12
8
  return names;
13
9
  };
14
10
 
15
- module.exports.getParents = getParents;
16
-
17
- const genCassetteName = test => getParents(test).filter(e => !!e).map(e => e.replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-+|-+$/g, '').replace(/^./, c => c.toLowerCase()).replace(/-(.)/g, (_, char) => char.toUpperCase())).concat(['recording.json']).join('_');
18
-
19
- module.exports.genCassetteName = genCassetteName;
11
+ export const genCassetteName = (test) => getParents(test)
12
+ .filter((e) => !!e)
13
+ .map((e) => e
14
+ .replace(/[^a-zA-Z0-9]+/g, '-')
15
+ .replace(/^-+|-+$/g, '')
16
+ .replace(/^./, (c) => c.toLowerCase())
17
+ .replace(/-(.)/g, (_, char) => char.toUpperCase()))
18
+ .concat(['recording.json'])
19
+ .join('_');
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
2
  "name": "node-tdd",
3
- "version": "3.3.0",
3
+ "type": "module",
4
+ "version": "3.3.3",
4
5
  "description": "Drop in extension for mocha to abstract commonly used test setups",
5
6
  "main": "lib/index.js",
6
7
  "scripts": {
7
8
  "test": "yarn run clean && yarn run gardener && yarn run test-simple",
8
9
  "clean": "rm -rf lib",
9
- "build": "npx babel src --out-dir lib --copy-files --include-dotfiles --config-file ./.babelrc",
10
+ "build": "cp -rf ./src ./lib",
10
11
  "build-clean": "yarn run clean && yarn run build",
11
- "test-simple": "nyc mocha \"./test/**/*.spec.js\"",
12
+ "test-simple": "c8 mocha --experimental-loader=./test/hot.js \"./test/**/*.spec.js\"",
12
13
  "docker": "docker run --net host -u`id -u`:`id -g` -v $(pwd):/user/project -v ~/.aws:/user/.aws -v ~/.npmrc:/user/.npmrc -w /user/project -it --entrypoint /bin/bash",
13
14
  "t": "yarn test",
14
15
  "ts": "yarn run test-simple",
15
16
  "tsv": "yarn run test-simple --verbose",
16
17
  "coveralls": "node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info",
17
18
  "semantic-release": "yarn run build-clean && npx semantic-release",
18
- "gardener": "node gardener",
19
+ "gardener": "node gardener.js",
19
20
  "u": "yarn upgrade --latest --force",
20
21
  "i": "yarn install --frozen-lockfile",
21
22
  "it": "yarn run i && yarn run t"
@@ -41,29 +42,26 @@
41
42
  },
42
43
  "homepage": "https://github.com/blackflux/node-tdd#readme",
43
44
  "devDependencies": {
44
- "@babel/cli": "7.14.8",
45
- "@babel/core": "7.15.0",
46
- "@babel/register": "7.15.3",
47
- "@blackflux/eslint-plugin-rules": "2.0.3",
48
- "@blackflux/robo-config-plugin": "5.3.0",
49
- "aws-sdk": "2.981.0",
50
- "aws-sdk-wrap": "9.6.0",
51
- "axios": "0.24.0",
52
- "babel-eslint": "10.1.0",
53
- "babel-preset-latest-node": "5.5.1",
54
- "chai": "4.3.4",
45
+ "@babel/core": "7.17.9",
46
+ "@babel/eslint-parser": "7.17.0",
47
+ "@babel/register": "7.17.7",
48
+ "@blackflux/eslint-plugin-rules": "2.1.0",
49
+ "@blackflux/robo-config-plugin": "7.7.6",
50
+ "aws-sdk": "2.1112.0",
51
+ "aws-sdk-wrap": "12.0.4",
52
+ "axios": "0.26.1",
53
+ "c8": "7.11.0",
54
+ "chai": "4.3.6",
55
55
  "coveralls": "3.1.1",
56
- "eslint": "7.32.0",
57
- "eslint-config-airbnb-base": "14.2.1",
58
- "eslint-plugin-import": "2.24.2",
56
+ "eslint": "8.13.0",
57
+ "eslint-config-airbnb-base": "15.0.0",
58
+ "eslint-plugin-import": "2.26.0",
59
59
  "eslint-plugin-json": "3.1.0",
60
- "eslint-plugin-markdown": "2.2.0",
61
- "eslint-plugin-mocha": "9.0.0",
62
- "fancy-log": "1.3.3",
63
- "js-gardener": "3.0.3",
64
- "lambda-monitor-logger": "3.0.0",
65
- "nyc": "15.1.0",
66
- "semantic-release": "17.4.7"
60
+ "eslint-plugin-markdown": "2.2.1",
61
+ "eslint-plugin-mocha": "10.0.3",
62
+ "fancy-log": "2.0.0",
63
+ "js-gardener": "3.0.5",
64
+ "lambda-monitor-logger": "3.2.2"
67
65
  },
68
66
  "licenses": [
69
67
  {
@@ -72,51 +70,21 @@
72
70
  }
73
71
  ],
74
72
  "engines": {
75
- "node": ">= 12"
76
- },
77
- "nyc": {
78
- "exclude": [
79
- "gardener.js",
80
- "node_modules/*",
81
- "coverage/*",
82
- "lib/*"
83
- ],
84
- "tempDir": "./coverage/.nyc_output",
85
- "report-dir": "./coverage",
86
- "check-coverage": true,
87
- "per-file": false,
88
- "lines": 100,
89
- "statements": 100,
90
- "functions": 100,
91
- "branches": 100,
92
- "include": [
93
- "**/*.js"
94
- ],
95
- "reporter": [
96
- "lcov",
97
- "text-summary"
98
- ],
99
- "require": [
100
- "@babel/register"
101
- ],
102
- "extension": [],
103
- "cache": true,
104
- "all": true,
105
- "babel": true
73
+ "node": ">= 14"
106
74
  },
107
75
  "files": [
108
76
  "lib"
109
77
  ],
110
78
  "dependencies": {
111
- "callsites": "3.1.0",
79
+ "callsites": "4.0.0",
112
80
  "compare-urls": "2.0.0",
113
- "joi-strict": "2.0.0",
81
+ "joi-strict": "2.0.1",
114
82
  "lodash.clonedeep": "4.5.0",
115
83
  "lodash.get": "4.4.2",
116
- "minimist": "1.2.5",
117
- "nock": "13.1.3",
118
- "object-scan": "17.0.0",
119
- "smart-fs": "2.0.2",
84
+ "minimist": "1.2.6",
85
+ "nock": "13.2.4",
86
+ "object-scan": "18.0.1",
87
+ "smart-fs": "3.0.1",
120
88
  "timekeeper": "2.2.0",
121
89
  "tmp": "0.2.1",
122
90
  "uuid": "8.3.0",