Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing conundrum when using webpack #3632

Closed
frenya opened this issue Apr 30, 2020 · 4 comments
Closed

Unit testing conundrum when using webpack #3632

frenya opened this issue Apr 30, 2020 · 4 comments
Labels
extensibility extension author content under /api support-request

Comments

@frenya
Copy link

frenya commented Apr 30, 2020

It seems to me there's one fundamental flaw when it comes to unit testing while using the suggested webpack configuration for distribution and a plain compilation using tsc for testing.

  • The main attribute of package.json must point to dist/extension.js
  • The code that is loaded by the extension host is therefore dist/extension.js even when unit testing
  • Any module you reference in your unit tests will be loaded from the out folder

This is not a good solution. For instance, you will run into problems in case your tests rely on any of those modules being initialised by the activate function (e.g. when you want to reference the extension context).

We need a way temporarily enforce loading the extension from out/extension.js for unit tests. Editing package.json every time is not a good option.

Maybe I'm just missing something, in which case please point me in the right direction.

@frenya
Copy link
Author

frenya commented May 1, 2020

This is a workaround I'm using at the moment, in case it helps anyone. It uses a global json tool to modify package.json just for the execution of unit tests. You can get it via npm install -g json

Excerpt from my package.json

{
  "scripts": {
    "test-compile": "npm run main-out && tsc -p ./",
    "main-out": "json -I -f package.json -e 'this.main=\"./out/extension.js\"'",
    "main-dist": "json -I -f package.json -e 'this.main=\"./dist/extension.js\"'"
  }
}

Excerpt from my launch.json

{
  "version": "0.2.0",
  "configurations": [{
      "name": "Extension Tests",
      "type": "extensionHost",
      "request": "launch",
      "runtimeExecutable": "${execPath}",
      "args": [
        "--extensionDevelopmentPath=${workspaceFolder}",
        "--extensionTestsPath=${workspaceFolder}/out/test/suite/index",
        "${workspaceFolder}/demo/demo.code-workspace"
      ],
      "outFiles": [
        "${workspaceFolder}/out/test/**/*.js"
      ],
      "preLaunchTask": "npm: test-compile",
      "postDebugTask": "npm: main-dist"
    }
  ]
}

@gregvanl gregvanl added extensibility extension author content under /api support-request labels May 1, 2020
@gregvanl
Copy link

gregvanl commented May 1, 2020

Hi @frenya I've asked the VS Code engineers and I don't think there is currently a better solution than your workaround. You can see microsoft/vscode#85779 which talks about the issue and imagines a switch like --overwrite-main-field-of-package-json.

@frenya
Copy link
Author

frenya commented May 2, 2020

Thanks for the update and the link to the related issue, Greg.

@gregvanl
Copy link

gregvanl commented May 2, 2020

@frenya Thanks for mentioning your workaround in the vscode issue to help other users.
I'll close this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensibility extension author content under /api support-request
Projects
None yet
Development

No branches or pull requests

2 participants