Skip to content

Commit

Permalink
Moved temp folder form node_modules to the root of project (#86)
Browse files Browse the repository at this point in the history
* update logic for temp folder

* added random name to folder

* added mkdir

* moved fs.mkdir to top of the file

* wrap mkdir to async functions

* fix await

* mkdir -> mkdirs

* update name

* path to temp folder

* revert back generateRandomString

* moved creation of folders to helper

* moved removing of folders to helper

* updated readme. minor fixes

* added empty line

* rename path -> tempDirPath

* moved temp dir path to config

* added comments

* revert context param.
fix tempDirPath in index file

* removed difficult logic what set tempDirPath

* fix typo

* fix comments

* to support env variable

* delete unuse code

Co-authored-by: Aleksandr Khorev <[email protected]>
Co-authored-by: Harry.Hou <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2020
1 parent 501156a commit 0ad30c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 10 additions & 2 deletions helper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const fs = require("fs-extra");
const path = require("path");

const dataDirPath = path.resolve(__dirname, "./temp/data");
const attachDirPath = path.resolve(__dirname, "./temp/images");
const tempDirPath = path.resolve(
process.env.JEST_HTML_REPORTERS_TEMP_DIR_PATH || __dirname,
"temp"
);
const dataDirPath = path.resolve(tempDirPath, "./data");
const attachDirPath = path.resolve(tempDirPath, "./images");

const distDirName = `./jest-html-reporters-attach`;

/**
*
* @param {Buffer | string} attach
* @param {string} description
* @param {object} context. Optional. It contains custom configs
*/
const addAttach = async (attach, description, context) => {
const { testPath, testName } = getJestGlobalData(context);
Expand All @@ -20,6 +25,7 @@ const addAttach = async (attach, description, context) => {
);
return;
}

const fileName = generateRandomString();
if (typeof attach === "string") {
const attachObject = { testPath, testName, filePath: attach, description };
Expand Down Expand Up @@ -49,6 +55,7 @@ const addAttach = async (attach, description, context) => {
/**
*
* @param {string} message
* @param {object} context. Optional. It contains custom configs
*/
const addMsg = async (message, context) => {
const { testPath, testName } = getJestGlobalData(context);
Expand Down Expand Up @@ -185,4 +192,5 @@ module.exports = {
dataDirPath,
attachDirPath,
distDirName,
tempDirPath,
};
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { promises } = require("dns");
const fs = require("fs");
const fse = require("fs-extra");
const path = require("path");
const {
tempDirPath,
dataDirPath,
attachDirPath,
distDirName,
Expand Down Expand Up @@ -119,7 +119,7 @@ class MyCustomReporter {
}

removeTempDir() {
fse.removeSync(path.resolve(__dirname, "./temp"));
fse.removeSync(tempDirPath);
}

removeAttachDir() {
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The options below are specific to the reporter.
| `customInfos` | JEST_HTML_REPORTERS_CUSTOM_INFOS | array | undefined | show some custom data info in the report, example value `[ {title: 'test1', value: 'test1'}, {title: 'test2', value: 'test2'}]`, you can also set value to a environment variable **JEST_HTML_REPORTERS_CUSTOM_INFOS**, see detail in [#32](https://github.com/Hazyzh/jest-html-reporters/issues/32) |
| `testCommand` | JEST_HTML_REPORTERS_TEST_COMMAND | string | "npx jest" | copy command content to quickly run test file |
| `multipleReportsUnitePath` | JEST_HTML_REPORTERS_MULTIPLE_REPORTS_UNITE_PATH | string | "" | the unite folder path for single page(show multiple test result). see detail in [Single Page for multiple reports](https://github.com/Hazyzh/jest-html-reporters/wiki/Single-Page-for-multiple-reports) |

|`env variable support only` | JEST_HTML_REPORTERS_TEMP_DIR_PATH | string | __dirname | path to a temporary folder with attachments title |
---

#### example add config options
Expand Down Expand Up @@ -94,7 +94,7 @@ const addAttach = async (attach, description, context) => { ... }
```

There are three params of this method, `description` is easy to understand. The param **`attach`** referring to the image, you can pass a `buffer` or `string`, if it was a buffer the package will help you create a dir named `jest-html-reporters-attach` and save that `buffer` as a `jpg` image in it under the `publicPath`. if you have already saved the image, just pass the image's path as the `attach` param.
`context` is optional parameter if you need add a specific context to attachment(like test path or name).
`context` is an optional parameter. Here can be specifeded **context** (default is this.global).

Here is an Example with [puppeteer](https://github.com/puppeteer/puppeteer).

Expand All @@ -110,7 +110,7 @@ describe("just examples", () => {
await page.goto("https://www.google.com");
const data = await page.screenshot();
await browser.close();
await addAttach(data, "test google 1");
await addAttach(data, "test google 1", this.global);

expect(1).toBe(1);
});
Expand Down Expand Up @@ -155,7 +155,7 @@ const addMsg = async (message, context) => { ... }
```

Only one parameter is required. If you stringify an object like this `JSON.stringify(object, null, 2)`, the object will be prettified.
`context` is optional parameter if you need add a specific context to message(like test path or name).
`context` is an optional parameter. Here can be specifeded **context** (default is this.global).

Here is an Example with [Nightmare](https://www.npmjs.com/package/nightmare).

Expand Down

0 comments on commit 0ad30c7

Please sign in to comment.