Skip to content

Commit

Permalink
- r converted mocha namer to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
lexler committed Mar 15, 2024
1 parent 58d7d9f commit 823f2e0
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 64 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
lib/Namer.js
lib/Core/*.js
lib/Providers/Jest/*.js
lib/Providers/Mocha/*.js
lib/Scrubbers/Scrubbers.js
lib/Scrubbers/DateScrubber.js
lib/Utilities/*.js
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ lib/Namer.js
lib/Scrubbers/Scrubbers.js
lib/Scrubbers/DateScrubber.js
/yarn.lock
/lib/Providers/Mocha/**/**.js
3 changes: 2 additions & 1 deletion lib/Approvals.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ var mochaExport = function (optionalBaseDir) {
optionalBaseDir = path.dirname(callsite()[1].getFileName());
}

var Namer = require("./Providers/Mocha/MochaNamer");
var { MochaNamer } = require("./Providers/Mocha/MochaNamer");
var Namer = MochaNamer
var baseVerifier = require("./Providers/BeforeEachVerifierBase.js");

baseVerifier(Namer, "require('Approvals').mocha();", optionalBaseDir);
Expand Down
9 changes: 4 additions & 5 deletions lib/Core/Options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {Scrubber} from "../Scrubbers/Scrubbers";
import type {Namer} from "../Namer";
import {getJestNamer} from "../Providers/Jest/JestNamer";
const Namer: any = require("../Namer");

export type ConfigModifier = (t: any) => any;

Expand Down Expand Up @@ -71,11 +70,11 @@ export class Options {
return modifier(config);
}

withNamer(namer: Namer): Options {
withNamer(namer: typeof Namer): Options {
return this.modify("Namer", namer);
}

getNamer(): Namer {
return this.get("Namer", () => getJestNamer());
getNamer(): typeof Namer {
return this.get("Namer", () => new Namer('',''));
}
}
4 changes: 2 additions & 2 deletions lib/Namer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import path from 'path';

export class Namer {

private path: string;
private name: string;
protected path: string;
protected name: string;

constructor(path: string, name: string) {
path = path || '';
Expand Down
2 changes: 2 additions & 0 deletions lib/Providers/Jest/JestApprovals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {printArray, printJson} from "../../Utilities/Printers";
```
*/
import {Options} from "../../Core/Options";
import {getJestNamer} from "./JestNamer";

const StringWriter = require("../../StringWriter");
const approvals = require("../../Approvals");


export function verify(sut: any, options?: Options): void {
options = options || new Options()
options = options.withNamer(getJestNamer());
const config = options.getConfig(approvals.getConfig());
const scrubbed = options.scrub(`${sut}`);
const writer = new StringWriter(config, scrubbed, options.forFile().getFileExtension());
Expand Down
49 changes: 49 additions & 0 deletions lib/Providers/Mocha/MochaApprovals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {printArray, printJson} from "../../Utilities/Printers";
/* ^^^ If the above line is failing, please add
# 1. Jest Config
```
"transformIgnorePatterns": ["node_modules/@3ds"]
```
# 2. tsconfig.json
```
"esModuleInterop": true
```
*/
import {Options} from "../../Core/Options";
import {MochaNamer} from "./MochaNamer";
import mocha from 'mocha';

const StringWriter = require("../../StringWriter");
const approvals = require("../../Approvals");
let mochaTest: any = null;

export function it2(label: string, test: () => void): void {
mocha.it(label, function () {
mochaTest = this;
console.log("Mocha Test: ", mochaTest.test.name);
test();
});
}

export function verify(sut: any, options?: Options): void {
options = options || new Options()
options = options.withNamer(new MochaNamer(mochaTest.test));
const config = options.getConfig(approvals.getConfig());
const scrubbed = options.scrub(`${sut}`);
const writer = new StringWriter(config, scrubbed, options.forFile().getFileExtension());
let namer = options.getNamer();
approvals.verifyWithControl(namer, writer, null, config);
}

export function verifyAsJson(data: any, options?: Options): void {
const text = printJson(data);
options = options || new Options()
options = options.forFile().withFileExtention(".json")
verify(text, options);
}

export function verifyAll<T>(header:string, list: T[], formatter?: ((element: T) => string), options?: Options): void{
const text = printArray(header, list, formatter);
verify(text, options);
}
55 changes: 0 additions & 55 deletions lib/Providers/Mocha/MochaNamer.js

This file was deleted.

60 changes: 60 additions & 0 deletions lib/Providers/Mocha/MochaNamer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import path from "path";

const Namer: any = require("../../Namer");

export interface MochaTest {
file: string;
parent?: MochaTest;
title: string;
}

export class MochaNamer extends Namer {
private ctx: MochaTest;

constructor(mochaTest: any, overrideBasePath?: string) {
if (!mochaTest) {
throw new Error("Mocha test context was not supplied");
}
mochaTest = mochaTest.test || mochaTest;

super('', '');

this.ctx = mochaTest;
this.path = overrideBasePath || path.dirname(mochaTest.file);
}

getFullTestName(testContext: MochaTest): string {
let test = testContext;

let parentStack: MochaTest[] = [];
let currParent: MochaTest | undefined = test;
while (currParent && currParent.parent) {
parentStack.push(currParent);
currParent = currParent.parent;
}

let newTitle = '';
let parentStackReversed = parentStack.reverse();
parentStackReversed.forEach((item, index) => {
if (index !== 0) {
newTitle += ".";
}

newTitle += item.title.split(' ').join('_').replace(/[^\w\s]/gi, '_');
});

return newTitle;
}

pathCreator(type: string, ext: string): string {
if (!this.name) {
if (!this.ctx) {
throw new Error("ctx was not defined.");
}
this.name = this.getFullTestName(this.ctx);
}

return super.pathCreator(type, ext);
}
}

2 changes: 1 addition & 1 deletion test/Providers/Mocha/MochaNamerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var assert = require("assert");
var path = require('path');
var MochaNamer = require("../../../lib/Providers/Mocha/MochaNamer.js");
var { MochaNamer } = require("../../../lib/Providers/Mocha/MochaNamer");

describe('MochaNamer', function () {

Expand Down
8 changes: 8 additions & 0 deletions test/Providers/Mocha/Typescript/Mocha.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
//require('approvals').mocha();

import {it2, verify} from "../../../../lib/Providers/Mocha/MochaApprovals";

const assert = require('assert');
// import { describe, it } from 'mocha';

describe("Mocha Typescript Approvals", () => {

it("verify", () => {
// verify("Hello From Approvals");
assert.equal(2, 2);
});

// it2("verify approvals", () => {
// verify("Hello From Approvals");
// // get the test name of the current mocha test
// });
});


0 comments on commit 823f2e0

Please sign in to comment.