-
Notifications
You must be signed in to change notification settings - Fork 59
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
unable to import vscode in test using jest #37
Comments
Hi @matthoang08, thanks for giving jest a try. Would https://jestjs.io/docs/en/configuration#globals-object or https://jestjs.io/docs/en/configuration#globalsetup-string help you? jestjs/jest#708 also seems to mention handling global jquery. I can give it a try later in Monday as well. If this works I can put together an official recipe. |
I was able to reference So to make it work with declare namespace NodeJS {
export interface Process {
vscode?: any
}
} Then in import * as path from 'path';
import { runCLI } from 'jest-cli';
import * as vscode from 'vscode';
process.vscode = vscode;
export function run(): Promise<void> {
return new Promise((resolve, reject) => {
... And finally in your test file you can reference describe('Extension Test Suite', () => {
beforeEach(() => {
process.vscode.window.showInformationMessage('Start all tests.');
});
it('Sample test', () => {
process.vscode.commands.executeCommand('extension.helloWorld');
expect(1).toBe(1);
});
}); It is very hacky way of accessing vscode instance, so try to find a better solution. I just post my findings FYI. |
Are you sure this actually works? I've tried implementing that, but it still fails with the same error when the tests try to load the file that is still loading vscode (in your example that would be the one containing function run()) |
Any progress on this? |
having this issue also. |
seems like it can't be imported for tests, but it can be by the vs code extension when you do an e2e test. does that sound right? |
Any updates on this? I am having the same problem in my setup. |
I found a very slim workaround! I was also struggling like hell with that. I found that article which got me on the right track: https://www.richardkotze.com/coding/unit-test-mock-vs-code-extension-api-jest However to make it run in the default TypeScript extension workflow you need to put the Within the export const vscode = {
// mock the vscode API which you use in your project. Jest will tell you which keys are missing.
}; In my case an empty |
@vinnichase You have a flaw there. The way vscode extensions are meant to be tested is IN vscode precisely so that you test vscode fully integrated. This is also the way microsoft introduces you to extension testing... Otherwise any vscode update may blow up your extension because you forgot to update your mocks.. You also won't be able to properly test by passing via the process module, because jest runs everything via the node "vm" module ( https://nodejs.org/api/vm.html ) which prevents you from selectively modifying the vscode object if the vscode object is not part of the vms global object (the vm context). That is you will be able to modify it IN the test but your modifications to the vscode module will NOT propagate outwards to your extension code, because of the vm. It does not matter that the process PID is the same. The key point is the "vm" module that jest uses. The correct way to use jest inside a vscode host is you do
and then
You still wont be able to IMPORT vscode like normal in your tests, which is annoying with those import quickfixes, but vscode is available as a global now and now you can mock those pesky extensions error messages etc. and changes will propagate outwards |
Thanks for the hint! However this breaks my understanding of unit tests which I was doing in my case. Your approach would apply better in an integration test. I didn't even mock There used to be a special vscode typings package on npm for that purpose but maintenance stopped and it deviated from the ones exposed in the vscode extension context. So thats ridiculous that you can not use the interace description (which is by definition the abstraction of the running code) separately from the runtime context. |
So I actually manage to inject vscode into jest, and it's not that ugly. This works for me, hope it works for someone else too! First of all, create your own vscode environment file // vscode-environment.js
const NodeEnvironment = require('jest-environment-node');
const vscode = require('vscode');
class VsCodeEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
this.global.vscode = vscode;
}
async teardown() {
this.global.vscode = {};
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = VsCodeEnvironment; next, make jest use this environment, I did it by adding it into my jest config // jest config
module.exports = {
// more config
testEnvironment: './vscode-environment.js',
// more config
}; now we are just missing the tricky and most important part. Create a file, call it whatever you want, I called it // vscode.js
module.exports = global.vscode; Final step, go to your jest config, and define a module mapper like this const path = require('path');
module.exports = {
testEnvironment: './vscode-environment.js',
modulePaths: ['<rootDir>'],
moduleNameMapper: {
vscode: path.join(__dirname, 'test-jest', 'vscode.js') // <----- most important line
},
}; my vscode file is in test-jest/vscode.js place yours wherever you need! By doing this, any line that is importing vscode will really import it into jest, so you are able to run methods, mock them or do whatever you want because you have the real vscode object! I hope this helps someone!! [EDITED] Forgot to mention you also need to define a custom Vscode jest runner if someone needs this too I'll add it to this comment! |
@MarioCadenas I would appreciate some guidance on how to define a custom VS Code Jest runner! |
Hi @mark-wiemer ! of course! What can I help you with? I'll do my best 😄 |
Thanks @MarioCadenas, but just a few hours after posting that I started to consider other options (e.g. just using Mocha instead). I have little experience in this area, so I think I'll do some reading of my own before coming back with more specific questions. If I never reply, it's because I went with an alternative :) Thanks anyway! |
I try this, but the console throw a error: Cannot find module 'vscode' |
Do you have a repo to check this? Maybe I can try to help 😄 |
@MarioCadenas would it be possible to add the custom vscode jest runner here. Your solution unfortunately is unusable verbatim without it. Thank you. |
FYI: I've pulled things from here and there around the web and ended up: https://github.com/daddykotex/jest-tests it seems to work fine and uses jest as a test runner instead of mocha |
Hi, I know its an old thread, but I'm dealing with the same error, the test environment can't find the |
HI @DiegoWong I can try to help if you provide a repository |
Hi @MarioCadenas, Here's the repo. Basically is the same repo as @daddykotex but adding webpack as the build tool. Thanks in advance for your help! |
Adding a mocks folder for vscode per blog article here is a clean/efficient solution: https://www.richardkotze.com/coding/unit-test-mock-vs-code-extension-api-jest |
The problem here is you are using stubs/mocks, so you can't do actual integration/E2E/UI tests and verify things work in vscode instance itself, or verify the API hasn't changed. That's the real benefit of using vscode-test here vs. just mocking/stubbing out all the external calls. |
To summarize the workaround for this issue:
Alternatively, mock all your calls to |
I managed to get tests working with the latest version of Jest (29.5.0 as of writing). The gist is to use As a bonus this update also lets you use VSCode up to 1.73 to run tests. Later versions fail to run tests, I haven't really investigated why. See this commit for the full diff. @daddykotex Let me know if you'd like me to open a PR on your repo with the updated setup. |
Sure @Blond11516 , I'll merge it no problem |
since it is hard to deal with `import vscode`. See microsoft/vscode-test#37 Signed-off-by: Qingpeng Li <[email protected]>
To address this issue, I created jest-mock-vscode. It can help with testing logic using unit tests. It is not designed for E2E/UI testing. The vscode-test runner is best for that. |
Hi, I'm trying to run some integration tests for my vscode extension using jest. Unit tests work fine since I'm able to mock vscode however, when I'm trying to run an integration test I'm getting
Cannot find module 'vscode'
in the files when i'm using vscode api (import * as vscode from 'vscode'
)jest config looks like this:
runTest.ts
index.ts
I installed both
vscode-test
and@types/vscode
as dev dependencies. I'm not sure why my tests are unable to find the 'vscode' dependency. Vscode is the only dependency that has this issue, other modules work fine. Can anyone help?Thanks!
The text was updated successfully, but these errors were encountered: