@approval-tests/approvals 7.5.9 → 7.5.10

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.
@@ -94,7 +94,7 @@ export declare function verifyAsJSONAndScrub(dirName: string, testName: string,
94
94
  /**
95
95
  * This allows you to take full control of naming and writing files before verifying.
96
96
  *
97
- * For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
97
+ * For an example that we use to generate the docs within the readme, check out the [test/mocha/readmeTests.mts](test/mocha/readmeTests.mts) in this project.
98
98
  *
99
99
  * @param {Object} namer
100
100
  * @param {Object} writer
package/lib/Approvals.js CHANGED
@@ -67,6 +67,7 @@ const AUtils_1 = require("./AUtils");
67
67
  // they won't get a helpful error. So we put this in there - just in case"
68
68
  const MochaNamer_1 = require("./Providers/Mocha/MochaNamer");
69
69
  const BeforeEachVerifierBase_1 = require("./Providers/BeforeEachVerifierBase");
70
+ const OptionalFramework_1 = require("./Providers/OptionalFramework");
70
71
  if (typeof beforeEach === "function") {
71
72
  beforeEach(function () {
72
73
  if (!this) {
@@ -125,6 +126,8 @@ function getConfig(overrideOptions) {
125
126
  * @param {*} optionalBaseDir - An optional folder to save approval files to.
126
127
  */
127
128
  function mocha(optionalBaseDir) {
129
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("mocha"))
130
+ return module.exports;
128
131
  // if not providing a base dir, fallback to the current calling code's directory
129
132
  if (!optionalBaseDir) {
130
133
  let fileName = (0, callsite_1.default)()[1].getFileName();
@@ -263,7 +266,7 @@ function verifyAsJSONAndScrub(dirName, testName, data, scrubber, optionsOverride
263
266
  /**
264
267
  * This allows you to take full control of naming and writing files before verifying.
265
268
  *
266
- * For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
269
+ * For an example that we use to generate the docs within the readme, check out the [test/mocha/readmeTests.mts](test/mocha/readmeTests.mts) in this project.
267
270
  *
268
271
  * @param {Object} namer
269
272
  * @param {Object} writer
package/lib/Approvals.ts CHANGED
@@ -26,6 +26,7 @@ import { stringifyKeysInOrder } from "./AUtils";
26
26
  import { MochaNamer } from "./Providers/Mocha/MochaNamer";
27
27
 
28
28
  import { beforeEachVerifierBase } from "./Providers/BeforeEachVerifierBase";
29
+ import { isFrameworkAvailable } from "./Providers/OptionalFramework";
29
30
 
30
31
  /**
31
32
  * Contains some helpful and util scrubbers that can be used for scrubbing data before saving to a received file.
@@ -96,6 +97,7 @@ export function getConfig(overrideOptions?: cfg.Config): cfg.Config {
96
97
  */
97
98
 
98
99
  export function mocha(optionalBaseDir?: string): typeof module.exports {
100
+ if (!isFrameworkAvailable("mocha")) return module.exports;
99
101
  // if not providing a base dir, fallback to the current calling code's directory
100
102
  if (!optionalBaseDir) {
101
103
  let fileName = callsite()[1].getFileName();
@@ -293,7 +295,7 @@ export function verifyAsJSONAndScrub(
293
295
  /**
294
296
  * This allows you to take full control of naming and writing files before verifying.
295
297
  *
296
- * For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
298
+ * For an example that we use to generate the docs within the readme, check out the [test/mocha/readmeTests.mts](test/mocha/readmeTests.mts) in this project.
297
299
  *
298
300
  * @param {Object} namer
299
301
  * @param {Object} writer
@@ -52,7 +52,10 @@ const Options_1 = require("../../Core/Options");
52
52
  const JestNamer_1 = require("./JestNamer");
53
53
  const StringWriter_1 = require("../../StringWriter");
54
54
  const approvals = __importStar(require("../../Approvals"));
55
+ const OptionalFramework_1 = require("../OptionalFramework");
55
56
  function verify(sut, options) {
57
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("jest"))
58
+ return;
56
59
  options = options || new Options_1.Options();
57
60
  options = options.withNamer((0, JestNamer_1.getJestNamer)());
58
61
  const config = options.getConfig(approvals.getConfig());
@@ -15,8 +15,10 @@ import { getJestNamer } from "./JestNamer";
15
15
 
16
16
  import { StringWriter } from "../../StringWriter";
17
17
  import * as approvals from "../../Approvals";
18
+ import { isFrameworkAvailable } from "../OptionalFramework";
18
19
 
19
20
  export function verify(sut: any, options?: Options): void {
21
+ if (!isFrameworkAvailable("jest")) return;
20
22
  options = options || new Options();
21
23
  options = options.withNamer(getJestNamer());
22
24
  const config = options.getConfig(approvals.getConfig());
@@ -4,12 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getJestNamer = getJestNamer;
7
- const globals_1 = require("@jest/globals");
8
7
  const path_1 = __importDefault(require("path"));
9
8
  const Namer_1 = require("../../Namer");
10
9
  const Namer_2 = require("../../Core/Namer");
11
10
  function getJestNamer() {
12
- const state = globals_1.expect.getState();
11
+ const { expect } = require("@jest/globals");
12
+ const state = expect.getState();
13
13
  const file = path_1.default.parse(state.testPath);
14
14
  const testPath = file.dir;
15
15
  const testFileName = file.name;
@@ -1,9 +1,9 @@
1
- import { expect } from "@jest/globals";
2
1
  import path from "path";
3
2
  import { Namer } from "../../Namer";
4
3
  import { convertToFilename } from "../../Core/Namer";
5
4
 
6
5
  export function getJestNamer(): Namer {
6
+ const { expect } = require("@jest/globals") as typeof import("@jest/globals");
7
7
  const state = expect.getState();
8
8
  const file = path.parse(state.testPath as string);
9
9
  const testPath = file.dir;
@@ -4,19 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.JestReporter = void 0;
7
- const globals_1 = require("@jest/globals");
7
+ const OptionalFramework_1 = require("../OptionalFramework");
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  class JestReporter {
10
10
  constructor() {
11
11
  this.name = "JestReporter";
12
12
  }
13
13
  canReportOn(_fileName) {
14
- return true;
14
+ return (0, OptionalFramework_1.isFrameworkAvailable)("jest");
15
15
  }
16
16
  report(approvedFilePath, receivedFilePath, _) {
17
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("jest"))
18
+ return;
19
+ const { expect } = require("@jest/globals");
17
20
  const approvedText = fs_1.default.readFileSync(approvedFilePath).toString();
18
21
  const receivedText = fs_1.default.readFileSync(receivedFilePath).toString();
19
- (0, globals_1.expect)(receivedText).toBe(approvedText);
22
+ expect(receivedText).toBe(approvedText);
20
23
  }
21
24
  }
22
25
  exports.JestReporter = JestReporter;
@@ -1,4 +1,4 @@
1
- import { expect } from "@jest/globals";
1
+ import { isFrameworkAvailable } from "../OptionalFramework";
2
2
  import fs from "fs";
3
3
  import { Config } from "../../config";
4
4
  import { Reporter } from "../../Core/Reporter";
@@ -7,7 +7,7 @@ export class JestReporter implements Reporter {
7
7
  name: string = "JestReporter";
8
8
 
9
9
  canReportOn(_fileName: string) {
10
- return true;
10
+ return isFrameworkAvailable("jest");
11
11
  }
12
12
 
13
13
  report(
@@ -15,6 +15,9 @@ export class JestReporter implements Reporter {
15
15
  receivedFilePath: string,
16
16
  _: Partial<Config>,
17
17
  ) {
18
+ if (!isFrameworkAvailable("jest")) return;
19
+ const { expect } =
20
+ require("@jest/globals") as typeof import("@jest/globals");
18
21
  const approvedText = fs.readFileSync(approvedFilePath).toString();
19
22
  const receivedText = fs.readFileSync(receivedFilePath).toString();
20
23
  expect(receivedText).toBe(approvedText);
@@ -56,12 +56,15 @@ const Printers_1 = require("../../Utilities/Printers");
56
56
  */
57
57
  const Options_1 = require("../../Core/Options");
58
58
  const MochaNamer_1 = require("./MochaNamer");
59
- const mocha_1 = __importDefault(require("mocha"));
59
+ const OptionalFramework_1 = require("../OptionalFramework");
60
60
  const StringWriter_1 = __importDefault(require("../../StringWriter"));
61
61
  const approvals = __importStar(require("../../Approvals"));
62
62
  let mochaTest = null;
63
63
  function it2(label, test) {
64
- mocha_1.default.it(label, function () {
64
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("mocha"))
65
+ return;
66
+ const mocha = require("mocha");
67
+ mocha.it(label, function () {
65
68
  mochaTest = this;
66
69
  console.log("Mocha Test: ", mochaTest.test.name);
67
70
  test();
@@ -69,12 +72,16 @@ function it2(label, test) {
69
72
  }
70
73
  let namer;
71
74
  function setNamer(that) {
75
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("mocha"))
76
+ return;
72
77
  namer = (0, MochaNamer_1.getMochaNamer)(that.currentTest);
73
78
  }
74
79
  function getNamer() {
75
80
  return namer;
76
81
  }
77
82
  function verify(sut, options) {
83
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("mocha"))
84
+ return;
78
85
  options = options || new Options_1.Options();
79
86
  options = options.withNamer(getNamer());
80
87
  const config = options.getConfig(approvals.getConfig());
@@ -12,7 +12,7 @@ import { printArray, printJson } from "../../Utilities/Printers";
12
12
  */
13
13
  import { Options } from "../../Core/Options";
14
14
  import { getMochaNamer } from "./MochaNamer";
15
- import mocha from "mocha";
15
+ import { isFrameworkAvailable } from "../OptionalFramework";
16
16
 
17
17
  import StringWriter from "../../StringWriter";
18
18
 
@@ -22,6 +22,8 @@ import { Namer } from "../../Core/Namer";
22
22
  let mochaTest: any = null;
23
23
 
24
24
  export function it2(label: string, test: () => void): void {
25
+ if (!isFrameworkAvailable("mocha")) return;
26
+ const mocha = require("mocha") as typeof import("mocha");
25
27
  mocha.it(label, function (this: any) {
26
28
  mochaTest = this;
27
29
  console.log("Mocha Test: ", mochaTest.test.name);
@@ -32,6 +34,7 @@ export function it2(label: string, test: () => void): void {
32
34
  let namer: Namer;
33
35
 
34
36
  export function setNamer(that: any): void {
37
+ if (!isFrameworkAvailable("mocha")) return;
35
38
  namer = getMochaNamer(that.currentTest);
36
39
  }
37
40
 
@@ -40,6 +43,7 @@ export function getNamer(): Namer {
40
43
  }
41
44
 
42
45
  export function verify(sut: any, options?: Options): void {
46
+ if (!isFrameworkAvailable("mocha")) return;
43
47
  options = options || new Options();
44
48
  options = options.withNamer(getNamer());
45
49
  const config = options.getConfig(approvals.getConfig());
@@ -1,5 +1,5 @@
1
1
  import { Namer } from "../../Namer";
2
- import { Runnable } from "mocha";
2
+ import type { Runnable } from "mocha";
3
3
  export declare class MochaNamer extends Namer {
4
4
  private ctx;
5
5
  constructor(mochaTest: Mocha.Context, overrideBasePath?: string);
@@ -1,6 +1,6 @@
1
1
  import path from "path";
2
2
  import { Namer } from "../../Namer";
3
- import { Runnable, Suite } from "mocha";
3
+ import type { Runnable, Suite } from "mocha";
4
4
  import { convertToFilename } from "../../Core/Namer";
5
5
 
6
6
  export class MochaNamer extends Namer {
@@ -0,0 +1 @@
1
+ export declare function isFrameworkAvailable(framework: string): boolean;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isFrameworkAvailable = isFrameworkAvailable;
4
+ function isFrameworkAvailable(framework) {
5
+ try {
6
+ require.resolve(framework);
7
+ return true;
8
+ }
9
+ catch (error) {
10
+ if (error.code !== "MODULE_NOT_FOUND") {
11
+ throw error;
12
+ }
13
+ return false;
14
+ }
15
+ }
@@ -0,0 +1,11 @@
1
+ export function isFrameworkAvailable(framework: string): boolean {
2
+ try {
3
+ require.resolve(framework);
4
+ return true;
5
+ } catch (error) {
6
+ if ((error as NodeJS.ErrnoException).code !== "MODULE_NOT_FOUND") {
7
+ throw error;
8
+ }
9
+ return false;
10
+ }
11
+ }
@@ -41,7 +41,10 @@ const Options_1 = require("../../Core/Options");
41
41
  const VitestNamer_1 = require("./VitestNamer");
42
42
  const StringWriter_1 = require("../../StringWriter");
43
43
  const approvals = __importStar(require("../../Approvals"));
44
+ const OptionalFramework_1 = require("../OptionalFramework");
44
45
  function verify(sut, options) {
46
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("vitest"))
47
+ return;
45
48
  options = options || new Options_1.Options();
46
49
  options = options.withNamer((0, VitestNamer_1.getVitestNamer)());
47
50
  const config = options.getConfig(approvals.getConfig());
@@ -4,8 +4,10 @@ import { getVitestNamer } from "./VitestNamer";
4
4
 
5
5
  import { StringWriter } from "../../StringWriter";
6
6
  import * as approvals from "../../Approvals";
7
+ import { isFrameworkAvailable } from "../OptionalFramework";
7
8
 
8
9
  export function verify(sut: any, options?: Options): void {
10
+ if (!isFrameworkAvailable("vitest")) return;
9
11
  options = options || new Options();
10
12
  options = options.withNamer(getVitestNamer());
11
13
  const config = options.getConfig(approvals.getConfig());
@@ -5,14 +5,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.VitestReporter = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
+ const OptionalFramework_1 = require("../OptionalFramework");
8
9
  class VitestReporter {
9
10
  constructor() {
10
11
  this.name = "VitestReporter";
11
12
  }
12
13
  canReportOn(_fileName) {
13
- return true;
14
+ return (0, OptionalFramework_1.isFrameworkAvailable)("vitest");
14
15
  }
15
16
  report(approvedFilePath, receivedFilePath, _) {
17
+ if (!(0, OptionalFramework_1.isFrameworkAvailable)("vitest"))
18
+ return;
16
19
  const approvedText = fs_1.default.readFileSync(approvedFilePath).toString();
17
20
  const receivedText = fs_1.default.readFileSync(receivedFilePath).toString();
18
21
  expect(receivedText).toBe(approvedText);
@@ -1,6 +1,7 @@
1
1
  import fs from "fs";
2
2
  import { Config } from "../../config";
3
3
  import { Reporter } from "../../Core/Reporter";
4
+ import { isFrameworkAvailable } from "../OptionalFramework";
4
5
 
5
6
  declare const expect: (actual: unknown) => { toBe(expected: unknown): void };
6
7
 
@@ -8,7 +9,7 @@ export class VitestReporter implements Reporter {
8
9
  name: string = "VitestReporter";
9
10
 
10
11
  canReportOn(_fileName: string) {
11
- return true;
12
+ return isFrameworkAvailable("vitest");
12
13
  }
13
14
 
14
15
  report(
@@ -16,6 +17,7 @@ export class VitestReporter implements Reporter {
16
17
  receivedFilePath: string,
17
18
  _: Partial<Config>,
18
19
  ) {
20
+ if (!isFrameworkAvailable("vitest")) return;
19
21
  const approvedText = fs.readFileSync(approvedFilePath).toString();
20
22
  const receivedText = fs.readFileSync(receivedFilePath).toString();
21
23
  expect(receivedText).toBe(approvedText);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "description": "Approval Tests Library - Capturing Human Intelligence",
7
- "version": "7.5.9",
7
+ "version": "7.5.10",
8
8
  "homepage": "http://approvaltests.com",
9
9
  "author": {
10
10
  "name": "Jason Jarrett",
@@ -39,12 +39,13 @@
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsc",
42
- "test": "npm run lint && npm run mocha && jest && vitest run",
43
- "vitest": "vitest run",
42
+ "test": "node scripts/test.cjs",
43
+ "vitest": "node scripts/test-isolated.cjs vitest",
44
44
  "test2": "npm run mocha && npm run lint",
45
45
  "lint": "eslint lib test",
46
- "mocha": "mocha",
47
- "coverage": "nyc npm run mocha"
46
+ "mocha": "node scripts/test-isolated.cjs mocha",
47
+ "coverage": "node scripts/test-isolated.cjs mocha --coverage",
48
+ "jest": "node scripts/test-isolated.cjs jest"
48
49
  },
49
50
  "dependencies": {
50
51
  "ansi-colors": "^4.1.3",
@@ -54,7 +55,6 @@
54
55
  "event-stream": "^4.0.1",
55
56
  "file-type": "^12.4.2",
56
57
  "glob": "^13.0.0",
57
- "jest": "^30.0.0",
58
58
  "js-yaml": "^5.1.0",
59
59
  "lodash": "^4.17.21",
60
60
  "marked": "^18.0.14",
@@ -62,8 +62,7 @@
62
62
  "minimist": "^1.2.8",
63
63
  "mkdirp": "^3.0.1",
64
64
  "shelljs": "^0.10.0",
65
- "temp": "^0.9.4",
66
- "ts-jest": "^29.0.3"
65
+ "temp": "^0.9.4"
67
66
  },
68
67
  "devDependencies": {
69
68
  "@types/chai": "^5.0.1",
@@ -74,17 +73,13 @@
74
73
  "chai": "^6.0.1",
75
74
  "eslint": "^10.2.1",
76
75
  "eslint-plugin-promise": "^7.2.1",
77
- "jest-junit": "^17.0.0",
78
76
  "jsdoc-to-markdown": "^9.1.1",
79
- "mocha": "^12.0.2",
80
- "mocha-multi-reporters": "^1.5.1",
81
77
  "nyc": "^18.0.0",
82
78
  "pre-commit": "^2.0.0",
83
79
  "prettier": "^3.4.2",
84
80
  "sinon": "^22.0.0",
85
81
  "ts-node": "^10.9.1",
86
- "typescript": "^6.0.2",
87
- "vitest": "^5.0.1"
82
+ "typescript": "^6.0.2"
88
83
  },
89
84
  "keywords": [
90
85
  "Approve",
@@ -96,5 +91,26 @@
96
91
  "Testing",
97
92
  "Verification",
98
93
  "Verify"
94
+ ],
95
+ "peerDependencies": {
96
+ "jest": "^30.0.0",
97
+ "mocha": "^12.0.2",
98
+ "vitest": "^5.0.1"
99
+ },
100
+ "peerDependenciesMeta": {
101
+ "jest": {
102
+ "optional": true
103
+ },
104
+ "mocha": {
105
+ "optional": true
106
+ },
107
+ "vitest": {
108
+ "optional": true
109
+ }
110
+ },
111
+ "workspaces": [
112
+ "test/mocha",
113
+ "test/jest",
114
+ "test/vitest"
99
115
  ]
100
116
  }
package/readme.md CHANGED
@@ -69,6 +69,10 @@ What is an approval test? Check out a brief [overview here](http://staxmanade.co
69
69
 
70
70
  ## Getting Started
71
71
 
72
+ Mocha, Jest, and Vitest are optional peer dependencies. Install the framework
73
+ your tests use alongside Approvals. Approvals silently skips integrations for
74
+ frameworks that are not installed.
75
+
72
76
  ### Jest Typescript Starter Project
73
77
 
74
78
  If you want to jump start a new project, clone the [Jest Starter Project](https://github.com/approvals/ApprovalTests.TypeScript.Jest.StarterProject)
@@ -90,10 +94,10 @@ Below is a simple getting started using Mocha.
90
94
  npm install --save-dev @approval-tests/approvals
91
95
  ```
92
96
 
93
- 1. Install [Mocha](http://mochajs.org/) globally to execute our tests
97
+ 1. Install [Mocha](http://mochajs.org/) in the project to execute our tests
94
98
 
95
99
  ```shell
96
- npm install -g mocha
100
+ npm install --save-dev mocha
97
101
  ```
98
102
 
99
103
  1. Create a sample Mocha test file called `SimpleLogger.test.js`.
@@ -112,7 +116,7 @@ Below is a simple getting started using Mocha.
112
116
  1. Test the file with mocha.
113
117
 
114
118
  ```shell
115
- mocha SimpleLogger.test.js
119
+ npx mocha SimpleLogger.test.js
116
120
  ```
117
121
 
118
122
  1. You should be presented with a diff tool. (if not, you may need to install one?)
@@ -507,7 +511,7 @@ approvals.verifyAndScrub(__dirname, 'sample-approval-test', { a: "some text in a
507
511
  ### approvals~verifyWithControl(namer, writer, [reporterFactory], [optionsOverride])
508
512
  This allows you to take full control of naming and writing files before verifying.
509
513
 
510
- For an example that we use to generate the docs within the readme, check out the [test/readmeTests.mts](test/readmeTests.mts) in this project.
514
+ For an example that we use to generate the docs within the readme, check out the [test/mocha/readmeTests.mts](test/mocha/readmeTests.mts) in this project.
511
515
 
512
516
  **Kind**: inner method of [<code>approvals</code>](#module_approvals)
513
517