Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created February 1, 2024 22:24
Show Gist options
  • Save timcharper/b61ea34a71eded1b5f5ce44d8cdde7ae to your computer and use it in GitHub Desktop.
Save timcharper/b61ea34a71eded1b5f5ce44d8cdde7ae to your computer and use it in GitHub Desktop.
Reproduce dape jest integratedTerminal issue

Create the files

mkdir -p src/__tests__/

cat <<-EOF > ./package.json
{
  "name": "debug-repro",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@types/jest": "^29.5.11",
    "jest": "^29.7.0",
    "ts-jest": "^29.1.1",
    "ts-node": "^10.9.2",
    "typescript": "^5.3.3"
  }
}
EOF

cat <<-EOF > ./tsconfig.json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "inlineSourceMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}
EOF

cat <<-EOF > src/add.ts
export function add(a: number, b: number): number {
  return a + b;
}
EOF

cat <<-EOF > src/__tests__/add.test.ts
import { add } from '../add';

test('adds 1 + 2 to equal 3', () => {
  console.log("This is some important output")
  expect(add(1, 2)).toBe(3);
});
EOF


cat <<-EOF > jest.config.js
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};
EOF

Install dependencies

npm install

Initial test (confirm everything is set up correctly)

npm test 2>&1

Testing dape

Load config and helper functions

;; dape needs to be loaded first
(require 'dape)

(defun dape-jest/find-file-buffer-default ()
  "Read filename at project root, defaulting to current buffer. Return vector of jest args to run said file"
  (let ((file (dape-buffer-default)))
    (if file
        `["--runInBand" "--no-coverage" ,file]
      (user-error "No file found"))))
(put 'dape-jest/find-file-buffer-default 'dape--minibuffer-hint t)

(defun dape-jest/ensure (config)
  "Ensure node is available, jest is installed, that the dapDebugServer is installed"

  (dape-ensure-command config)
  (let ((cwd (dape-cwd))
        (js-debug-file (expand-file-name
                        (dape--config-eval-value (car (plist-get config 'command-args)))
                        (dape--config-eval-value (plist-get config 'command-cwd))))
        (node-jest-file (expand-file-name
                         (dape--config-eval-value (plist-get config :program))
                         (dape--config-eval-value (plist-get config :cwd)))))
    (unless (file-exists-p js-debug-file)
      (user-error "Debug server file %S does not exist" js-debug-file))
    (unless (file-exists-p node-jest-file)
      (user-error "Jest executable not found at %S" node-jest-file))))

;; working config
(setq dape-jest-working-config `(jest-internal-console
                                 modes (js-mode js-ts-mode typescript-mode)
                                 ensure dape-jest/ensure
                                 command "node"
                                 command-cwd dape-command-cwd
                                 command-args (,(expand-file-name
                                                 (file-name-concat dape-adapter-dir
                                                                   "js-debug"
                                                                   "src"
                                                                   "dapDebugServer.js"))
                                               :autoport)
                                 port :autoport
                                 fn dape-config-autoport
                                 :type "pwa-node"
                                 :cwd dape-cwd
                                 :program "node_modules/.bin/jest"
                                 :args dape-jest/find-file-buffer-default
                                 :outputCapture "console"
                                 :sourceMapRenames t
                                 :pauseForSourceMap nil
                                 :autoAttachChildProcesses t
                                 :console "internalConsole"
                                 :outputCapture "std"
                                 :killBehavior "forceful"))
(setq dape-jest-broken-config
      (cons 'jest-integrated-terminal
            (plist-put (copy-sequence (cdr dape-jest-working-config)) :console "integratedTerminal")))

(add-to-list 'dape-configs dape-jest-working-config)
(add-to-list 'dape-configs dape-jest-broken-config)

Run it

Now open ./src/__tests__/add.test.ts, set a breakpoint on line 5, and run dape:

  • First with jest-internal-console (this one works)
  • Next with jest-integrated-terminal (this one fails)
    * Welcome to Dape REPL! *
    Available Dape commands: debug, next, continue, pause, step, out, restart, kill, disconnect, quit
    Empty input will rerun last command.
    
    Debug server listening at 127.0.0.1:33475
    Adapter connection shutdown without successfully initializing
    Configuration:
      modes (js-mode js-ts-mode typescript-mode)
      ensure dape-jest/ensure
      command "node"
      command-cwd "/home/tim/src/oss/debug-repro/"
      command-args ("/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js" "33475")
      port 33475
      fn dape-config-autoport
      :type "pwa-node"
      :cwd "/home/tim/src/oss/debug-repro/"
      :program "node_modules/.bin/jest"
      :args ["--runInBand" "--no-coverage" "src/__tests__/add.test.ts"]
      :outputCapture "std"
      :sourceMapRenames t
      :pauseForSourceMap nil
      :autoAttachChildProcesses t
      :console "integratedTerminal"
      :outputCapture "std"
      :killBehavior "forceful"
    TypeError: Cannot read properties of undefined (reading 'processId')
        at new t (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:131:38022)
        at El.launchProgram (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:136:430)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async n (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:133:4088)
        at async _l.launch (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:131:30729)
        at async eb.captureLaunch (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:136:9297)
        at async eb._launch (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:136:9044)
        at async Promise.all (index 3)
        at async eb._boot (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:136:8168)
        at async Server.<anonymous> (/home/tim/.emacs.d/debug-adapters/js-debug/src/dapDebugServer.js:142:3662)
        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment