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:
committed by
Veljko V
parent
73434e1ef7
commit
1d32461b3d
@@ -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(
|
||||
|
@@ -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",
|
||||
|
@@ -1 +0,0 @@
|
||||
require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] });
|
@@ -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 {
|
||||
|
@@ -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';
|
||||
|
||||
|
@@ -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';
|
||||
|
||||
|
@@ -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 = {
|
||||
|
@@ -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 = {
|
||||
|
@@ -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';
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai';
|
||||
import {
|
||||
createHistoryRecord,
|
||||
historyRedo,
|
||||
|
@@ -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';
|
||||
|
@@ -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';
|
||||
|
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai';
|
||||
import { selectors } from '../../../../assets/js/src/form-editor/store/selectors';
|
||||
import {
|
||||
createBlocksMock,
|
||||
|
@@ -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
5
mailpoet/tests/javascript/global.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import type * as chai from 'chai';
|
||||
|
||||
declare global {
|
||||
const expect: typeof chai.expect;
|
||||
}
|
9
mailpoet/tests/javascript/mocha-env.mjs
Normal file
9
mailpoet/tests/javascript/mocha-env.mjs
Normal 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;
|
@@ -1,4 +1,3 @@
|
||||
import { expect } from 'chai';
|
||||
import moment, { Moment } from 'moment';
|
||||
import { MailPoetDate } from '../../../assets/js/src/date';
|
||||
|
||||
|
Reference in New Issue
Block a user