Search code examples
mocha.jsrewire

running mocha tests through vsCode throws error 'source not found'


all my tests are running with mocha.js and I can debug them easily, except for one specific test file that causes the error Could not load source 'some path here/fileNameToTest.js': Source not found. to emerge.

In my launch.json file I have the following block -

      "type": "node",
      "request": "launch",
      "name": "Mocha Tests",
      "runtimeExecutable": "${env:HOME}/.nvm/versions/node/v14.16.0/bin/node",
      "cwd": "${workspaceRoot}",
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "internalConsoleOptions": "openOnSessionStart",
      "outFiles": [
        "${workspaceFolder}/**/*.js"
      ],
      "sourceMaps": true
    },

I've searched online and even tried chatGPT but I have no idea what could be the problem with it...

I have 2 dependencies in the test file - sinon.js,rewire.js and I am using them in most of the other test files..


Solution

  • The rewired module should be initialized in the before call of Mocha's describe.

    For example, instead of writing this:

    const rewire = require("rewire");
    const SUT = rewire(resolve("some/path/to/file"));
    
    describe("some/path/to/file", () => {
      // tests here...
    });
    

    We should write:

    const rewire = require("rewire");
    let SUT;
    
    describe(resolve("some/path/to/file"), () => {
      before(() => {
        SUT = rewire("some/path/to/file");
      })
      // tests here...
    });