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

Developing tests in a different directory/repository #265

Open
eliericha opened this issue Apr 23, 2024 · 2 comments
Open

Developing tests in a different directory/repository #265

eliericha opened this issue Apr 23, 2024 · 2 comments

Comments

@eliericha
Copy link

Hello folks,

I would like to develop tests in a different directory/repository than the extension source code. This is to foster a separation of the test code from the functional code and to make it easier for other teams to get involved in writing tests.

I'm struggling to do this separation because all of the examples I found assume that the tests are in the same root directory as the extension source code.

I tried implementing the separation on a fork of the helloworld-test-cli-sample.

I created a separate directory with its own package.json and tsconfig.json and .vscode-test.mjs.

I used the "workspaces" feature in package.json to allow the test code to import modules from the extension as follows:

// Import the extension to test it
import * as myExtension from 'helloworld-sample/src/extension';

and I used extensionDevelopmentPath in .vscode-test.mjs to let vscode-test know where the extension is.

This allowed me to compile the tests successfully, and test execution is able to load the extension.

However the execution stumbles on the import line in the compiled JS test file:

Loading development extension at /home/richa/ancr/src/vscode-extension-samples/helloworld-test-cli-sample
Error: Cannot find module 'helloworld-sample/src/extension'
Require stack:
- /home/richa/ancr/src/vscode-extension-samples/helloworld-test-cli-sample-tests/out/test/suite/extension.test.js
- /home/richa/ancr/src/vscode-extension-samples/helloworld-test-cli-sample-tests/node_modules/mocha/lib/mocha.js
- /home/richa/ancr/src/vscode-extension-samples/helloworld-test-cli-sample-tests/node_modules/mocha/index.js
...

Indeed the JS file has the following module import that can't be resolved:

// extension.test.js
// Import the extension to test it
const myExtension = require("helloworld-sample/src/extension");

I tried reading various documentations about module resolution but I can't figure out what I should do.

Should I somehow get tsc to emit a resolved require statement with the actual path to the compiled extension?

Or should I somehow tell the test run-time environment how to resolve the helloworld-sample package based on the package.json workspaces?

I would appreciate any advice on the matter.

Thanks!

@connor4312
Copy link
Member

I notice you're trying to import helloworld-sample/src/extension -- but this would try to import the source typescript file, not the compiled extension file. I don't think tsc will automatically change the import path when compiling in a workspace (you can verify by looking at your compiled files) so you may want to try helloworld-sample/out/extension instead.

@eliericha
Copy link
Author

Ah indeed. Changing the import to helloworld-sample/out/extension worked. Thanks!

I also had to add "declaration": true in the tsconfig.json of the extension workspace to produce a TS declaration file under out/ that allows me to make the above import.

Would you like me to cleanup the example and submit it in a PR? I realize that I moved the tests to a separate workspace. Instead I could keep the original workspace as is, and add new sample tests in the separate workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants