created: 2020-06-06T21:46:32.000Z

[jest] SyntaxError: The string did not match the expected pattern

axios を使った jest のテストで以下のようなエラーが出た

SyntaxError: The string did not match the expected pattern.

これは jest ランタイムだと axios が XMLHttpRequest を使おうとしてしまうため発生するエラーだったようだ

jest.config.js に testEnvironment: 'node' を追加すればよい

module.exports = {
  verbose: true,
  testEnvironment: 'node',
  ...

参考

単体テストの考え方/使い方
[ad] 単体テストの考え方/使い方
Vladimir Khorikov, 須田智之 (単行本(ソフトカバー))