node-tdd 3.3.1 → 3.4.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/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,11 +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(),
58
- nockReqHeaderOverwrite: Joi.object().optional().pattern(Joi.string(), Joi.alternatives(Joi.function(), Joi.string())),
51
+ nockReqHeaderOverwrite: Joi.object().optional()
52
+ .pattern(Joi.string(), Joi.alternatives(Joi.function(), Joi.string())),
59
53
  fixtureFolder: Joi.string().optional(),
60
54
  envVarsFile: Joi.string().optional(),
61
55
  envVars: Joi.object().optional().unknown(true).pattern(Joi.string(), Joi.string()),
62
- 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(),
63
60
  record: Joi.any().optional(),
64
61
  cryptoSeed: Joi.string().optional(),
65
62
  cryptoSeedReseed: Joi.when('cryptoSeed', {
@@ -84,6 +81,7 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
84
81
  const cryptoSeedReseed = get(opts, 'cryptoSeedReseed', false);
85
82
  const timeout = get(opts, 'timeout', null);
86
83
  const nockHeal = get(minimist(process.argv.slice(2)), 'nock-heal', false);
84
+
87
85
  let dir = null;
88
86
  let requestRecorder = null;
89
87
  let envManagerFile = null;
@@ -93,31 +91,22 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
93
91
  let randomSeeder = null;
94
92
 
95
93
  const getArgs = () => ({
96
- capture: async fn => {
94
+ capture: async (fn) => {
97
95
  try {
98
96
  await fn();
99
97
  } catch (e) {
100
98
  return e;
101
99
  }
102
-
103
- throw new assert.AssertionError({
104
- message: 'expected [Function] to throw an error'
105
- });
100
+ throw new assert.AssertionError({ message: 'expected [Function] to throw an error' });
106
101
  },
107
- fixture: name => {
102
+ fixture: (name) => {
108
103
  const filepath = fs.guessFile(path.join(fixtureFolder, name));
109
-
110
104
  if (filepath === null) {
111
- throw new assert.AssertionError({
112
- message: `fixture "${name}" not found or ambiguous`
113
- });
105
+ throw new assert.AssertionError({ message: `fixture "${name}" not found or ambiguous` });
114
106
  }
115
-
116
107
  return fs.smartRead(filepath);
117
108
  },
118
- ...(dir === null ? {} : {
119
- dir
120
- }),
109
+ ...(dir === null ? {} : { dir }),
121
110
  ...(logRecorder === null ? {} : {
122
111
  recorder: {
123
112
  verbose: logRecorder.verbose,
@@ -126,56 +115,37 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
126
115
  }
127
116
  })
128
117
  });
129
-
130
118
  let beforeCb = () => {};
131
-
132
119
  let afterCb = () => {};
133
-
134
120
  let beforeEachCb = () => {};
121
+ let afterEachCb = () => {};
135
122
 
136
- let afterEachCb = () => {}; // eslint-disable-next-line func-names
137
-
138
-
123
+ // eslint-disable-next-line func-names
139
124
  mocha.describe(suiteName, function () {
140
125
  return (async () => {
141
126
  if (timeout !== null) {
142
127
  this.timeout(timeout);
143
- } // eslint-disable-next-line func-names
144
-
128
+ }
145
129
 
130
+ // eslint-disable-next-line func-names
146
131
  mocha.before(function () {
147
132
  return (async () => {
148
133
  if (getParents(this.test).length === 3 && fs.existsSync(envVarsFile)) {
149
- envManagerFile = EnvManager({
150
- envVars: fs.smartRead(envVarsFile),
151
- allowOverwrite: false
152
- });
134
+ envManagerFile = EnvManager({ envVars: fs.smartRead(envVarsFile), allowOverwrite: false });
153
135
  envManagerFile.apply();
154
136
  }
155
-
156
137
  if (envVars !== null) {
157
- envManagerDesc = EnvManager({
158
- envVars,
159
- allowOverwrite: false
160
- });
138
+ envManagerDesc = EnvManager({ envVars, allowOverwrite: false });
161
139
  envManagerDesc.apply();
162
140
  }
163
-
164
141
  if (timestamp !== null) {
165
- timeKeeper = TimeKeeper({
166
- timestamp
167
- });
142
+ timeKeeper = TimeKeeper({ timestamp });
168
143
  timeKeeper.inject();
169
144
  }
170
-
171
145
  if (cryptoSeed !== null) {
172
- randomSeeder = RandomSeeder({
173
- seed: cryptoSeed,
174
- reseed: cryptoSeedReseed
175
- });
146
+ randomSeeder = RandomSeeder({ seed: cryptoSeed, reseed: cryptoSeedReseed });
176
147
  randomSeeder.inject();
177
148
  }
178
-
179
149
  if (useNock === true) {
180
150
  requestRecorder = RequestRecorder({
181
151
  cassetteFolder: `${nockFolder}/`,
@@ -186,56 +156,47 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
186
156
  modifiers: nockModifiers
187
157
  });
188
158
  }
189
-
190
159
  await beforeCb.call(this);
191
160
  })();
192
- }); // eslint-disable-next-line func-names
161
+ });
193
162
 
163
+ // eslint-disable-next-line func-names
194
164
  mocha.after(function () {
195
165
  return (async () => {
196
166
  if (requestRecorder !== null) {
197
167
  requestRecorder.shutdown();
198
168
  requestRecorder = null;
199
169
  }
200
-
201
170
  if (randomSeeder !== null) {
202
171
  randomSeeder.release();
203
172
  randomSeeder = null;
204
173
  }
205
-
206
174
  if (timeKeeper !== null) {
207
175
  timeKeeper.release();
208
176
  timeKeeper = null;
209
177
  }
210
-
211
178
  if (envManagerDesc !== null) {
212
179
  envManagerDesc.unapply();
213
180
  envManagerDesc = null;
214
181
  }
215
-
216
182
  if (envManagerFile !== null) {
217
183
  envManagerFile.unapply();
218
184
  envManagerFile = null;
219
185
  }
220
-
221
186
  await afterCb.call(this);
222
187
  })();
223
- }); // eslint-disable-next-line func-names
188
+ });
224
189
 
190
+ // eslint-disable-next-line func-names
225
191
  mocha.beforeEach(function () {
226
192
  return (async () => {
227
193
  if (useTmpDir === true) {
228
194
  tmp.setGracefulCleanup();
229
- dir = tmp.dirSync({
230
- keep: false,
231
- unsafeCleanup: true
232
- }).name;
195
+ dir = tmp.dirSync({ keep: false, unsafeCleanup: true }).name;
233
196
  }
234
-
235
197
  if (useNock === true) {
236
198
  await requestRecorder.inject(genCassetteName(this.currentTest));
237
199
  }
238
-
239
200
  if (record !== false) {
240
201
  logRecorder = LogRecorder({
241
202
  verbose: process.argv.slice(2).includes('--verbose'),
@@ -243,61 +204,56 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
243
204
  });
244
205
  logRecorder.inject();
245
206
  }
246
-
247
207
  await beforeEachCb.call(this, getArgs());
248
208
  })();
249
- }); // eslint-disable-next-line func-names
209
+ });
250
210
 
211
+ // eslint-disable-next-line func-names
251
212
  mocha.afterEach(function () {
252
213
  return (async () => {
253
214
  await afterEachCb.call(this, getArgs());
254
-
255
215
  if (logRecorder !== null) {
256
216
  logRecorder.release();
257
217
  logRecorder = null;
258
218
  }
259
-
260
219
  if (requestRecorder !== null) {
261
220
  await requestRecorder.release();
262
221
  }
263
-
264
222
  if (dir !== null) {
265
223
  dir = null;
266
224
  }
267
225
  })();
268
226
  });
269
- const globalsPrev = Object.keys(mocha).reduce((p, key) => Object.assign(p, {
270
- [key]: global[key]
271
- }));
272
-
273
- global.it = (testName, fn) => mocha.it(testName, fn.length === 0 || /^[^(=]*\({/.test(fn.toString()) // eslint-disable-next-line func-names
274
- ? function () {
275
- return fn.call(this, getArgs());
276
- } // eslint-disable-next-line func-names
277
- : function (done) {
278
- return fn.call(this, done);
279
- });
280
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
+ );
281
242
  global.specify = global.it;
282
243
  global.describe = desc;
283
244
  global.context = global.describe;
284
-
285
- global.before = fn => {
245
+ global.before = (fn) => {
286
246
  beforeCb = fn;
287
247
  };
288
-
289
- global.after = fn => {
248
+ global.after = (fn) => {
290
249
  afterCb = fn;
291
250
  };
292
-
293
- global.beforeEach = fn => {
251
+ global.beforeEach = (fn) => {
294
252
  beforeEachCb = fn;
295
253
  };
296
-
297
- global.afterEach = fn => {
254
+ global.afterEach = (fn) => {
298
255
  afterEachCb = fn;
299
256
  };
300
-
301
257
  await tests.call(this);
302
258
  Object.entries(globalsPrev).forEach(([k, v]) => {
303
259
  global[k] = v;
@@ -305,5 +261,4 @@ const desc = (suiteName, optsOrTests, testsOrNull = null) => {
305
261
  })();
306
262
  });
307
263
  };
308
-
309
- 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.1",
3
+ "type": "module",
4
+ "version": "3.4.0",
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.10",
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.12",
50
+ "aws-sdk": "2.1131.0",
51
+ "aws-sdk-wrap": "12.1.3",
52
+ "axios": "0.27.2",
53
+ "c8": "7.11.2",
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.15.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",