Skip to content

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

Closed

Description

@jeffyoung
  • 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"
},

Activity

  1. weinand commented on May 21, 2016

    @weinand
    Contributor

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

  2. added
    info-needed
    on May 23, 2016
  3. jeffyoung commented on May 23, 2016

    @jeffyoung
    Author

    Hi Andre Weinand (@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).

  4. aeschli commented on May 24, 2016

    @aeschli
    Contributor

    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.

  5. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

info-needed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions