Skip to content

Latest commit

 

History

History
178 lines (134 loc) · 5.13 KB

migrating-from-jest.md

File metadata and controls

178 lines (134 loc) · 5.13 KB

Migrating from Jest

A step-by-step guide to porting your test suite from Jest to Pure Test.

1. Update package.json

  "scripts": {
-   "test": "jest"
+   "test": "pure-test tests/"
  }

2. Rename test files

Jest uses transforms to handle .ts files. Pure Test requires .mjs or runtime-native TypeScript (Deno, Bun).

# Rename .test.ts → .test.mjs
for f in $(find tests -name '*.test.ts'); do
  mv "$f" "${f%.ts}.mjs"
done

If using Deno or Bun, you can keep .ts files and run them directly.

3. Update imports

- import { describe, it, expect, jest } from '@jest/globals'
+ import { describe, it, expect, spyFn, spyOn, restoreAllMocks } from '@igorjs/pure-test'

4. Replace jest namespace calls

  // jest.fn() → spyFn()
- const callback = jest.fn()
+ const callback = spyFn()

  // jest.fn(impl) → spyFn(impl)
- const double = jest.fn((x) => x * 2)
+ const double = spyFn((x) => x * 2)

  // jest.spyOn() → spyOn()
- jest.spyOn(obj, 'method')
+ spyOn(obj, 'method')

  // jest.restoreAllMocks() → restoreAllMocks()
- jest.restoreAllMocks()
+ restoreAllMocks()

  // jest.clearAllMocks() → clearAllMocks()
- jest.clearAllMocks()
+ clearAllMocks()

  // jest.resetAllMocks() → resetAllMocks()
- jest.resetAllMocks()
+ resetAllMocks()

  // afterEach cleanup
  afterEach(() => {
-   jest.restoreAllMocks()
+   restoreAllMocks()
  })

5. Mock instance methods (no changes needed)

These work identically in Jest and Pure Test:

spy.mockReturnValue(42)
spy.mockReturnValueOnce(1)
spy.mockImplementation(() => 'hi')
spy.mockImplementationOnce(() => 'once')
spy.mockResolvedValue({ data: 'ok' })
spy.mockResolvedValueOnce('first')
spy.mockRejectedValue(new Error('fail'))
spy.mockRejectedValueOnce(new Error('once'))
spy.mockClear()
spy.mockReset()
spy.mockRestore()
spy.mock.calls
spy.mock.results
spy.mock.lastCall

6. Remove jest.config.js

Pure Test has no config file. CLI args handle everything:

pure-test tests/                     # default (spec reporter)
pure-test tests/ --reporter tap      # TAP output for CI
pure-test tests/ --reporter json     # machine-readable

Features supported (no porting needed)

Most of the Jest API works as-is. These are supported by pure-test:

Jest feature pure-test equivalent
jest.useFakeTimers() useFakeTimers() or jest.useFakeTimers() — same API
jest.stubEnv(key, value) jest.stubEnv() — auto-restored by restoreAllMocks()
jest.stubGlobal(key, value) jest.stubGlobal() — auto-restored by restoreAllMocks()
expect.extend(matchers) expect.extend() — Proxy-dispatched, .not aware
--watch pure-test --watch — re-spawns child process per change
--testTimeout <ms> pure-test --testTimeout 5000
--testPathPattern <regex> pure-test --testPathPattern "auth"
--shard <i>/<n> pure-test --shard 1/4
--passWithNoTests pure-test --passWithNoTests
--listTests pure-test --listTests
--clearMocks / --resetMocks / --restoreMocks Same flags
--bail / --runInBand / --forceExit / --verbose Same flags

Features not supported

These Jest features are intentionally omitted. The table explains why and what to use instead.

Jest feature Why not Alternative
jest.mock('module') Module mocking is runtime-specific magic that requires transforms Use dependency injection: pass dependencies as parameters
jest.requireActual() Only needed alongside module mocking Not needed when using dependency injection
Snapshot testing File I/O, brittle, hides intent, diffs are hard to review Write explicit assertions that document expected behavior
--coverage (built-in) First-party coverage adds dependencies Use c8 on Node, deno coverage on Deno (per-runtime configs supported)
Worker isolation ~50ms overhead per file, unnecessary for well-written tests Use beforeEach/afterEach for per-test setup/teardown

Example: before and after

Before (Jest)

import { jest, describe, it, expect } from '@jest/globals'

describe('UserService', () => {
  afterEach(() => jest.restoreAllMocks())

  it('fetches a user', async () => {
    const mockFetch = jest.fn().mockResolvedValue({
      json: () => Promise.resolve({ id: 1, name: 'Alice' }),
    })

    const service = createUserService(mockFetch)
    const user = await service.getUser(1)

    expect(user.name).toBe('Alice')
    expect(mockFetch).toHaveBeenCalledWith('/users/1')
  })
})

After (Pure Test)

import { describe, it, expect, spyFn, restoreAllMocks, afterEach } from '@igorjs/pure-test'

describe('UserService', () => {
  afterEach(() => restoreAllMocks())

  it('fetches a user', async () => {
    const mockFetch = spyFn().mockResolvedValue({
      json: () => Promise.resolve({ id: 1, name: 'Alice' }),
    })

    const service = createUserService(mockFetch)
    const user = await service.getUser(1)

    expect(user.name).toBe('Alice')
    expect(mockFetch.mock.calls[0][0]).toBe('/users/1')
  })
})

Back to README