wallabyjs / public

Repository for Wallaby.js questions and issues
http://wallabyjs.com
758 stars 45 forks source link

Cannot resolve modules in pnpm monorepo with vitest #3370

Closed Ghirigoro closed 4 months ago

Ghirigoro commented 4 months ago

Issue description or question

I have a TypeScript project in a monorepo managed through pnpm (v9.0.4). I've tried to set up Wallaby for one of my internal packages but Wallaby seems unable to resolve any modules (both internal packages and npm modules).

I've tried:

But neither fixed (or changed) the problem.

I don't think it's an issue with IDE configuration, or the tests themselves. I pulled the code out into a clean folder of its own and was able to use Wallaby to run the tests with no problems (although using automatic configuration).

Not sure what to try next but any suggestions would be appreciated.

Wallaby diagnostics report

{
  editorVersion: '1.89.0',
  pluginVersion: '1.0.378',
  editorType: 'VSCode',
  osVersion: 'darwin 23.4.0',
  nodeVersion: 'v20.11.1',
  coreVersion: '1.0.1561',
  checksum: 'Njc4NTFiZjlmODk3N2U4ZDgyNjZlMzAzZjkwYjQ2OTksMTczOTIzMjAwMDAwMCww',
  config: {
    files: [ { pattern: 'src/**/*.ts', ignore: false, trigger: true, load: true, instrument: true, order: 1 } ],
    tests: [ { pattern: 'test/**/*.test.ts', ignore: false, trigger: true, load: true, test: true, order: 2 } ],
    autodetect: [ 'vitest' ],
    trace: true,
    diagnostics: {},
    captureConsoleLog: true,
    filesWithNoCoverageCalculated: [],
    runAllTestsInAffectedTestFile: false,
    updateNoMoreThanOneSnapshotPerTestFileRun: false,
    compilers: { '**/*.?(lit)coffee?(.md)': [Function (anonymous)] },
    logLimits: { inline: { depth: 5, elements: 5000 }, values: { default: { stringLength: 8192 }, autoExpand: { elements: 5000, stringLength: 8192, depth: 10 } } },
    preprocessors: {},
    maxConsoleMessagesPerTest: 100,
    autoConsoleLog: true,
    delays: { run: 0, edit: 100, update: 0 },
    workers: { initial: 0, regular: 0, recycle: false },
    teardown: undefined,
    hints: {
      ignoreCoverage: '__REGEXP /ignore coverage|istanbul ignore|c8 ignore/',
      ignoreCoverageForFile: '__REGEXP /ignore file coverage/',
      commentAutoLog: '?',
      testFileSelection: { include: '__REGEXP /file\\.only/', exclude: '__REGEXP /file\\.skip/' }
    },
    automaticTestFileSelection: true,
    runSelectedTestsOnly: false,
    mapConsoleMessagesStackTrace: false,
    extensions: {},
    env: { type: 'browser', kind: 'chrome', params: {}, viewportSize: { width: 800, height: 600 }, options: { width: 800, height: 600 }, bundle: true },
    reportUnhandledPromises: false,
    throwOnBeforeUnload: true,
    testFramework: { version: 'jasmine@3.10.0', configurator: 'jasmine@2.1.3', reporter: 'jasmine@2.1.3', starter: 'jasmine@2.1.3' },
    slowTestThreshold: 75,
    lowCoverageThreshold: 80,
    runAllTestsWhenNoAffectedTests: true,
    configCode: 'module.exports = function (wallaby) {\n\treturn {\n\t\tfiles: ["./src/**/*.ts"],\n\t\ttests: ["./test/**/*.test.ts"],\n\n\t\tautodetect: ["vitest"],\n\t\ttrace: true\n\t};\n};\n'
  },
  packageJSON: { dependencies: { '@voxpop/common': 'workspace:*', typescript: '5.4.5' }, devDependencies: { vite: '5.2.11', vitest: '1.6.0' } },
  fs: { numberOfFiles: 2 },
  debug: [
    '2024-05-09T01:24:08.491Z project waiting for initial run signal\n',
    '2024-05-09T01:24:08.603Z project Wallaby Node version: v20.11.1\n',
    '2024-05-09T01:24:08.603Z project Wallaby config: <homeDir>/Desktop/voxpop/packages/fsm/wallaby.cjs\n',
    '2024-05-09T01:24:08.630Z fs File system starting\n',
    '2024-05-09T01:24:08.644Z fs File system scan completed\n',
    '2024-05-09T01:24:08.647Z project File cache: <homeDir>/.vscode/extensions/wallabyjs.wallaby-vscode-1.0.378/projects/6a3713a3dfa72d17\n',
    '2024-05-09T01:24:08.678Z uiService Listening port 51235\n',
    '2024-05-09T01:24:08.693Z chromeRunner launch new instance (path: <default>, flags: --headless --disable-gpu --deterministic-fetch)\n',
    '2024-05-09T01:24:08.701Z workers Parallelism for initial run: 8, for regular run: 4\n',
    '2024-05-09T01:24:08.701Z workers Starting run worker instance #0\n',
    '2024-05-09T01:24:08.701Z workers Starting run worker instance #1\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #2\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #3\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #4\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #5\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #6\n',
    '2024-05-09T01:24:08.702Z workers Starting run worker instance #7\n',
    '2024-05-09T01:24:08.703Z workers Web server is listening at 58577\n',
    '2024-05-09T01:24:08.709Z chromeRunner Connection to Chrome Debugger Protocol on 127.0.0.1:58576 failed: Error: connect ECONNREFUSED 127.0.0.1:58576\n',
    '2024-05-09T01:24:08.710Z project Stopping process pool\n',
    '2024-05-09T01:24:08.710Z project File cache is up-to-date, starting full test run\n',
    '2024-05-09T01:24:08.712Z project Running postprocessor\n',
    '2024-05-09T01:24:08.713Z postprocessor New TypeScript language service is required\n',
    '2024-05-09T01:24:08.957Z project Postprocessor execution finished\n',
    '2024-05-09T01:24:08.957Z project Test run started; run priority: 3\n',
    '2024-05-09T01:24:08.958Z project Running all tests\n',
    '2024-05-09T01:24:08.958Z workers Starting test run, priority: 3\n',
    '2024-05-09T01:24:08.958Z chromeRunner Starting sandbox [worker #0, session #ybq9n]\n',
    '2024-05-09T01:24:09.212Z chromeRunner Connection to Chrome Debugger Protocol on 127.0.0.1:58576 succeeded\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #0\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #1\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #2\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #3\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #4\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #5\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #6\n',
    '2024-05-09T01:24:09.213Z workers Started run worker instance (immediate) #7\n',
    '2024-05-09T01:24:09.213Z chromeRunner Preparing sandbox [worker #0, session #ybq9n]\n',
    '2024-05-09T01:24:09.213Z browserRunner Total files to load in sandbox: 2\n',
    '2024-05-09T01:24:09.213Z browserRunner Sandbox is generated [worker #0, session #ybq9n]: http://localhost:58577/wallaby_sandbox0.html\n',
    '2024-05-09T01:24:09.213Z chromeRunner Prepared sandbox [worker #0, session #ybq9n]\n',
    '2024-05-09T01:24:09.213Z workers [worker #0, session #ybq9n] Running tests in sandbox\n',
    '2024-05-09T01:24:09.285Z workers Sandbox (active) [ybq9n] error: TypeError: Failed to resolve module specifier "@voxpop/common". Relative references must start with either "/", "./", or "../".\n',
    '2024-05-09T01:24:09.286Z workers Failed to map the stack to user code, entry message: Uncaught TypeError: Failed to resolve module specifier "@voxpop/common". Relative references must start with either "/", "./", or "../"., stack: TypeError: Failed to resolve module specifier "@voxpop/common". Relative references must start with either "/", "./", or "../".\n',
    '2024-05-09T01:24:09.286Z workers Sandbox (active) [ybq9n] error: TypeError: Failed to resolve module specifier "vitest". Relative references must start with either "/", "./", or "../".\n',
    '2024-05-09T01:24:09.286Z workers Failed to map the stack to user code, entry message: Uncaught TypeError: Failed to resolve module specifier "vitest". Relative references must start with either "/", "./", or "../"., stack: TypeError: Failed to resolve module specifier "vitest". Relative references must start with either "/", "./", or "../".\n',
    '2024-05-09T01:24:09.286Z workers [ybq9n] Loaded 0 test(s)\n',
    '2024-05-09T01:24:09.286Z workers [ybq9n] Run 0 test(s), skipped 0 test(s)\n',
    '2024-05-09T01:24:09.286Z workers [ybq9n] Sandbox is responsive, closing it\n',
    '2024-05-09T01:24:09.286Z chromeRunner Closing tab as requested\n',
    '2024-05-09T01:24:09.286Z chromeRunner Closing Chrome Tab\n',
    '2024-05-09T01:24:09.287Z project Test run finished\n',
    '2024-05-09T01:24:09.287Z project Processed console.log entries\n',
    '2024-05-09T01:24:09.287Z project Processed loading sequences\n',
    '2024-05-09T01:24:09.287Z project Processed executed tests\n',
    '2024-05-09T01:24:09.288Z project Processed code coverage\n',
    '2024-05-09T01:24:09.292Z project Test run result processed and sent to IDE\n'
  ]
}
ArtemGovorov commented 4 months ago

I pulled the code out into a clean folder of its own and was able to use Wallaby to run the tests with no problems (although using automatic configuration)

In this case autoconfig for an opened folder should hopefully work for you too. Can you please try opening your monorepo's root In VS Code, running Wallaby Select Configuration command, and select Automatic Configuration <Custom Directory>, then select the packages/fsm folder.

smcenlly commented 4 months ago

I spotted an error in your configuration file. The autodetect setting name is incorrectly cased, it should be autoDetect (with a capital D). Without your configuration, Wallaby is actually attempting to run your tests in the browser with Chrome instead of using vitest.

We also don't recommend setting files or tests with autoDetect unless there's a good reason. By default, Wallaby will be using vitest's file / test patterns so there shouldn't be a need to override them.

I believe @ArtemGovorov's solution will work for you, and is our recommended approach.

Ghirigoro commented 4 months ago

@ArtemGovorov + @smcenlly: Thanks for the quick response. Sorry that I failed to mention it, but I was already selecting the configuration in VSCode. The typo seems to be the issue, since now it works, although I found that if I select "Closest project in monorepo" automatic detection works even without specifying a config file.

Just want to say that Wallaby and Quokka are that rarest of things: a tool that highlights the strengths of the JS ecosystem while abstracting away the chaos rather than adding to it. I have no idea how you all manage to tame the mess of tooling into something coherent, but thanks for your dedication to such a great product.