Skip to content

Commit

Permalink
addContext in before & after hooks
Browse files Browse the repository at this point in the history
Proposition for adamgruber#284
expected : 
- addContext in 'before' hook --> add the context in results.suites[0].beforeHooks[0]
- addContext in 'after' hook --> add the context in results.suites[0].afterHooks[0]
- addContext in 'afterEach' and 'beforeEach' hooks --> add the context to each test

But in addContext module, it's difficult to know if we are in a before/after or in a beaforeEach/afterEach, so the only way I found is to add a third parameter, which indicates if the context must be added in global (beforeHooks & afterHooks) instead of in the firs or last test

here is an example of use :
```
describe('Context', async function() {
  this.timeout(120000);
  var count = 0;
  
  it('Open Spoggy', async function() {
    step = this.test.title;
    await driver.get('https://scenaristeur.github.io/spoggy-simple/');
    await driver.wait(until.titleIs('Spoggy'), 10000);
    const title1 = await driver.getTitle();
    expect(title1).to.equal("Spoggy");
  });
  
  it('test 1', async function() {
    console.log("test 1")
  });
  
  it('test 2', async function() {
    console.log("test 2")
  });
  
  it('test 3', async function() {
    addContext(this, 'some context in 3');
    console.log("test 3")
  });
  
  it('test 4', async function() {
    console.log("test 4")
  });
  
  before('avant', function () {
    addContext(this, 'some context before',true);
  });
  beforeEach('avant chaque', async function () {
    addContext(this, 'some context before each');
  });
  afterEach('après chaque', async function () {
    addContext(this, 'some context after each');
  });
  
  after('après', async function() {
    addContext(this, 'some context after',true);
  });
});
```
that gives me : 
```
 Context
true ' currentTest : ' 'Open Spoggy' ' test : ' '"before all" hook: avant' '-> context in : ' '"before all" hook: avant'
false ' currentTest : ' 'Open Spoggy' ' test : ' '"before each" hook: avant chaque' '-> context in : ' 'Open Spoggy'

    ✓ Open Spoggy (7415ms)
false ' currentTest : ' 'Open Spoggy' ' test : ' '"after each" hook: après chaque' '-> context in : ' 'Open Spoggy'
false ' currentTest : ' 'test 1' ' test : ' '"before each" hook: avant chaque' '-> context in : ' 'test 1'
test 1
    ✓ test 1
false ' currentTest : ' 'test 1' ' test : ' '"after each" hook: après chaque' '-> context in : ' 'test 1'
false ' currentTest : ' 'test 2' ' test : ' '"before each" hook: avant chaque' '-> context in : ' 'test 2'
test 2
    ✓ test 2
false ' currentTest : ' 'test 2' ' test : ' '"after each" hook: après chaque' '-> context in : ' 'test 2'
false ' currentTest : ' 'test 3' ' test : ' '"before each" hook: avant chaque' '-> context in : ' 'test 3'
false ' currentTest : ' 'no currentTest var ' ' test : ' 'test 3' '-> context in : ' 'test 3'
test 3
    ✓ test 3
false ' currentTest : ' 'test 3' ' test : ' '"after each" hook: après chaque' '-> context in : ' 'test 3'
false ' currentTest : ' 'test 4' ' test : ' '"before each" hook: avant chaque' '-> context in : ' 'test 4'
test 4
    ✓ test 4
false ' currentTest : ' 'test 4' ' test : ' '"after each" hook: après chaque' '-> context in : ' 'test 4'
true ' currentTest : ' 'test 4' ' test : ' '"after all" hook: après' '-> context in : ' '"after all" hook: après'
```




```
  before('avant', function () {
    addContext(this, 'some context before',true);
  });
  beforeEach('avant chaque', async function () {
    addContext(this, 'some context before each');
  });
  afterEach('après chaque', async function () {
    addContext(this, 'some context after each');
  });
  
  after('après', async function() {
    addContext(this, 'some context after',true);
  });
```

and the json is as expected : 

```
{
  "stats": {
    "suites": 1,
    "tests": 5,
    "passes": 5,
    "pending": 0,
    "failures": 0,
    "start": "2019-06-13T14:46:03.397Z",
    "end": "2019-06-13T14:46:10.844Z",
    "duration": 7447,
    "testsRegistered": 5,
    "passPercent": 100,
    "pendingPercent": 0,
    "other": 0,
    "hasOther": false,
    "skipped": 0,
    "hasSkipped": false
  },
  "results": [
    {
      "uuid": "f20af57e-738d-473b-93e5-dd94b0145fa3",
      "title": "",
      "fullFile": "",
      "file": "",
      "beforeHooks": [],
      "afterHooks": [],
      "tests": [],
      "suites": [
        {
          "uuid": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
          "title": "Context",
          "fullFile": "/produits/wsimu5/simulateur/scenarios/testContext.js",
          "file": "/produits/wsimu5/simulateur/scenarios/testContext.js",
=>          "beforeHooks": [
            {
              "title": "\"before all\" hook: avant",
              "fullTitle": "Context \"before all\" hook: avant",
              "timedOut": false,
              "duration": 1,
              "state": null,
              "speed": null,
              "pass": false,
              "fail": false,
              "pending": false,
=>              "context": "\"some context before\"",
              "code": "addContext(this, 'some context before',true);\n//  count++;\n//  addContext(this, { title:'counter', value:\"before : \"+count });",
              "err": {},
              "uuid": "1ac94093-73c7-4520-806b-daa6c631b305",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": true,
              "skipped": false
            },
            {
              "title": "\"before each\" hook: avant chaque",
              "fullTitle": "Context \"before each\" hook: avant chaque",
              "timedOut": false,
              "duration": 1,
              "state": null,
              "speed": null,
              "pass": false,
              "fail": false,
              "pending": false,
              "context": null,
              "code": "addContext(this, 'some context before each');\n/*  addContext(this, {\ntitle: 'beforeEach context',\nvalue: { b: 2 }\n  });\n  count++;\n  addContext(this, { title:'counter', value:\"beforeEach : \"+count });*/",
              "err": {},
              "uuid": "4516ed45-4c85-47be-9ba4-826a6fe5f3d3",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": true,
              "skipped": false
            }
          ],
=>          "afterHooks": [
            {
              "title": "\"after all\" hook: après",
              "fullTitle": "Context \"after all\" hook: après",
              "timedOut": false,
              "duration": 0,
              "state": null,
              "speed": null,
              "pass": false,
              "fail": false,
              "pending": false,
=>              "context": "\"some context after\"",
              "code": "addContext(this, 'some context after',true);\n/*  count++;\naddContext(this, { title:'counter', value:\"after : \"+count });\n*/\n//  addContext(this, { title:'config', value:\"after\" });\ndriver.quit();",
              "err": {},
              "uuid": "9c8cc803-23ae-4fcd-b8c6-737d00ca276e",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": true,
              "skipped": false
            },
            {
              "title": "\"after each\" hook: après chaque",
              "fullTitle": "Context \"after each\" hook: après chaque",
              "timedOut": false,
              "duration": 0,
              "state": null,
              "speed": null,
              "pass": false,
              "fail": false,
              "pending": false,
              "context": null,
              "code": "addContext(this, 'some context after each');\n/*  addContext(this, {\ntitle: 'afterEach context',\nvalue: { a: 1 }\n});\ncount++;\naddContext(this, { title:'counter', value:\"afterEach : \"+count });*/",
              "err": {},
              "uuid": "230e0487-14c3-4c00-b18f-a470d0357195",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": true,
              "skipped": false
            }
          ],
          "tests": [
            {
              "title": "Open Spoggy",
              "fullTitle": "Context Open Spoggy",
              "timedOut": false,
              "duration": 7415,
              "state": "passed",
              "speed": "slow",
              "pass": true,
              "fail": false,
              "pending": false,
=>              "context": "[\n  \"some context before each\",\n  \"some context after each\"\n]",
              "code": "step = this.test.title;\nawait driver.get('https://scenaristeur.github.io/spoggy-simple/');\nawait driver.wait(until.titleIs('Spoggy'), 10000);\nconst title1 = await driver.getTitle();\nexpect(title1).to.equal(\"Spoggy\");",
              "err": {},
              "uuid": "3c7662d8-c055-420d-bf0f-b9f0fd132cf5",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": false,
              "skipped": false
            },
            {
              "title": "test 1",
              "fullTitle": "Context test 1",
              "timedOut": false,
              "duration": 1,
              "state": "passed",
              "speed": "fast",
              "pass": true,
              "fail": false,
              "pending": false,
=>              "context": "[\n  \"some context before each\",\n  \"some context after each\"\n]",
              "code": "console.log(\"test 1\")",
              "err": {},
              "uuid": "25c038d5-c939-4e5f-9083-2bd808d80a3a",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": false,
              "skipped": false
            },
            {
              "title": "test 2",
              "fullTitle": "Context test 2",
              "timedOut": false,
              "duration": 0,
              "state": "passed",
              "speed": "fast",
              "pass": true,
              "fail": false,
              "pending": false,
=>              "context": "[\n  \"some context before each\",\n  \"some context after each\"\n]",
              "code": "console.log(\"test 2\")",
              "err": {},
              "uuid": "c58ed9d0-2060-4f65-a348-41e0ba5011c5",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": false,
              "skipped": false
            },
            {
              "title": "test 3",
              "fullTitle": "Context test 3",
              "timedOut": false,
              "duration": 1,
              "state": "passed",
              "speed": "fast",
              "pass": true,
              "fail": false,
              "pending": false,
=>              "context": "[\n  \"some context before each\",\n  \"some context in 3\",\n  \"some context after each\"\n]",
              "code": "addContext(this, 'some context in 3');\n//  count++;\n//  addContext(this, { title:'counter', value:\"counter in 3 : \"+count });\nconsole.log(\"test 3\")",
              "err": {},
              "uuid": "c80968a0-04eb-4a3e-9e25-94c153df937a",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": false,
              "skipped": false
            },
            {
              "title": "test 4",
              "fullTitle": "Context test 4",
              "timedOut": false,
              "duration": 0,
              "state": "passed",
              "speed": "fast",
              "pass": true,
              "fail": false,
              "pending": false,
=>              "context": "[\n  \"some context before each\",\n  \"some context after each\"\n]",
              "code": "console.log(\"test 4\")",
              "err": {},
              "uuid": "543751ac-0700-4871-ac33-fa72217b2169",
              "parentUUID": "179c7517-f0b5-4520-ac2a-0acb7f8ed6f6",
              "isHook": false,
              "skipped": false
            }
          ],
          "suites": [],
          "passes": [
            "3c7662d8-c055-420d-bf0f-b9f0fd132cf5",
            "25c038d5-c939-4e5f-9083-2bd808d80a3a",
            "c58ed9d0-2060-4f65-a348-41e0ba5011c5",
            "c80968a0-04eb-4a3e-9e25-94c153df937a",
            "543751ac-0700-4871-ac33-fa72217b2169"
          ],
          "failures": [],
          "pending": [],
          "skipped": [],
          "duration": 7417,
          "root": false,
          "rootEmpty": false,
          "_timeout": 120000
        }
      ],
      "passes": [],
      "failures": [],
      "pending": [],
      "skipped": [],
      "duration": 0,
      "root": true,
      "rootEmpty": true,
      "_timeout": 2000
    }
  ],
  "meta": {
    "mocha": {
      "version": "6.1.4"
    },
    "mochawesome": {
      "options": {
        "quiet": false,
        "reportFilename": "mochawesome",
        "saveHtml": true,
        "saveJson": true,
        "useInlineDiffs": false
      },
      "version": "4.0.0"
    },
    "marge": {
      "options": {
        "reportDir": "/var/lib/jenkins/workspace/testContext/mochawesome-report",
        "reportFilename": "mochawesome"
      },
      "version": "4.0.0"
    }
  }
}


```
  • Loading branch information
scenaristeur authored Jun 13, 2019
1 parent 91d80c0 commit 2fdf101
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/addContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ function _isValidContext(ctx) {

const addContext = function (...args) {
// Check args to see if we should bother continuing
if ((args.length !== 2) || !isObject(args[0])) {
/*if ((args.length !== 2) || !isObject(args[0])) {
log(ERRORS.INVALID_ARGS, 'error');
return;
}
}*/
const global = args[2] || false;

const ctx = args[1];

Expand All @@ -86,7 +87,16 @@ const addContext = function (...args) {
* If `addContext` is called from inside a `beforeEach` or `afterEach`
* the test object will be `.currentTest`, otherwise just `.test`
*/
const test = args[0].currentTest || args[0].test;

// args[0].currentTest != undefined? currentTitle = args[0].currentTest.title : currentTitle = "no currentTest var ";;
//args[0].test !=undefined? testTitle = args[0].test.title : testTitle = "no test var "
if (global == true){
test = args[0].test;
}else{
test = args[0].currentTest || args[0].test ;
}
//console.log("global ",global, " currentTest : ",currentTitle , " test : ", testTitle , "-> context in : ", test.title)


if (!test) {
log(ERRORS.INVALID_TEST, 'error');
Expand Down

0 comments on commit 2fdf101

Please sign in to comment.