Skip to content

Commit

Permalink
build: Enable ESM support
Browse files Browse the repository at this point in the history
Configure TypeScript and Jest to enable import of ECMAScript modules.
This enables to un-mock `react-markdown` and test functionality
surrounding its usage in the source.
  • Loading branch information
dcalhoun committed Jun 26, 2024
1 parent 851c054 commit 1264f3a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import '@testing-library/jest-dom';
// used by `@php-wasm/universal` and it's not available in the Jest environment.
// eslint-disable-next-line import/no-unresolved
import 'web-streams-polyfill/polyfill';
import { jest } from '@jest/globals';
import nock from 'nock';
import ResizeObserverPolyfill from 'resize-observer-polyfill';

if ( typeof window !== 'undefined' ) {
// The ipcListener global is usually defined in preload.ts
window.ipcListener = { subscribe: jest.fn() };
window.ipcListener = {
subscribe: jest.fn( () => jest.fn() ),
};

// Mock `matchMedia` as it's not implemented in JSDOM
// Reference: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Expand Down Expand Up @@ -35,7 +39,7 @@ jest.mock( './src/hooks/use-offline', () => ( {
} ) );

jest.mock( 'strip-ansi', () => ( {
default: jest.fn().mockImplementation( ( str: string ) => str ),
default: jest.fn( ( str: string ) => str ),
} ) );

global.ResizeObserver = require( 'resize-observer-polyfill' );
global.ResizeObserver = ResizeObserverPolyfill;
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module.exports = {
roots: [ '<rootDir>/src' ],
preset: 'ts-jest/presets/default-esm',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: {
exclude: [ '**/vendor/wp-now/**/*' ],
},
useESM: true,
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"publish": "electron-forge publish",
"lint": "eslint --ext .ts,.tsx,.js,.jsx,.mjs .",
"format": "prettier . --write",
"test": "jest",
"test:watch": "jest --watch",
"test": "node --experimental-vm-modules --trace-warnings node_modules/jest/bin/jest.js",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
"e2e": "npx playwright install && npx playwright test",
"make-pot": "wp-babel-makepot \"./src/**/*.{js,jsx,ts,tsx}\" --ignore \"**/*.d.ts\" --base \"./src\" --dir \"./out/pots\" --output \"./out/pots/bundle-strings.pot\""
},
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES6",
"allowJs": true,
"module": "commonjs",
"module": "ESNext",
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 1264f3a

Please sign in to comment.