Repository navigation
Expand file tree
/
Copy pathjest.config.js
More file actions
102 lines (99 loc) · 4.28 KB
/
Copy pathjest.config.js
File metadata and controls
102 lines (99 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* External dependencies
*/
const path = require( 'path' );
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/jest-unit.config.js' );
// At runtime WordPress provides the framework (`@wordpress/sync`) and Yjs as
// `wp.sync`; under Jest there is no such global, so we resolve the framework
// from the pinned Gutenberg subtree in `./gutenberg` — the same copy wp-env
// and e2e run against — and pin `yjs` itself to the framework's SINGLE copy so
// the plugin and the framework share one Yjs instance
// (https://github.com/yjs/yjs/issues/438). y-protocols and lib0 are left to
// normal resolution so their package `exports` pick the CommonJS build Jest
// can load; being stateless, a duplicate of them is harmless as long as they
// bind to the one shared `yjs`. Requires the subtree to be installed and
// built (see Setup in AGENTS.md). Set WP_SYNC_FRAMEWORK_ROOT to test against
// a live framework checkout instead (co-development), mirroring the PHP
// bootstrap's WP_SYNC_FRAMEWORK_PLUGIN env override.
const FRAMEWORK_ROOT =
process.env.WP_SYNC_FRAMEWORK_ROOT ||
path.resolve( __dirname, 'gutenberg' );
const FRAMEWORK_MODULES = path.join( FRAMEWORK_ROOT, 'node_modules' );
const SYNC_SRC = path.join( FRAMEWORK_ROOT, 'packages/sync/src' );
module.exports = {
...defaultConfig,
// Discover ONLY this plugin's own tests. The pinned Gutenberg subtree in
// `gutenberg/` carries thousands of the monorepo's own test files; without
// this restriction Jest would recurse into it and run (and fail) them.
roots: [ '<rootDir>/src', '<rootDir>/tests' ],
// The frozen cross-language vector replay locates its fixture through
// `import.meta.url`; Jest runs CommonJS, so transform it away with the same
// plugin the framework's build uses. Overriding `transform` (rather than
// adding a project babel config) keeps this scoped to Jest and leaves the
// webpack build's babel untouched.
transform: {
'\\.(?:mjs|[jt]sx?)$': [
require.resolve( 'babel-jest' ),
{
presets: [
require.resolve( '@wordpress/babel-preset-default' ),
],
plugins: [
require.resolve( 'babel-plugin-transform-import-meta' ),
],
},
],
},
moduleNameMapper: {
...( defaultConfig.moduleNameMapper || {} ),
// The contract tests import core-data source. Match WordPress's single
// API-fetch instance and use the local CJS uuid with this Jest runner.
'^uuid$': require.resolve( 'uuid' ),
'^@wordpress/api-fetch$': require.resolve( '@wordpress/api-fetch' ),
'^@wordpress/sync$': SYNC_SRC,
// Stateless grammar parser used by the intent-log manager to read
// persisted syncIds out of loaded record content; resolved from the
// subtree so no plugin-local install is needed (at runtime it is the
// wp-block-serialization-default-parser script).
'^@wordpress/block-serialization-default-parser$': path.join(
FRAMEWORK_MODULES,
'@wordpress/block-serialization-default-parser'
),
'^yjs$': path.join( FRAMEWORK_MODULES, 'yjs' ),
// The private-API lock lives in a module-scoped WeakMap; the framework
// locks and the plugin unlocks, so both MUST share one copy of
// @wordpress/private-apis (at runtime this is the single wp.privateApis).
'^@wordpress/private-apis$': path.join(
FRAMEWORK_MODULES,
'@wordpress/private-apis'
),
// The framework reads provider creators through the `sync.providers`
// hook filter that the tests write to; both sides must share one hooks
// registry (the single wp.hooks at runtime).
'^@wordpress/hooks$': path.join(
FRAMEWORK_MODULES,
'@wordpress/hooks'
),
// The slow-awareness store is a @wordpress/data store; resolve the
// package from the subtree (the single wp.data at runtime).
'^@wordpress/data$': path.join( FRAMEWORK_MODULES, '@wordpress/data' ),
// The de-rtc doc bridge serializes/parses through the editor's block
// library (the single wp.blocks at runtime); resolve it from the
// subtree. Tests mock it — block registration is editor state.
'^@wordpress/blocks$': path.join(
FRAMEWORK_MODULES,
'@wordpress/blocks'
),
},
setupFiles: [
...( defaultConfig.setupFiles || [] ),
path.join( __dirname, 'tests/js/jest-setup.js' ),
],
setupFilesAfterEnv: [
...( defaultConfig.setupFilesAfterEnv || [] ),
path.join( __dirname, 'tests/js/jest-setup-after-env.js' ),
],
};