From 0ad30c7ef5df3634ad87fcab29a0418fd816034e Mon Sep 17 00:00:00 2001 From: AleksandrHorev Date: Sat, 19 Dec 2020 10:44:51 +0300 Subject: [PATCH] Moved temp folder form node_modules to the root of project (#86) * 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 Co-authored-by: Harry.Hou --- helper.js | 12 ++++++++++-- index.js | 4 ++-- readme.md | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/helper.js b/helper.js index 0dfa73b..d66f13d 100644 --- a/helper.js +++ b/helper.js @@ -1,8 +1,12 @@ 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`; @@ -10,6 +14,7 @@ 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); @@ -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 }; @@ -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); @@ -185,4 +192,5 @@ module.exports = { dataDirPath, attachDirPath, distDirName, + tempDirPath, }; diff --git a/index.js b/index.js index a39b819..b126af8 100644 --- a/index.js +++ b/index.js @@ -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, @@ -119,7 +119,7 @@ class MyCustomReporter { } removeTempDir() { - fse.removeSync(path.resolve(__dirname, "./temp")); + fse.removeSync(tempDirPath); } removeAttachDir() { diff --git a/readme.md b/readme.md index 9ed251d..7a9bad0 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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). @@ -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); }); @@ -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).