hbenl / vscode-jasmine-test-adapter

Jasmine Test Adapter for the VS Code Test Explorer
MIT License
20 stars 20 forks source link

Ability to set the current working directory when jasmine is executed #23

Closed justfalter closed 6 years ago

justfalter commented 6 years ago

It would be nice if there were a way to configure the Jasmine Test Explorer to set it's current working directory to something other than the project workspace root directory. perhaps jasmineExplorer.cwd?

I've got a project that has distinct frontend and backend components, both having entirely separate unit tests:

proj/
  (other stuff common to project)
  frontend/bla/bla1.js
  frontend/bla/bla1.spec.js
  frontend/spec/support/jasmine.json
  backend/foo/foo1.js
  backend/foo/foo1.spec.js
  backend/foo/spec/support/jasmine.json

I want to be able to configure Jasmine Test Explorer to only discover specs of contents of proj/backend without having to open up a VS Code workspace that is exclusive to proj/backend. There are simply too many things convenient to having a single folder workspace in my situation.

Things are set up such that you are expected to execute jasmine from the proj/backend or proj/frontend directories, but never from the root, proj/. So, if you want to run backend tests, you'd change your current working directory to proj/backend, and then execute npx jasmine. If you try to do this from the root of project, things will fail.

I have the following in my settings.json:

{
    "jasmineExplorer.config": "backend/spec/support/jasmine.json"
}

backend/spec/support/jasmine.json:

{
  "spec_dir": ".",
  "spec_files": [
    "**/*[sS]pec.js"
  ],
  "helpers": [
    "spec/helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

However, when loadTests.js is executed, it's current working directory is always the root of the project workspace, proj/, and it runs into something problematic somewhere in proj/frontend/, and bombs out.

As a workaround, I've configured jasmineExplorer.nodeArgv to load a module whose sole purpose is to change the current working directory to proj/backend/.

settings.json:

{
    "jasmineExplorer.config": "backend/spec/support/jasmine.json",
    "jasmineExplorer.nodeArgv": [
      "-r",
      "./set-jasmine-workdir-to-backend"
    ]
}

And proj/set-jasmine-workdir-to-backend.js:

require('process').chdir('backend');
hbenl commented 6 years ago

I have just added jasmineExplorer.cwd in version 1.3.0. The path to jasmine.json and the paths within that file are relative to the working directory, which should work with your setup.

justfalter commented 6 years ago

@hbenl That did the trick! Thank you very much!