Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #360

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open

Next #360

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f0143db
7.0.0-alpha.0
Aug 22, 2021
53f07ef
Rename source files with ts extension
Aug 22, 2021
f01e783
pkg: update mocha peer/dev dep
Aug 24, 2021
e4b8303
pkg: use full lodash library
Aug 24, 2021
da317bb
pkg: add typescript deps
Aug 24, 2021
0768e93
rename mochawesome -> index
Aug 24, 2021
23c2856
convert require to import
Aug 24, 2021
0501f26
convert to class (no changes)
Aug 24, 2021
e3f2c09
Add mochawesome declaration file
Aug 24, 2021
6e7fd84
break out class methods
Aug 24, 2021
d7f15d5
Refactor loading of console reporter
Aug 24, 2021
97ab4d4
Refactor done method
Aug 24, 2021
c99c46a
Move 'end' handler to class method
Aug 24, 2021
a4b2be7
Remove handler to assign uuids
Aug 24, 2021
ed6fe3c
Add meta class property
Aug 24, 2021
15abf00
Refactor handleEndEvent
Aug 24, 2021
d48d7ba
Extend from Base reporter class
Aug 24, 2021
932d62a
Add typings for parallel event listeners
Aug 24, 2021
dd22231
Refactor and type config function
Aug 24, 2021
d003917
pkg: add types for mocha and node
Aug 24, 2021
803c9e5
Use default export
Aug 24, 2021
cd9ca72
Refactor stripFnStart helper
Aug 24, 2021
109b101
Add lodash types and update imports
Aug 24, 2021
2f756ea
Add types for diff/json-stringify
Sep 2, 2021
61bb7e1
Define module for mocha utils
Sep 2, 2021
ef04451
Define RunProcessor class
Sep 2, 2021
dd1ced5
Define Logger class
Sep 2, 2021
6358d66
Remove unused utils file
Sep 2, 2021
fb0447c
fixup processor
Sep 2, 2021
dd70b50
Implement new Logger class
Sep 2, 2021
c63bf62
Implement RunProcessor
Sep 2, 2021
5492da5
Add type defs for Mochawesome
Sep 2, 2021
b0c2081
Move register back to JS
Sep 2, 2021
984d925
fixup logger
Sep 2, 2021
d967760
addContext: replace logger
Sep 2, 2021
73fa765
Move Mocha types into module definition
Sep 2, 2021
5e9880d
Refactor addContext
Sep 2, 2021
e7e71e2
Type errors
Oct 20, 2021
94c86a9
Use result objects instead of arrays
Oct 20, 2021
5c2c5f0
Add tsconfig
Oct 20, 2021
e044068
Update functional tests
Oct 20, 2021
afd2c7d
Add dev script
Oct 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
501 changes: 405 additions & 96 deletions package-lock.json

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "mochawesome",
"version": "6.2.2",
"version": "7.0.0-alpha.0",
"description": "A gorgeous reporter for Mocha.js",
"scripts": {
"lint": "eslint src test*",
"dev": "mocha test-functional/simple --config test-functional/.mocharc.json",
"test": "npm run lint && cross-env NODE_ENV=test nyc mocha --config test/.mocharc.json",
"test:fn": "mocha test-functional --config test-functional/.mocharc.json",
"test:par": "mocha test-parallel --parallel --config test-parallel/.mocharc.json",
Expand Down Expand Up @@ -48,32 +49,37 @@
"branches": 90
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"chalk": "^4.1.0",
"diff": "^5.0.0",
"json-stringify-safe": "^5.0.1",
"lodash.isempty": "^4.4.0",
"lodash.isfunction": "^3.0.9",
"lodash.isobject": "^3.0.2",
"lodash.isstring": "^4.0.1",
"lodash": "^4.17.21",
"mochawesome-report-generator": "^5.2.0",
"strip-ansi": "^6.0.0",
"uuid": "^8.3.2"
},
"peerDependencies": {
"mocha": ">=7"
"mocha": ">=9"
},
"devDependencies": {
"@types/diff": "^5.0.1",
"@types/json-stringify-safe": "^5.0.0",
"@types/lodash": "^4.14.172",
"@types/mocha": "^9.0.0",
"@types/node": "^16.7.1",
"cross-env": "^7.0.3",
"eslint": "^7.20.0",
"eslint-config-prettier": "^7.2.0",
"husky": "^5.0.9",
"lint-staged": "^10.5.4",
"mocha": "^8.3.0",
"mocha": "^9.1.0",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"proxyquire": "^2.1.0",
"should": "^13.2.3",
"sinon": "^9.2.4"
"sinon": "^9.2.4",
"typescript": "^4.3.5"
},
"lint-staged": {
"*.js": "eslint --cache --fix",
Expand Down
135 changes: 0 additions & 135 deletions src/addContext.js

This file was deleted.

117 changes: 117 additions & 0 deletions src/addContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import isObject from 'lodash/isobject';
import jsonStringify from 'json-stringify-safe';
import Logger from './logger';

const logger = new Logger(console);
const errorPrefix = 'Error adding context:';
const ERRORS = {
INVALID_ARGS: `${errorPrefix} Invalid arguments.`,
INVALID_TEST: `${errorPrefix} Invalid test object.`,
INVALID_CONTEXT: (ctx: Mochawesome.ContextArg) => {
const expected =
'Expected a string or an object of shape { title: string, value: any } but saw:';
return `${errorPrefix} ${expected}\n${jsonStringify(
ctx,
(key, val) => (val === undefined ? 'undefined' : val),
2
)}`;
},
};

function _normalizeContext(
context: Mochawesome.ContextArg
): Mochawesome.Context | undefined {
// Ensure that context meets the requirements
if (
(typeof context === 'string' && !context.trim()) ||
(typeof context === 'object' && context.title === undefined)
) {
logger.error(ERRORS.INVALID_CONTEXT(context));
return;
}

if (typeof context === 'string') {
return {
title: '',
value: context,
};
}

if (context.value === undefined) {
return {
...context,
value: 'undefined',
};
}

return context;
}

/**
* Add context to the test object so it can
* be displayed in the mochawesome report
*
* Usage:
*
* it('should test something', function () {
* someFunctionThatTestsCode();
*
* addContext(this, 'some context to add');
*
* addContext(this, {
* title: 'Expected number of something'
* value: 42
* });
*
* assert('something');
* });
*
*/
const addContext = function (
runnable: MochaRunnable,
_context: Mochawesome.ContextArg
) {
// Check args to see if we should bother continuing
if (!runnable || !_context || !isObject(runnable)) {
logger.error(ERRORS.INVALID_ARGS);
return;
}

const context = _normalizeContext(_context);
if (!context) {
return;
}

/*
* When `addContext` is called from inside a hook the test object
* will be `.currentTest`, and the hook will be `.test`.
* Otherwise the test is just `.test` and `.currentTest` is undefined.
*/
const { currentTest, test: activeTest } = runnable;

/*
* For `beforeAll` and `afterAll`, add the context to the hook,
* otherwise add it to the actual test.
*/
const isEachHook =
currentTest &&
activeTest &&
/^"(?:before|after)\seach"/.test(activeTest.title);

const theTest = isEachHook ? currentTest : activeTest;

if (!theTest) {
logger.error(ERRORS.INVALID_TEST);
return;
}

// Test doesn't already have context -> set it
if (!theTest.context) {
theTest.context = [context];
} else if (Array.isArray(theTest.context)) {
// Test has context and context is an array -> push new context
theTest.context.push(context);
}
};

export = addContext;
48 changes: 0 additions & 48 deletions src/config.js

This file was deleted.

Loading