jestjs/jest · error · TypeError
Invalid first argument. It must be a callback function.
Error message
Invalid first argument. It must be a callback function.
What it means
The global `beforeEach` (and the identically-guarded afterEach, beforeAll, afterAll) wrappers in the jasmine interface require their first argument to be a function. If a non-function is passed (string, number, undefined), the wrapper throws a TypeError at jasmineLight.ts:120 before delegating to env.beforeEach. This catches mistakes where the hook is called with no body or wrong argument.
Solutions
- Pass a function: `beforeEach(() => { ... })`.
- If referencing a named function, pass the reference without invoking: `beforeEach(setup)` not `beforeEach(setup())`.
- For async hooks, use `beforeEach(async () => { ... })` or `beforeEach((done) => { ... })`.
Example fix
// before
beforeEach; // missing call/callback
// or
beforeEach(setup());
// after
beforeEach(() => { /* ... */ });
// or
beforeEach(setup); Defensive patterns
Strategy: type-guard
Validate before calling
// Guard all hook registrations.
function safeBeforeEach(fn: unknown) {
if (typeof fn !== 'function') {
throw new TypeError(`beforeEach requires a callback function, got ${typeof fn}`);
}
return beforeEach(fn as () => void);
} Type guard
function isHookFn(value: unknown): value is () => void | Promise<void> {
return typeof value === 'function';
} Prevention
- Always pass a function to beforeEach/afterEach/beforeAll/afterAll.
- Pass function references without invoking them (no parentheses).
- Use TypeScript so hook signatures require a function argument.
When it happens
Trigger: Calling `beforeEach()` with no arguments, `beforeEach('setup')`, or `beforeEach(undefined)`. The check `typeof arguments[0] !== 'function'` at jasmineLight.ts:119 triggers the throw. The same pattern guards afterEach (127), beforeAll (137), and afterAll (147).
Common situations: Writing `beforeEach` without a callback; passing a config object where the function was expected; a refactor that extracted the hook body into a variable that is undefined; calling `beforeEach(someFn())` instead of `beforeEach(someFn)`.
Related errors
- Invalid second argument
- Invalid second argument
- Argument passed to callFake should be a function, got
- describe does not expect any arguments
- Missing second argument. It must be a callback function.
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/9f6f0933835b274f.
Report an issue: GitHub.
Appendix: source
Thrown at packages/jest-jasmine2/src/jasmine/jasmineLight.ts:120
fdescribe(description: string, specDefinitions: SpecDefinitionsFn) {
return env.fdescribe(description, specDefinitions);
},
it() {
return env.it.apply(env, arguments);
},
xit() {
return env.xit.apply(env, arguments);
},
fit() {
return env.fit.apply(env, arguments);
},
beforeEach() {
if (typeof arguments[0] !== 'function') {
throw new TypeError(
'Invalid first argument. It must be a callback function.',
);
}
return env.beforeEach.apply(env, arguments);
},
afterEach() {
if (typeof arguments[0] !== 'function') {
throw new TypeError(
'Invalid first argument. It must be a callback function.',
);
}
return env.afterEach.apply(env, arguments);
},
beforeAll() {
if (typeof arguments[0] !== 'function') {
throw new TypeError(View on GitHub (pinned to 8e6d128e4a)