@magic/test

Test Isolation

@magic/test supports test isolation to prevent tests from affecting each other. Tests in the same suite can share state, but you can isolate them:

export default [  // This test runs in isolation from others  {    fn: () => {      const state = { counter: 0 }      state.counter++      return state.counter    },    expect: 1,    info: 'isolated test with local state',  },]

__isolate

Global Isolation Mode:

By default, tests in the same file share global state. To enable strict isolation where each test gets a fresh environment, set `export const __isolate = true` at the top of your test file.

export const __isolate = trueexport default [  { fn: () => (global.test = 1), expect: 1 },  { fn: () => global.test === undefined, expect: true, info: 'fresh global state' },]

This ensures each test runs with a fresh global environment, preventing state leakage between tests.

Programmatic Detection

You can programmatically check if a suite requires isolation using the `suiteNeedsIsolation` utility:

import { suiteNeedsIsolation } from '@magic/test'const needsIsolation = suiteNeedsIsolation(tests)

This is useful for custom runners or when building test tooling.