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 82ea66e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { assert, utilFormat, isPromise, mock } from './engine.js'
import * as node from './engine.js'
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 { fetchReplay, fetchRecord, websocketRecord, websocketReplay } from './replay.js'
import { createCallerLocationHook, insideEsbuild } from './dark.cjs'
import { haveValidTimers } from './version.js'
Expand All @@ -14,7 +11,22 @@ import { format as prettyFormat } from 'pretty-format'

const { getCallerLocation, installLocationInNextTest } = createCallerLocationHook()

async function loadModules() {
if (process.env.EXODUS_TEST_ENVIRONMENT !== 'bundle') {

Check failure on line 15 in src/jest.js

View workflow job for this annotation

GitHub Actions / Lint

Merge this if statement with the nested one
// We can't provide snapshots in inband tests yet, and mocks/timers are unsafe there
if (process.argv.length === 2 && process.argv[1].endsWith('/inband.js')) return {}

Check failure on line 17 in src/jest.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected `if` as the only statement in a `if` block without `else`
}

return {
jestTimers: await import('./jest.timers.js'),
...(await import('./jest.mock.js')),
...(await import('./jest.snapshot.js')),
}
}

const { jestModuleMocks, jestTimers, setupSnapshots } = await loadModules()
expect.extend(matchers)
setupSnapshots?.(expect)

let defaultTimeout = jestConfig().testTimeout // overridable via jest.setTimeout()
const defaultConcurrency = jestConfig().maxConcurrency
Expand Down Expand Up @@ -186,7 +198,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
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 82ea66e

Please sign in to comment.