Tests: Emulate form blocks experiments in integration tests#73705
Tests: Emulate form blocks experiments in integration tests#73705
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
I considered adding some sort of test helper here, like a |
|
Size Change: 0 B Total Size: 2.58 MB ℹ️ View Unchanged
|
jsnajdr
left a comment
There was a problem hiding this comment.
Yes, this is a good idea, turns out we don't need the granular block-library exports in this test.
| formSubmissionNotification, | ||
| ] ); | ||
| // in on the experimental settings page. | ||
| originalEnableFormBlocks = window.__experimentalEnableFormBlocks; |
There was a problem hiding this comment.
I'm a bit surprised that you have to restore the original window state. The window object should be synthetic, created anew for each test. By the jest-environment-jsdom package.
There was a problem hiding this comment.
I didn't actually test to verify this, but I did do some searching yesterday that concluded that that the window environment is not reset automatically between tests. I'll try to pull up some of those resources to confirm or counter that claim.
There was a problem hiding this comment.
Okay, after looking again, I believe the behavior is that the window is reset between test files, and shared within a test file (jestjs/jest#1224 (comment)). Since we're using this as an beforeAll / afterAll, I suppose it should be fine to set without restoring.
There was a problem hiding this comment.
There are different policies for modules, mocks and the environment (window etc.). Mocks and modules are shared by the entire test file, but the environment should be new for each individual test.
The expected behavior for Jest + JSDOM should be that individual test files have an isolated window global See: https://github.com/WordPress/gutenberg/pull/73705/files#r2585370933
What?
Updates "full content" integration tests to avoid explicitly registering form blocks, and instead sets up the environment that they would be registered as if the requisite experiment is enabled.
Related to #72978
Why?
As discussed in #72978, the import subpaths for
@wordpress/block-libraryas used in these tests are not valid exports for the package, and only work because Jest presumably doesn't actually respect the exports API. This means it's likely to break in the future.While we could update these imports to import from
build-module, that means that the tests would only pass if the package were built ahead of time, which is not a requirement we should want to impose (see related code).How?
Updates test to assign the
windowglobal that's evaluated when considering to register these blocks (source).Testing Instructions
npm run test:unit test/integration/full-content/full-content.test.js