{"record":{"id":"91a1e0a9c9f1551b","repo":"vitest-dev/vitest","slug":"expect-soft-can-only-be-used-inside-a-test","errorCode":null,"errorMessage":"expect.soft() can only be used inside a test","messagePattern":"expect\\.soft\\(\\) can only be used inside a test","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/utils.ts","lineNumber":120,"sourceCode":"    if (name !== 'withTest') {\n      utils.flag(this, '_name', name)\n    }\n\n    if (!utils.flag(this, 'soft')) {\n      // avoid WebKit's proper tail call to preserve stacktrace offset for inline snapshot\n      // https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit\n      try {\n        return fn.apply(this, args)\n      }\n      finally {\n        // no lint\n      }\n    }\n\n    const test: Test = utils.flag(this, 'vitest-test')\n\n    if (!test) {\n      throw new Error('expect.soft() can only be used inside a test')\n    }\n\n    try {\n      const result = fn.apply(this, args)\n\n      if (result && typeof result === 'object' && typeof result.then === 'function') {\n        return result.then(noop, (err) => {\n          handleTestError(test, err)\n        })\n      }\n\n      return result\n    }\n    catch (err) {\n      handleTestError(test, err)\n    }\n  }\n}","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/utils.ts#L102-L138","documentation":"`expect.soft()` records failures on the current test without aborting it, so it needs the active `Test` context (set via the `vitest-test` flag). If no test is in progress, there is nowhere to attach the soft failure and the call throws a plain `Error`.","triggerScenarios":"Calling `expect.soft()` at module top level, inside `beforeAll`/`afterAll`, in a setup hook, or in any context where Vitest has not bound the current test to the assertion.","commonSituations":"Moving an assertion out of a test body into a shared helper called from a hook; using `expect.soft` in a worker or non-test entry point; calling it eagerly during module load.","solutions":["Move the `expect.soft()` call inside an `it`/`test` body so a test context exists.","Use plain `expect()` in hooks where soft semantics are not required.","If asserting in a helper, ensure the helper is only invoked from within a test."],"exampleFix":"// before\nbeforeAll(() => {\n  expect.soft(config).toBeDefined()\n})\n\n// after\nit('has config', () => {\n  expect.soft(config).toBeDefined()\n})","handlingStrategy":"validation","validationCode":"// ensure expect.soft is only called where a test context is active\nif (!currentTest) { throw new Error('expect.soft requires a test') }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep expect.soft() inside it()/test() bodies only.","Use plain expect() in beforeAll/afterAll hooks.","Ensure shared helpers that assert are invoked from within a test."],"tags":["expect","soft-assertion","test-context","lifecycle"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}