Sid
Sid

Reputation: 645

Need to get file path using jasmine

i have a requirement where i need to get file names.

I have seen jasmine.getEnv().currentSpec.description which returns spec.js description. Similarly i need to get get spec.js full path. Is it possible to get file path?

Upvotes: 3

Views: 1526

Answers (1)

gnerkus
gnerkus

Reputation: 12037

jasmine does not provide a reference to the file a spec is being run from. The env (from getEnv()) object only provides information on the specs and suites but not the files.

You can obtain a reference to the file's full path by utilizing the __filename global within the file itself. For example:

const jasmine = require('jasmine-node');
console.log(__filename);

describe('description for spec', () => {
  it('should do stuff', () => {
    console.log(jasmine.getEnv());
  });
});

Below's a summary of the env object:

{ currentSpec:
   { id: 0,
     env: [Circular],
     suite:
      { id: 0,
        description: 'description for spec',
        queue: [Object],
        parentSuite: null,
        env: [Circular],
        before_: [],
        after_: [],
        children_: [Object],
        suites_: [],
        specs_: [Object],
        exclusive_: 0 },
     description: 'should do stuff',
     queue:
      { env: [Circular],
        ensured: [Object],
        blocks: [Object],
        running: true,
        index: 0,
        offset: 0,
        abort: false,
        onComplete: [Function] },
     afterCallbacks: [],
     spies_: [],
     results_:
      { totalCount: 0,
        passedCount: 0,
        failedCount: 0,
        skipped: false,
        items_: [],
        description: 'should do stuff' },
     matchersClass: null,
     exclusive_: 0 },
  currentSuite: null,
  currentRunner_:
   { env: [Circular],
     queue:
      { env: [Circular],
        ensured: [Object],
        blocks: [Object],
        running: true,
        index: 0,
        offset: 0,
        abort: false,
        onComplete: [Function] },
     before_: [],
     after_: [],
     suites_: [ [Object] ] },
  reporter: { subReporters_: [ [Object] ] },
  updateInterval: 250,
  defaultTimeoutInterval: 5000,
  lastUpdate: 0,
  specFilter: [Function],
  nextSpecId_: 1,
  nextSuiteId_: 1,
  equalityTesters_: [],
  exclusive_: 0,
  matchersClass: [Function] }

Upvotes: 4

Related Questions