Skip to content

Commit

Permalink
feat: don't expose snapshots, timer, module mocks in in-band tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Sep 10, 2024
1 parent 3bbeb6e commit 272d420
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 12 additions & 4 deletions src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { jestConfig } from './jest.config.js'
import { jestFunctionMocks } from './jest.fn.js'
import { jestModuleMocks } from './jest.mock.js'
import * as jestTimers from './jest.timers.js'
import './jest.snapshot.js'
import { setupSnapshots } from './jest.snapshot.js'
import { fetchReplay, fetchRecord, websocketRecord, websocketReplay } from './replay.js'
import { createCallerLocationHook, insideEsbuild } from './dark.cjs'
import { haveValidTimers } from './version.js'
Expand All @@ -14,7 +14,15 @@ import { format as prettyFormat } from 'pretty-format'

const { getCallerLocation, installLocationInNextTest } = createCallerLocationHook()

let addStatefulApis = true
if (process.env.EXODUS_TEST_ENVIRONMENT !== 'bundle') {
// We can't provide snapshots in inband tests yet, and mocks/timers are unsafe there
const files = process.argv.slice(1)
if (files.length === 1 && files[0].endsWith('/inband.js')) addStatefulApis = false
}

expect.extend(matchers)
if (addStatefulApis) setupSnapshots?.(expect)

let defaultTimeout = jestConfig().testTimeout // overridable via jest.setTimeout()
const defaultConcurrency = jestConfig().maxConcurrency
Expand Down Expand Up @@ -186,7 +194,7 @@ node.afterEach(() => {

if (globalThis.process) {
node.after(() => {
jestTimers.useRealTimers()
jestTimers?.useRealTimers?.()
const prefix = `Tests completed, but still have asynchronous activity after`

// give everything additional (configurable) defaultTimeout time to finish, otherwide fail
Expand Down Expand Up @@ -242,8 +250,8 @@ export const jest = {
return this
},
...jestFunctionMocks,
...jestModuleMocks,
...jestTimers,
...(addStatefulApis ? jestModuleMocks : {}),
...(addStatefulApis ? jestTimers : {}),
}

const wrapCallback = (fn) => (fn.length > 0 ? (t, c) => fn(c) : () => fn())
Expand Down
20 changes: 11 additions & 9 deletions src/jest.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
readSnapshot,
relativeRequire,
} from './engine.js'
import { expect } from 'expect'
import { format, plugins as builtinPlugins } from 'pretty-format'
import { jestConfig } from './jest.config.js'
import { getTestNamePath } from './dark.cjs'
Expand Down Expand Up @@ -111,7 +110,7 @@ const deepMerge = (obj, matcher) => {
return res
}

const snapOnDisk = (orig, matcher) => {
const snapOnDisk = (expect, orig, matcher) => {
if (matcher) {
expect(orig).toMatchObject(matcher)
// If we passed, make appear that the above call never happened
Expand Down Expand Up @@ -143,11 +142,14 @@ const snapOnDisk = (orig, matcher) => {
}
}

expect.extend({
toMatchInlineSnapshot: (obj, i) => wrap(() => snapInline(obj, i)),
toMatchSnapshot: (obj, matcher) => wrap(() => snapOnDisk(obj, matcher)),
toThrowErrorMatchingInlineSnapshot: (...a) => wrap(() => throws(a, (m) => snapInline(m, a[1]))),
toThrowErrorMatchingSnapshot: (...a) => wrap(() => throws(a, (m) => snapOnDisk(m))),
})
export function setupSnapshots(expect) {
expect.extend({
toMatchInlineSnapshot: (obj, i) => wrap(() => snapInline(obj, i)),
toMatchSnapshot: (obj, matcher) => wrap(() => snapOnDisk(expect, obj, matcher)),
toThrowErrorMatchingInlineSnapshot: (...a) => wrap(() => throws(a, (m) => snapInline(m, a[1]))),
toThrowErrorMatchingSnapshot: (...a) => wrap(() => throws(a, (m) => snapOnDisk(expect, m))),
})

expect.addSnapshotSerializer = (plugin) => plugins.push(plugin)
// eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
expect.addSnapshotSerializer = (plugin) => plugins.push(plugin)
}

0 comments on commit 272d420

Please sign in to comment.