Workaround for the issue with chai v5 and ES modules

I couldn't make it work with the Babel loader. There is a possible
solution using ts-node as a loader. I didn't want to install another
package, so I chose a different workaround.
mocha-env.mjs is processed as a module thanks to the suffix, and it
sets chai. expect globally, and then I updated tests to use the global expect.

Please see https://github.com/chaijs/chai/issues/1568
[MAILPOET-6008]
This commit is contained in:
Rostislav Wolny
2024-06-13 12:53:10 +02:00
committed by Veljko V
parent 73434e1ef7
commit 1d32461b3d
17 changed files with 16 additions and 16 deletions

View File

@@ -388,7 +388,7 @@ class RoboFile extends \Robo\Tasks {
}
public function testJavascript($xmlOutputFile = null) {
$command = './node_modules/.bin/mocha --recursive --require tests/javascript/babel-register.js tests/javascript --extension spec.js --extension spec.ts';
$command = './node_modules/.bin/mocha --recursive --require tests/javascript/mocha-env.mjs tests/javascript --extension spec.ts';
if (!empty($xmlOutputFile)) {
$command .= sprintf(

View File

@@ -9,7 +9,7 @@
"scss": "sass assets/css/src/:assets/dist/css/ --style compressed",
"stylelint": "stylelint --fix",
"stylelint-check": "stylelint",
"test": "env NODE_PATH=$NODE_PATH:./assets/js/src mocha --recursive --require tests/javascript/babel-register.js tests/javascript --extension spec.js --extension spec.ts",
"test": "env NODE_PATH=$NODE_PATH:./assets/js/src mocha --recursive --require tests/javascript/mocha-env.mjs tests/javascript --extension spec.ts",
"check-types": "NODE_OPTIONS=--max_old_space_size=2048 tsc --noEmit",
"storybook": "start-storybook -s ./ -p 8083",
"build-storybook": "build-storybook",

View File

@@ -1 +0,0 @@
require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] });

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { partial, isEmpty, isUndefined } from 'lodash';
import { blocksToFormBodyFactory } from '../../../../assets/js/src/form-editor/store/blocks-to-form-body';
import {

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { partial, isEmpty, isUndefined } from 'lodash';
import { formBodyToBlocksFactory } from '../../../../assets/js/src/form-editor/store/form-body-to-blocks.jsx';

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { isEmpty } from 'lodash';
import { validateForm as validate } from '../../../../assets/js/src/form-editor/store/form-validator.jsx';

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { mapFormDataAfterLoading as map } from '../../../../assets/js/src/form-editor/store/map-form-data-after-loading.jsx';
const data = {

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { mapFormDataBeforeSaving as map } from '../../../../assets/js/src/form-editor/store/map-form-data-before-saving.jsx';
const data = {

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { createCustomFieldStartedFactory } from '../../../../../assets/js/src/form-editor/store/reducers/create-custom-field-started';
import { CustomFieldStartedAction } from '../../../../../assets/js/src/form-editor/store/actions-types';
import { createCustomFieldMock, createStateMock } from '../mocks/partial-mocks';

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import {
createHistoryRecord,
historyRedo,

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { identity } from 'lodash';
import { saveFormStartedFactory } from '../../../../../assets/js/src/form-editor/store/reducers/save-form-started';
import { createStateMock } from '../mocks/partial-mocks';

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { toggleSidebarPanel as reducer } from '../../../../../assets/js/src/form-editor/store/reducers/toggle-sidebar-panel';
import { ToggleSidebarPanelAction } from '../../../../../assets/js/src/form-editor/store/actions-types';
import { createStateMock } from '../mocks/partial-mocks';

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import { selectors } from '../../../../assets/js/src/form-editor/store/selectors';
import {
createBlocksMock,

View File

@@ -1,5 +1,3 @@
import { expect } from 'chai';
import { asNum } from '../../../../assets/js/src/form-editor/store/server-value-as-num';
describe('Server value as num', () => {

5
mailpoet/tests/javascript/global.d.ts vendored Normal file
View File

@@ -0,0 +1,5 @@
import type * as chai from 'chai';
declare global {
const expect: typeof chai.expect;
}

View File

@@ -0,0 +1,9 @@
import * as chai from 'chai';
import register from '@babel/register';
// Register Babel to transpile our tests
register({ extensions: ['.js', '.jsx', '.ts', '.tsx'] });
// Set up chai.expect globally due issues with ES modules
// See https://github.com/chaijs/chai/issues/1568
global.expect = chai.expect;

View File

@@ -1,4 +1,3 @@
import { expect } from 'chai';
import moment, { Moment } from 'moment';
import { MailPoetDate } from '../../../assets/js/src/date';