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

Unable to run extension unit tests via gulp (cannot find module 'vscode') #6586

Closed
jeffyoung opened this issue May 20, 2016 · 4 comments
Closed
Assignees
Labels
info-needed Issue requires more information from poster

Comments

@jeffyoung
Copy link

  • VSCode Version: 1.1.1
  • OS Version: Windows 10

I'm developing an extension for VS Code and writing unit tests against my objects used by that extension. Those tests run successfully from within VS Code. However, I also run them as part of my CI build and I do that via gulp. I have a gulp task that uses "gulp mocha" to run the tests. Those tests run successfully via gulp until I bring in a class that, anywhere in its transitive closure of module references, pulls in 'vscode'.

My gulp output is below:

[14:00:48] Starting 'test'...
[14:00:48] { [Error: Cannot find module 'vscode']
  code: 'MODULE_NOT_FOUND',
  name: 'Error',
  message: 'Cannot find module \'vscode\'',
  stack: 'Error: Cannot find module \'vscode\'\n
at Function.Module._resolveFilename     (module.js:325:15)\n
at Function.Module._load (module.js:276:25)\n
at Module.require     (module.js:353:17)\n
at require (internal/module.js:12:17)\n
at Object.<anonymous> (C:\\vsts-    vscode.repro.0\\out\\src\\myobject.js:2:18)\n
at Module._compile (module.js:397:26)\n
at     Object.Module._extensions..js (module.js:404:10)\n
at Module.load (module.js:343:32)\n
at     Function.Module._load (module.js:300:12)\n
at Module.require (module.js:353:17)',
  showStack: true,
  showProperties: true,
  plugin: 'gulp-mocha' }
[14:00:48] 'test' errored after 45 ms
[14:00:48] Error in plugin 'gulp-mocha'
Message:
    Cannot find module 'vscode'
Details:
    code: MODULE_NOT_FOUND
Stack:
Error: Cannot find module 'vscode'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\vsts-vscode.repro.0\out\src\myobject.js:2:18)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)

In the call stack, there's a reference to out\src\myobject.js:2:18. At that location is this statement:

const vscode_1 = require("vscode");

In my typescript source, that is:

import { workspace } from "vscode";

If I remove the code that uses workspace, the tests go back to running successfully.

After going through the documentation regarding node.js module resolution, I can understand why I get MODULE_NOT_FOUND. Since "vscode" isn't a core module and isn't a relative reference (".", "/", "../"), node will look for the package.json file. Assuming it finds it (procmon tells me it does), it looks for a "main" field. I'm guessing, at this point, it doesn't find the "main" field and fails the resolution.

Presumably, when running the tests in VS Code, the resolution also includes locating the typings file and is thus successful.

What I can't figure out is what I might do to resolve the reference to "vscode" when I run tests via gulp.

Any thoughts?

myobject.ts

import { workspace } from "vscode";   
export class MyObject {
    constructor() {
        let configuration = workspace.getConfiguration();
    }
}

myobject.test.ts

import { MyObject } from "../src/myobject";
var assert = require("chai").assert;    
describe("MyObject", function() {
    it("should verify MyObject", function() {
        new MyObject();
    });
});

package.json (scripts and dependencies)

"scripts": {
  "vscode:prepublish": "node ./node_modules/vscode/bin/compile",
  "compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
  "postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
  "chai": "^3.4.1",
  "gulp": "^3.9.0",
  "gulp-mocha": "^2.2.0",
  "gulp-tslint": "^3.2.0",
  "gulp-util": "^3.0.7",
  "should": "^8.1.1",
  "typescript": "^1.7.5",
  "vscode": "^0.11.x"
},
@weinand
Copy link
Contributor

weinand commented May 21, 2016

@jeffyoung make sure that the "postinstall" script runs because it pulls in the correct version of the 'vscode' node module.

@weinand weinand added the info-needed Issue requires more information from poster label May 23, 2016
@jeffyoung
Copy link
Author

Hi @weinand. I've made sure that step gets run so the proper version of vscode gets installed.

> node ./node_modules/vscode/bin/install
Detected VS Code engine version: ^0.10.7
Found minimal version that qualifies engine range: 0.10.8
Fetching vscode.d.ts from:     https://raw.githubusercontent.com/Microsoft/vscode/0.10.8/src/vs/vscode.d.ts
vscode.d.ts successfully installed!

I can provide a small-ish repro that will run successfully within VS Code but a minimal gulpfile.js will result in the missing module error (it's about an 11KB zip file).

@aeschli
Copy link
Contributor

aeschli commented May 24, 2016

Note that vscode is not a real node_module. You can only present in the context of VSCode.
We have a special test runner that first starts VSCode and then runs your test inside the extension host.

https://github.com/Microsoft/vscode/blob/master/scripts/test-integration.sh shows how to run such tests on a CI build. This requires, however, VSCode being present on the machine running the tests.

@bpasero
Copy link
Member

bpasero commented May 24, 2016

Yes, you cannot run the integration tests via gulp, please follow the instructions: http://code.visualstudio.com/docs/extensions/testing-extensions#_running-tests-automatically-on-travis-ci-build-machines

@bpasero bpasero closed this as completed May 24, 2016
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

4 participants