Cannot find module 'vscode' error when running jest test (typescript VSCode project)

Viewed 446

I have a VSCode extension written in TypeScript and I am currently in the process of writing tests using jest. When running jest extension.unit.test.ts (or npm test), I get the following error:

enter image description here

I see other StackOverflow posts similar to this involving the pathsToModuleNameMapper method, but I couldn't find a fix for this issue.

Here are the related files:

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "out",
    "lib": ["es6"],
    "types": ["node", "jest"],
    "sourceMap": true,
    "rootDir": "src",
    "strict": true, 
    "paths": {
      "~/*": ["*"],
    }
  },
  "exclude": ["node_modules"]
}

jest.config.js

const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');

module.exports = {
    preset: "ts-jest",
    testEnvironment: "node",
    moduleFileExtensions: ['ts', 'js', 'json', 'node'],
    moduleDirectories: ['node_modules', 'src'],
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' } )
};

package.json

"scripts": {
    ...
    "test": "jest",
  },
  "devDependencies": {
    "@types/glob": "^7.1.3",
    "@types/jest": "^27.5.1",
    "@types/node": "^12.11.7",
    "@types/vscode": "^1.50.0",
    "jest": "^28.1.0",
    "ts-jest": "^28.0.3",
    "typescript": "^4.6.2",
    ...
  }

extension.unit.test.js

import * as vscode from "vscode";
import * as extension from "../../extension";
import * as path from 'path';

jest.mock("vscode");
jest.mock("fs");
jest.mock("fs-extra");
jest.mock("util");
...

Folder structure:

enter image description here

Could someone help me with this?

Edit: I initially thought this was an issue with all the imports, but it seems to only be the 'vscode' module import that's failing.

0 Answers
Related