@exodus/test 1.0.0-rc.13 → 1.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/test",
3
- "version": "1.0.0-rc.13",
3
+ "version": "1.0.0-rc.14",
4
4
  "author": "Exodus Movement, Inc.",
5
5
  "description": "A test suite runner",
6
6
  "homepage": "https://github.com/ExodusMovement/test",
@@ -36,6 +36,7 @@
36
36
  "src/dark.cjs",
37
37
  "src/jest.js",
38
38
  "src/jest.config.js",
39
+ "src/jest.environment.js",
39
40
  "src/jest.fn.js",
40
41
  "src/jest.mock.js",
41
42
  "src/jest.snapshot.js",
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict'
2
2
  import { readFile } from 'node:fs/promises'
3
3
  import path from 'node:path'
4
4
  import { createRequire } from 'node:module'
5
+ import { specialEnvironments } from './jest.environment.js'
5
6
 
6
7
  const files = process.argv.slice(1)
7
8
  const baseDir = files.length === 1 ? path.dirname(path.resolve(files[0])) : undefined
@@ -69,32 +70,10 @@ const normalizeJestConfig = (config) => ({
69
70
  },
70
71
  })
71
72
 
72
- const specialEnvs = {
73
- __proto__: null,
74
-
75
- 'setup-polly-jest/jest-environment-node': (require) => {
76
- const { Polly } = require('@pollyjs/core')
77
- const { JestPollyGlobals } = require('setup-polly-jest/lib/common')
78
- const pollyGlobals = new JestPollyGlobals(globalThis)
79
- pollyGlobals.isJestPollyEnvironment = true
80
-
81
- beforeEach((t) => {
82
- if (!pollyGlobals.isPollyActive) return
83
- pollyGlobals.pollyContext.polly = new Polly(t.fullName, pollyGlobals.pollyContext.options)
84
- })
85
-
86
- afterEach(async () => {
87
- if (!pollyGlobals.pollyContext.polly) return
88
- await pollyGlobals.pollyContext.polly.stop()
89
- pollyGlobals.pollyContext.polly = null
90
- })
91
- },
92
- }
93
-
94
73
  function verifyJestConfig(c) {
95
74
  assert(!configUsed, 'Can not apply new config as the current one was already used')
96
75
 
97
- if (!Object.hasOwn(specialEnvs, c.testEnvironment)) {
76
+ if (!Object.hasOwn(specialEnvironments, c.testEnvironment)) {
98
77
  assert.equal(c.testEnvironment, 'node', 'Only "node" testEnvironment is supported')
99
78
  }
100
79
 
@@ -147,7 +126,10 @@ export async function installJestEnvironment(jestGlobals) {
147
126
 
148
127
  const require = createRequire(config.rootDir)
149
128
 
150
- if (Object.hasOwn(specialEnvs, c.testEnvironment)) specialEnvs[c.testEnvironment](require)
129
+ if (Object.hasOwn(specialEnvironments, c.testEnvironment)) {
130
+ specialEnvironments[c.testEnvironment](require, jestGlobals, c.testEnvironmentOptions)
131
+ }
132
+
151
133
  for (const file of c.setupFiles || []) require(file)
152
134
  for (const file of c.setupFilesAfterEnv || []) require(file)
153
135
 
@@ -0,0 +1,93 @@
1
+ // Shoult not import src/ stuff here, as this goes into runner too (to check config)
2
+
3
+ function getTestNamePath(t, { require } = {}) {
4
+ // No implementation in Node.js yet, will have to PR
5
+ if (t.fullName) return t.fullName.split(' > ')
6
+
7
+ // We are on Node.js < 22.3.0 where even t.fullName doesn't exist yet, polyfill
8
+ const namePath = Symbol('namePath')
9
+ try {
10
+ if (t[namePath]) return t[namePath]
11
+
12
+ // Sigh, ok, whatever
13
+ const { Test } = require('node:internal/test_runner/test')
14
+
15
+ const usePathName = Symbol('usePathName')
16
+ const restoreName = Symbol('restoreName')
17
+ Test.prototype.getNamePath = function () {
18
+ if (this === this.root) return []
19
+ return [...(this.parent?.getNamePath() || []), this.name]
20
+ }
21
+
22
+ const diagnostic = Test.prototype.diagnostic
23
+ Test.prototype.diagnostic = function (...args) {
24
+ if (args[0] === usePathName) {
25
+ this[restoreName] = this.name
26
+ this.name = this.getNamePath()
27
+ return
28
+ }
29
+
30
+ if (args[0] === restoreName) {
31
+ this.name = this[restoreName]
32
+ delete this[restoreName]
33
+ return
34
+ }
35
+
36
+ return diagnostic.apply(this, args)
37
+ }
38
+
39
+ const TestContextProto = Object.getPrototypeOf(t)
40
+ Object.defineProperty(TestContextProto, namePath, {
41
+ get() {
42
+ this.diagnostic(usePathName)
43
+ const result = this.name
44
+ this.diagnostic(restoreName)
45
+ return result
46
+ },
47
+ })
48
+
49
+ return t[namePath]
50
+ } catch {}
51
+
52
+ return [t.name] // last resort
53
+ }
54
+
55
+ export const specialEnvironments = {
56
+ __proto__: null,
57
+
58
+ // Reproduces setup-polly-jest/jest-environment-node ad hacks into 'setup-polly-jest'.pollyJest
59
+ 'setup-polly-jest/jest-environment-node': (require, jestGlobals) => {
60
+ const { Polly } = require('@pollyjs/core')
61
+ const pollyJest = require('setup-polly-jest')
62
+ const { JestPollyGlobals, createPollyContextAccessor } = require('setup-polly-jest/lib/common')
63
+ const pollyGlobals = new JestPollyGlobals(globalThis)
64
+ pollyGlobals.isJestPollyEnvironment = true
65
+ pollyJest.setupPolly = (options) => {
66
+ if (!pollyGlobals.isJestPollyEnvironment) return
67
+
68
+ jestGlobals.beforeAll(() => {
69
+ pollyGlobals.isPollyActive = true
70
+ pollyGlobals.pollyContext.options = options
71
+ })
72
+
73
+ jestGlobals.afterAll(() => {
74
+ pollyGlobals.isPollyActive = false
75
+ pollyGlobals.pollyContext.options = null
76
+ })
77
+
78
+ return createPollyContextAccessor(pollyGlobals)
79
+ }
80
+
81
+ jestGlobals.beforeEach((t) => {
82
+ if (!pollyGlobals.isPollyActive) return
83
+ const name = getTestNamePath(t, { require }).join('/')
84
+ pollyGlobals.pollyContext.polly = new Polly(name, pollyGlobals.pollyContext.options)
85
+ })
86
+
87
+ jestGlobals.afterEach(async () => {
88
+ if (!pollyGlobals.pollyContext.polly) return
89
+ await pollyGlobals.pollyContext.polly.stop()
90
+ pollyGlobals.pollyContext.polly = null
91
+ })
92
+ },
93
+ }