Skip to content

Commit

Permalink
introduce engine.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jul 19, 2024
1 parent ff3a125 commit 8c0530e
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 40 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@
"bin/babel.cjs",
"bin/jest.js",
"src/dark.cjs",
"src/engine.js",
"src/engine.node.cjs",
"src/engine.select.cjs",
"src/jest.js",
"src/jest.config.js",
"src/jest.environment.js",
"src/jest.fn.js",
"src/jest.mock.js",
"src/jest.snapshot.js",
"src/jest.timers.js",
"src/node.js",
"src/tape.js",
"src/version.js",
"!__tests__",
Expand Down
19 changes: 19 additions & 0 deletions src/engine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import engine from './engine.select.cjs' // need to be sync for non-preloaded imports into cjs

const { assert, assertLoose } = engine
export { assert, assertLoose }

const { mock, describe, test, beforeEach, afterEach, before, after } = engine
export { mock, describe, test, beforeEach, afterEach, before, after }

const { builtinModules, syncBuiltinESMExports } = engine
export { builtinModules, syncBuiltinESMExports }

const { utilFormat, isPromise, nodeVersion } = engine
export { utilFormat, isPromise, nodeVersion }

const { baseFile, relativeRequire, isTopLevelESM } = engine
export { baseFile, relativeRequire, isTopLevelESM }

const { snapshot, readSnapshot, setSnapshotSerializers, setSnapshotResolver } = engine
export { snapshot, readSnapshot, setSnapshotSerializers, setSnapshotResolver }
39 changes: 39 additions & 0 deletions src/engine.node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const assert = require('node:assert/strict')
const assertLoose = require('node:assert')
const { types, format: utilFormat } = require('node:util')
const { existsSync, readFileSync } = require('node:fs')
const { normalize, basename, dirname, join: pathJoin } = require('node:path')
const { createRequire, builtinModules, syncBuiltinESMExports } = require('node:module')
const nodeTest = require('node:test')

const { mock, describe, test, beforeEach, afterEach, before, after } = nodeTest

const isPromise = types.isPromise
const nodeVersion = process.versions.node

const files = process.argv.slice(1)
const baseFile = files.length === 1 && existsSync(files[0]) ? normalize(files[0]) : undefined

const relativeRequire = baseFile ? createRequire(baseFile) : require
const isTopLevelESM = () => !baseFile || !Object.hasOwn(relativeRequire.cache, baseFile) // assume ESM otherwise

const snapshot = nodeTest.snapshot
let snapshotResolver = (dir, name) => [dir, `${name}.snapshot`] // default per Node.js docs
const resolveSnapshot = (f) => pathJoin(...snapshotResolver(dirname(f), basename(f)))
const readSnapshot = (f = baseFile) => (f ? readFileSync(resolveSnapshot(f), 'utf8') : null)
const setSnapshotSerializers = (list) => snapshot?.setDefaultSnapshotSerializers(list)
const setSnapshotResolver = (fn) => {
snapshotResolver = fn
snapshot?.setResolveSnapshotPath(resolveSnapshot)
}

/* eslint-disable unicorn/no-useless-spread */
module.exports = {
...{ assert, assertLoose },
...{ mock, describe, test, beforeEach, afterEach, before, after },
...{ builtinModules, syncBuiltinESMExports },
...{ utilFormat, isPromise, nodeVersion },
...{ baseFile, relativeRequire, isTopLevelESM },
...{ snapshot, readSnapshot, setSnapshotSerializers, setSnapshotResolver },
}
/* eslint-enable unicorn/no-useless-spread */
1 change: 1 addition & 0 deletions src/engine.select.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./engine.node.cjs')
2 changes: 1 addition & 1 deletion src/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Not using ./node.js yet
// Not using ./engine.js yet, might pass / embed already loaded config instead
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
Expand Down
2 changes: 1 addition & 1 deletion src/jest.fn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mock, assert } from './node.js'
import { mock, assert } from './engine.js'

const registry = new Set()
let callId = 0
Expand Down
4 changes: 2 additions & 2 deletions src/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
assert,
utilFormat,
isPromise,
} from './node.js'
} from './engine.js'
import { jestConfig } from './jest.config.js'
import { jestFunctionMocks } from './jest.fn.js'
import { jestModuleMocks } from './jest.mock.js'
Expand Down Expand Up @@ -190,4 +190,4 @@ const jest = {

export { jest, describe, test, test as it }
export { expect } from 'expect'
export { beforeEach, afterEach, before as beforeAll, after as afterAll } from './node.js'
export { beforeEach, afterEach, before as beforeAll, after as afterAll } from './engine.js'
2 changes: 1 addition & 1 deletion src/jest.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
isTopLevelESM,
builtinModules,
syncBuiltinESMExports,
} from './node.js'
} from './engine.js'
import { jestfn } from './jest.fn.js'
import { makeEsbuildMockable } from './dark.cjs'

Expand Down
2 changes: 1 addition & 1 deletion src/jest.snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setSnapshotSerializers,
readSnapshot,
relativeRequire,
} from './node.js'
} from './engine.js'
import { expect } from 'expect'
import { format, plugins as builtinPlugins } from 'pretty-format'
import { jestConfig } from './jest.config.js'
Expand Down
2 changes: 1 addition & 1 deletion src/jest.timers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mock, assert } from './node.js'
import { mock, assert } from './engine.js'
import { jestConfig } from './jest.config.js'
import { haveValidTimers, haveNoTimerInfiniteLoopBug } from './version.js'

Expand Down
30 changes: 0 additions & 30 deletions src/node.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/tape.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertLoose, test } from './node.js'
import { assert, assertLoose, test } from './engine.js'
import { createCallerLocationHook } from './dark.cjs'
import './version.js'

Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, nodeVersion } from './node.js'
import { assert, nodeVersion } from './engine.js'

const [major, minor, patch] = nodeVersion.split('.').map(Number)
assert(major !== 21, 'Node.js 21.x is deprecated!') // reached EOL, no reason to even test
Expand Down

0 comments on commit 8c0530e

Please sign in to comment.