I’ve been spending some of my time recently making sure WordPress Playground works in Windows. While working on Windows fixes I learned a lot about making sure Javascript code works POSIX and Windows when running it in Node.js.
One thing that I learned is that you shouldn’t use URL to construct paths.
If you construct a path using new URL(import.meta.url) in Node.js and use pathname to to obtain the path, it will always have a leading slash in front of the path. This is ok for POSIX but in Windows it results in invalid paths like /C:/Users/.
What to use instead of URL
You can use fileURLToPath from the URL package, it will correctly construct both POSIX and Windows paths from file:/// paths.
Polyfilling __dirname and __filename in Node 20+
In Node 20 and above you can use import.meta.filename and import.meta.dirname to polyfill __dirname and __filename.
Example
import { fileURLToPath } from 'url';
import path from 'path';
const windowsImportMetaUrl = "file:///C:/Users/me/Downloads/index.js";
console.log('windowsImportMetaUrl', windowsImportMetaUrl);
let __dirname = new URL('.', windowsImportMetaUrl).pathname;
let __filename = new URL(windowsImportMetaUrl).pathname;
console.log('__filename', __filename);
console.log('__dirname', __dirname);
__filename = fileURLToPath(
windowsImportMetaUrl,
{
windows: true,
}
);
__dirname = path.win32.dirname(__filename);
console.log('__filename', __filename);
console.log('__dirname', __dirname);
__filename = import.meta.filename;
__dirname = import.meta.dirname;
console.log('__filename', __filename);
console.log('__dirname', __dirname);
The original approach using Bash was a bit complicated, but after Playground created a way to start the Playground CLI from JavaScript by calling runCLI, using Playground for automated tests became easy.
In this post I will demonstrate how to get started with using the Playground CLI for testing WordPress projects.
Before we can start building tests, we need to get familiar with the Playground CLI.
The Playground CLI enables us creates a local server for our WordPress projects.
Mount a project into Playground
To start using the CLI make sure you have at least Node 20 installed and after that run npx @wp-playground/cli server --mount=.:/wordpress/wp-content/plugins/playground-testing-demo to start a server and mount your current directory as a WordPress plugin.
Now we can start a server and activate the plugin during boot using npx @wp-playground/cli server --mount=.:/wordpress/wp-content/plugins/ --blueprint=./blueprint.json
Notes
You can replace . with a path to your project and /wordpress/wp-content/plugins/ with a path where you would like your project to be loaded.
/wordpress/ is the site root folder, similar to /var/www/ in some other severs.
If you are mounting an entire WordPress site use a combination of --mountBeforeInstall and --skipWordPressSetup.
End-to-end tests
In the sample plugin you can run end-to-end tests using npm run test:e2e.
For the sample plugin we want to test the form and confirm that the data is persisted after a page reload.
Now we can run out integration test and check if the API returns a valid response.
In the test we first obtain the nonce, so that we can send it in the request. Because we use fetchCookie, the cookie that was sent in the nonce response will now be sent back together with the request.