For AI agents: the complete documentation index is available at https://rstest.rs/llms.txt, the full documentation bundle is available at https://rstest.rs/llms-full.txt, and this page is available as Markdown at https://rstest.rs/guide/advanced/scoped-cleanup.md.
close
  • English
  • Scoped cleanup

    Rstest supports scoped cleanup for temporary test changes. When your runtime or toolchain supports explicit resource management, using automatically restores resources when a block exits.

    using syntax

    rs.spyOn(), rs.stubEnv(), rs.stubGlobal(), and rs.useFakeTimers() return disposable resources that work with the using syntax.

    Spy cleanup

    Mocks and spies implement Symbol.dispose, so a spy created with rs.spyOn() is restored when the using block exits.

    logger.test.ts
    import { expect, rs, test } from '@rstest/core';
    
    test('logs a warning', () => {
      {
        using warn = rs.spyOn(console, 'warn').mockImplementation(() => undefined);
    
        console.warn('invalid payload');
        expect(warn).toHaveBeenCalledWith('invalid payload');
      }
    
      // console.warn is restored here
    });

    See Mock for more spy examples.

    Environment variables

    rs.stubEnv() returns a disposable resource. When it leaves a using scope, only that scoped env stub is restored.

    env.test.ts
    import { expect, rs, test } from '@rstest/core';
    
    test('uses test env', () => {
      {
        using _env = rs.stubEnv('NODE_ENV', 'test');
    
        expect(process.env.NODE_ENV).toBe('test');
      }
    
      // NODE_ENV is restored here
    });

    Nested env stubs are restored in reverse order:

    using _outer = rs.stubEnv('FEATURE_FLAG', 'on');
    expect(process.env.FEATURE_FLAG).toBe('on');
    
    {
      using _inner = rs.stubEnv('FEATURE_FLAG', 'off');
      expect(process.env.FEATURE_FLAG).toBe('off');
    }
    
    expect(process.env.FEATURE_FLAG).toBe('on');

    You can still call rs.unstubAllEnvs() to restore all env stubs in the current test.

    Globals

    rs.stubGlobal() also returns a disposable resource. This is convenient for temporarily replacing global APIs.

    global.test.ts
    import { expect, rs, test } from '@rstest/core';
    
    test('uses a global feature flag', () => {
      {
        using _global = rs.stubGlobal('__FEATURE_ENABLED__', true);
    
        expect(globalThis.__FEATURE_ENABLED__).toBe(true);
      }
    
      // __FEATURE_ENABLED__ is restored here
    });

    You can still call rs.unstubAllGlobals() to restore all global stubs in the current test.

    Fake timers

    rs.useFakeTimers() returns a disposable resource that restores real timers when the block exits.

    timer.test.ts
    import { expect, rs, test } from '@rstest/core';
    
    test('runs delayed work', () => {
      {
        using _timers = rs.useFakeTimers();
    
        const fn = rs.fn();
        setTimeout(fn, 1000);
    
        rs.advanceTimersByTime(1000);
        expect(fn).toHaveBeenCalledTimes(1);
      }
    
      expect(rs.isFakeTimers()).toBe(false);
    });