skip to content

In Detox, when do you use device.setURLBlacklist() or the detoxURLBlacklistRegex launch argument, and how do the patterns have to be written?

level: middleimportance: nice to knowfreq 22%

answer

  1. noise requests, not user-flow ones
  2. long polling, sockets, telemetry
  3. launch arg covers startup traffic
  4. strings or RegExp, flags i m s
  5. setURLBlacklist([]) resets it

basics

~20 s

Use them when requests that never finish, such as long polling or background uploads, keep Detox's network synchronization busy. setURLBlacklist works mid-test; detoxURLBlacklistRegex applies from launch. Both take regex strings or RegExp objects with only the i, m and s flags.

solid answer

~50 s

Detox waits for every in-flight network request, which blocks tests when the app keeps a request open by design: a long-poll for live draw results, a WebSocket-style connection, or background telemetry uploads. Those are noise for a user-flow test, so exclude them from network synchronization. `await device.setURLBlacklist([/draws\/live/, '.*telemetry.*'])` applies from that point in the test, and `setURLBlacklist([])` clears it. For requests the app makes during startup, the list must be in place before launch: `device.launchApp({ newInstance: true, launchArgs: { detoxURLBlacklistRegex: [/draws\/live/] } })`. Both accept strings (regular-expression sources) or `RegExp` objects; only the `i`, `m` and `s` flags are portable, and `g`, `y`, `d`, `u` or `v` throw a `TypeError`. The array form is recommended over the legacy single-string form, whose comma splitting could corrupt patterns like `\d{1,3}`. The blacklist affects only network tracking; animations and timers are still synchronized.

code

javascript · 13 lines
javascript
const NOISY_URLS = [/draws\/live/, /telemetry\.example\.com/i];

beforeAll(async () => {
  await device.launchApp({
    newInstance: true,
    launchArgs: { detoxURLBlacklistRegex: NOISY_URLS },
  });
});

it('buys a ticket while live results poll in the background', async () => {
  await element(by.id('buy-ticket')).tap();
  await expect(element(by.id('purchase-confirmation'))).toBeVisible();
});

go deeper

for a junior

Recall that Detox waits for network requests and that noisy endpoints can be excluded with setURLBlacklist.

for a middle

Explain the launch-argument versus mid-test forms, the RegExp and string pattern rules, and the portable i, m and s flags.

for a senior

Decide which requests are noise for a flow, keep patterns narrow and centralized, and never exclude a request an assertion depends on.

for a principal

Prefer URL exclusion over disabling synchronization as a team policy, and review the shared list so it cannot silently hide broken dependencies.

## The problem: requests that never finish **Detox** treats every in-flight network request as a reason to wait. That is what lets a test tap "Buy ticket" and immediately assert the confirmation: Detox waits for the purchase request to return. But some requests are **not part of the flow under test** and may never complete while the screen is open: - a **long-poll** that asks the lottery server for live draw results and stays open until a result arrives; - a persistent streaming connection to a real-time endpoint; - **background telemetry** or log uploads that retry; - requests to a local development service on the test machine. While any of these is pending, the app is never network-idle and every step blocks. Detox's answer is a **URL blacklist**: requests whose URL matches a pattern are ignored by network synchronization. ## Two ways to set it | API | When it applies | Typical use | |---|---|---| | `device.setURLBlacklist(patterns)` | from the call onwards, in the running app | a screen that starts polling mid-test | | `detoxURLBlacklistRegex` launch argument | from app launch | requests fired during startup | Mid-test: ```js await device.setURLBlacklist([/draws\/live/, /telemetry\.example\.com/]); // ... steps on the live-draw screen ... await device.setURLBlacklist([]); // reset ``` From launch, because the launch process is itself synchronized and a request that starts before the test can call `setURLBlacklist` can block it: ```js await device.launchApp({ newInstance: true, launchArgs: { detoxURLBlacklistRegex: [/draws\/live/, /telemetry\.example\.com/] }, }); ``` ## How patterns must be written Both APIs accept an **array** whose entries are either strings, interpreted as regular-expression sources, or `RegExp` objects, and a mix of both is allowed. The rules that trip people up: 1. **Flags.** Only `i`, `m` and `s` are portable across iOS and Android. A `RegExp` with `g`, `y`, `d`, `u` or `v` throws a `TypeError` at launch or when `setURLBlacklist` is called. 2. **Escaping.** A string is a regex source, so `.` matches any character. `'.*127.0.0.1.*'` works, but `/.*127\.0\.0\.1.*/` says what you mean. 3. **Array form over the legacy string.** `detoxURLBlacklistRegex` also accepts an older single-string format with escaped quotes and parentheses. It is sensitive to formatting, and its comma splitting used to corrupt patterns containing commas, such as `\d{1,3}`. The array form avoids both problems. 4. **Match the whole URL you mean.** A pattern like `/live/` also excludes an unrelated `/api/livestream-config` request that the test may need to wait for. ## Finding the right URL to exclude Do not guess. When a step blocks for longer than `session.debugSynchronization` (10 seconds by default), Detox's busy log lists the in-flight requests by URL, for example "1 network requests with URLs: URL #1: https://draws.example.com/live?since=…". Build the narrowest pattern that matches exactly those URLs, anchor the host and path, and leave query strings out of the pattern when they vary. If the log shows no network entries at all, the blocker is something else, such as an animation or a short timer, and a URL blacklist will not help. ## What the blacklist does not do - It does **not** block or mock the request; the app still sends it and receives the response. It only tells Detox not to wait for it. - It does **not** exclude animations or timers. An endless loader stays a blocker; Detox has no animation blacklist. - It does **not** fix a request that should have completed. If the draw-results endpoint hangs because the test server is down, excluding it hides a broken dependency and the test may assert against a screen that never received its data. ## Deciding whether to blacklist Ask whether the user flow under test **depends** on the request's answer: - If it does, keep it synchronized and fix the slowness or the server. - If it does not, and the request is long-lived or background by design, exclude it, as narrowly as possible, and ideally as a named constant shared by the suite so reviewers see the list in one place. ## Summary The URL blacklist is the fine-grained alternative to switching synchronization off: every other resource stays synchronized, and only named noisy endpoints are ignored. Use the launch argument for startup traffic, `setURLBlacklist` for traffic that begins mid-test, portable regex flags, and the array form.

  • Why is setURLBlacklist in the first line of a test the wrong tool for a request fired at app launch?
    The launch process is itself synchronized, and a long-lived request started during launch can keep the app busy before the test reaches the line that sets the blacklist. Detox documents the `detoxURLBlacklistRegex` launch argument for exactly this case: endpoints the app calls on startup.
  • Does blacklisting a URL change what the app receives from it?
    No. The request is still sent and answered normally; Detox just stops counting it as a reason to wait. If the test needs the response, blacklisting is wrong, because Detox may move on before the data has arrived.

saying these in an interview costs you the question

  • A blacklisted URL is blocked, so the app never receives a response.
  • setURLBlacklist also stops Detox waiting for animations on that screen.
  • Any JavaScript RegExp flag, including g, works in the blacklist.
  • setURLBlacklist in the test body covers requests made during launch.
  • Blacklist the endpoint whose response the assertion depends on.