angular/angular.js · error · Error

Undefined argument `{}`; the argument is provided but not de

Error message

Undefined argument `{}`; the argument is provided but not defined

What it means

$httpBackend.when/expect (and every shortcut: whenGET, expectPOST, whenRoute, expectRoute, ...) validate the `url` slot with assertArgDefined: if an argument was provided at that position (args.length > index) but its value is undefined, they throw 'Undefined argument `url`; the argument is provided but not defined'. This distinguishes 'no URL given' (allowed, matches any URL) from 'URL given but undefined' (almost certainly a bug in the test data).

Source

Thrown at src/ngMock/angular-mocks.js:2097

        // Change url to `null` if `undefined` to stop it throwing an exception further down
        if (angular.isUndefined(url)) url = null;

        return $httpBackend[prefix](method, url, data, headers, keys);
      };
    });
  }

  function parseRouteUrl(url) {
    var strippedUrl = stripQueryAndHash(url);
    var parseOptions = {caseInsensitiveMatch: true, ignoreTrailingSlashes: true};
    return routeToRegExp(strippedUrl, parseOptions);
  }
}

function assertArgDefined(args, index, name) {
  if (args.length > index && angular.isUndefined(args[index])) {
    throw new Error('Undefined argument `' + name + '`; the argument is provided but not defined');
  }
}

function stripQueryAndHash(url) {
  return url.replace(/[?#].*$/, '');
}

function MockHttpExpectation(expectedMethod, expectedUrl, expectedData, expectedHeaders,
                             expectedKeys) {

  this.data = expectedData;
  this.headers = expectedHeaders;

  this.match = function(method, url, data, headers) {
    if (expectedMethod !== method) return false;
    if (!this.matchUrl(url)) return false;
    if (angular.isDefined(data) && !this.matchData(data)) return false;
    if (angular.isDefined(headers) && !this.matchHeaders(headers)) return false;

View on GitHub (pinned to d8f77817eb)

Solutions

  1. Pass a concrete matcher for the url: a string, RegExp, or function(url) returning true.
  2. If the URL is genuinely optional in your helper, branch instead of passing undefined: call when(method) with no second argument when you mean 'any URL'.
  3. Fix the source of the undefined value — typo'd variable, missing config key, or setup code that has not initialized the URL yet.

Example fix

// before
var base = config.apiBase; // undefined in tests
$httpBackend.whenGET(base + '/users').respond([]); // throws: url provided but undefined

// after
var base = config.apiBase || '';
$httpBackend.whenGET(base + '/users').respond([]);
Defensive patterns

Strategy: validation

Validate before calling

// Validate the url matcher before setting up the backend
function validUrlMatcher(url) {
  return typeof url === 'string' ||
         url instanceof RegExp ||
         typeof url === 'function';
}
if (!validUrlMatcher(url)) {
  throw new Error('url matcher is undefined; check the config/spec data');
}
$httpBackend.whenGET(url);

Type guard

function isUrlMatcher(url) {
  return url == null || // omitted = match any URL (valid)
    typeof url === 'string' ||
    url instanceof RegExp ||
    typeof url === 'function';
}

Prevention

When it happens

Trigger: $httpBackend.expectGET(undefined, headers); when(method, undefined); building the URL from a config object whose property is missing ($httpBackend.whenGET(CONFIG.apiBase)) where CONFIG.apiBase is undefined; a variable referenced with a typo so it evaluates to undefined at setup time.

Common situations: Expectation helpers driven by spec tables where one row lacks a URL; copy-pasting an expectation call and deleting the URL but leaving the trailing comma/args; environment-dependent base URLs that are undefined in the Karma test run; note only `url` is checked — data/headers may be passed undefined.

Related errors


AI-assisted analysis of angular/angular.js@d8f77817eb (2026-08-21). Data as JSON: /api/errors/f6883c6d9bbed8ed. Report an issue: GitHub.