Skip to content

Commit

Permalink
fix: revert converting test helpers to es modules (#5982)
Browse files Browse the repository at this point in the history
* Revert "fix: lint"

This reverts commit 050956d.

* Revert "fix: run mocha as a module"

This reverts commit 4dac25a.

* Revert "move to modules, but break mocha"

This reverts commit 220d7bb.
  • Loading branch information
BeksOmega committed Mar 9, 2022
1 parent 3740d51 commit 01d4597
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 97 deletions.
26 changes: 13 additions & 13 deletions tests/deps.mocha.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions tests/mocha/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"parserOptions": {
"sourceType": "module"
},
"env": {
"browser": true,
"mocha": true
Expand All @@ -14,8 +11,7 @@
"no-unused-vars": ["off"],
// Allow uncommented helper functions in tests.
"require-jsdoc": ["off"],
"prefer-rest-params": ["off"],
"no-invalid-this": ["off"]
"prefer-rest-params": ["off"]
},
"extends": "../../.eslintrc.json"
}
6 changes: 0 additions & 6 deletions tests/mocha/astnode_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('B


suite('ASTNode', function() {
console.log('1/a');
setup(function() {
sharedTestSetup.call(this);
Blockly.defineBlocksWithJsonArray([{
Expand Down Expand Up @@ -101,9 +100,7 @@ suite('ASTNode', function() {
});

suite('HelperFunctions', function() {
console.log('2');
test('findNextForInput_', function() {
console.log('3');
const input = this.blocks.statementInput1.inputList[0];
const input2 = this.blocks.statementInput1.inputList[1];
const connection = input.connection;
Expand Down Expand Up @@ -172,7 +169,6 @@ suite('ASTNode', function() {
});

suite('NavigationFunctions', function() {
console.log('b');
setup(function() {
Blockly.defineBlocksWithJsonArray([{
"type": "top_connection",
Expand Down Expand Up @@ -323,9 +319,7 @@ suite('ASTNode', function() {
workspaceTeardown.call(this, this.singleBlockWorkspace);
});

console.log('c');
test('fromPreviousToBlock', function() {
console.log('d');
const prevConnection = this.blocks.statementInput1.previousConnection;
const node = ASTNode.createConnectionNode(prevConnection);
const nextNode = node.next();
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
<block type="test_field_block"></block>
</xml>

<script type="module">
<script>
let runner = mocha.run(function(failures) {
var failureDiv = document.getElementById('failureCount');
failureDiv.setAttribute('tests_failed', failures);
Expand Down
23 changes: 15 additions & 8 deletions tests/mocha/test_helpers/block_definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
* SPDX-License-Identifier: Apache-2.0
*/

goog.declareModuleId('Blockly.test.helpers.blockDefinitions');
goog.module('Blockly.test.helpers.blockDefinitions');


export function defineStackBlock(name = 'stack_block') {
function defineStackBlock(name = 'stack_block') {
Blockly.defineBlocksWithJsonArray([{
"type": name,
"message0": "",
"previousStatement": null,
"nextStatement": null,
}]);
}
exports.defineStackBlock = defineStackBlock;

export function defineRowBlock(name = 'row_block') {
function defineRowBlock(name = 'row_block') {
Blockly.defineBlocksWithJsonArray([{
"type": name,
"message0": "%1",
Expand All @@ -29,8 +30,9 @@ export function defineRowBlock(name = 'row_block') {
"output": null,
}]);
}
exports.defineRowBlock = defineRowBlock;

export function defineStatementBlock(name = 'statement_block') {
function defineStatementBlock(name = 'statement_block') {
Blockly.defineBlocksWithJsonArray([{
"type": name,
"message0": "%1",
Expand All @@ -47,8 +49,9 @@ export function defineStatementBlock(name = 'statement_block') {
"helpUrl": "",
}]);
}
exports.defineStatementBlock = defineStatementBlock;

export function defineBasicBlockWithField(name = 'test_field_block') {
function defineBasicBlockWithField(name = 'test_field_block') {
Blockly.defineBlocksWithJsonArray([{
"type": name,
"message0": "%1",
Expand All @@ -61,8 +64,9 @@ export function defineBasicBlockWithField(name = 'test_field_block') {
"output": null,
}]);
}
exports.defineBasicBlockWithField = defineBasicBlockWithField;

export function defineMutatorBlocks() {
function defineMutatorBlocks() {
Blockly.defineBlocksWithJsonArray([
{
'type': 'xml_block',
Expand Down Expand Up @@ -154,8 +158,9 @@ export function defineMutatorBlocks() {
};
Blockly.Extensions.registerMutator('jso_mutator', jsoMutator);
}
exports.defineMutatorBlocks = defineMutatorBlocks;

export function createTestBlock() {
function createTestBlock() {
return {
'id': 'test',
'rendered': false,
Expand All @@ -169,10 +174,12 @@ export function createTestBlock() {
'updateVarName': Blockly.Block.prototype.updateVarName,
};
}
exports.createTestBlock = createTestBlock;

export function createRenderedBlock(workspaceSvg, type) {
function createRenderedBlock(workspaceSvg, type) {
const block = workspaceSvg.newBlock(type);
block.initSvg();
block.render();
return block;
}
exports.createRenderedBlock = createRenderedBlock;
11 changes: 7 additions & 4 deletions tests/mocha/test_helpers/code_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

goog.declareModuleId('Blockly.test.helpers.codeGeneration');
goog.module('Blockly.test.helpers.codeGeneration');

const {runTestSuites} = goog.require('Blockly.test.helpers.common');

Expand All @@ -15,7 +15,7 @@ const {runTestSuites} = goog.require('Blockly.test.helpers.common');
* @implements {TestCase}
* @record
*/
export class CodeGenerationTestCase {
class CodeGenerationTestCase {
/**
* Class for a code generation test case.
*/
Expand Down Expand Up @@ -43,13 +43,14 @@ export class CodeGenerationTestCase {
*/
createBlock(workspace) {}
}
exports.CodeGenerationTestCase = CodeGenerationTestCase;

/**
* Code generation test suite.
* @extends {TestSuite<CodeGenerationTestCase, CodeGenerationTestSuite>}
* @record
*/
export class CodeGenerationTestSuite {
class CodeGenerationTestSuite {
/**
* Class for a code generation test suite.
*/
Expand All @@ -60,6 +61,7 @@ export class CodeGenerationTestSuite {
this.generator;
}
}
exports.CodeGenerationTestSuite = CodeGenerationTestSuite;

/**
* Returns mocha test callback for code generation based on provided
Expand Down Expand Up @@ -100,7 +102,7 @@ const createCodeGenerationTestFn_ = (generator) => {
* Runs blockToCode test suites.
* @param {!Array<!CodeGenerationTestSuite>} testSuites The test suites to run.
*/
export const runCodeGenerationTestSuites = (testSuites) => {
const runCodeGenerationTestSuites = (testSuites) => {
/**
* Creates function used to generate mocha test callback.
* @param {!CodeGenerationTestSuite} suiteInfo The test suite information.
Expand All @@ -113,3 +115,4 @@ export const runCodeGenerationTestSuites = (testSuites) => {

runTestSuites(testSuites, createTestFn);
};
exports.runCodeGenerationTestSuites = runCodeGenerationTestSuites;
14 changes: 9 additions & 5 deletions tests/mocha/test_helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

goog.declareModuleId('Blockly.test.helpers.common');
goog.module('Blockly.test.helpers.common');

/**
* Test case configuration.
* @record
*/
export class TestCase {
class TestCase {
/**
* Class for a test case configuration.
*/
Expand All @@ -31,14 +31,15 @@ export class TestCase {
this.only;
}
}
exports.TestCase = TestCase;

/**
* Test suite configuration.
* @record
* @template {TestCase} T
* @template {TestSuite} U
*/
export class TestSuite {
class TestSuite {
/**
* Class for a test suite configuration.
*/
Expand Down Expand Up @@ -67,6 +68,7 @@ export class TestSuite {
this.only;
}
}
exports.TestSuite = TestSuite;

/**
* Runs provided test cases.
Expand All @@ -75,13 +77,14 @@ export class TestSuite {
* @param {function(T):Function} createTestCallback Creates test
* callback using given test case.
*/
export function runTestCases(testCases, createTestCallback) {
function runTestCases(testCases, createTestCallback) {
testCases.forEach((testCase) => {
let testCall = (testCase.skip ? test.skip : test);
testCall = (testCase.only ? test.only : testCall);
testCall(testCase.title, createTestCallback(testCase));
});
}
exports.runTestCases = runTestCases;

/**
* Runs provided test suite.
Expand All @@ -92,7 +95,7 @@ export function runTestCases(testCases, createTestCallback) {
* } createTestCaseCallback Creates test case callback using given test
* suite.
*/
export function runTestSuites(testSuites, createTestCaseCallback) {
function runTestSuites(testSuites, createTestCaseCallback) {
testSuites.forEach((testSuite) => {
let suiteCall = (testSuite.skip ? suite.skip : suite);
suiteCall = (testSuite.only ? suite.only : suiteCall);
Expand All @@ -106,3 +109,4 @@ export function runTestSuites(testSuites, createTestCaseCallback) {
});
});
}
exports.runTestSuites = runTestSuites;
17 changes: 11 additions & 6 deletions tests/mocha/test_helpers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

goog.declareModuleId('Blockly.test.helpers.events');
goog.module('Blockly.test.helpers.events');


/**
Expand All @@ -13,9 +13,10 @@ goog.declareModuleId('Blockly.test.helpers.events');
* calls on.
* @return {!SinonSpy} The created spy.
*/
export function createFireChangeListenerSpy(workspace) {
function createFireChangeListenerSpy(workspace) {
return sinon.spy(workspace, 'fireChangeListener');
}
exports.createFireChangeListenerSpy = createFireChangeListenerSpy;

/**
* Asserts whether the given xml property has the expected property.
Expand Down Expand Up @@ -74,7 +75,7 @@ function isXmlProperty_(key) {
* @param {boolean=} [isUiEvent=false] Whether the event is a UI event.
* @param {string=} message Optional message to prepend assert messages.
*/
export function assertEventEquals(event, expectedType,
function assertEventEquals(event, expectedType,
expectedWorkspaceId, expectedBlockId, expectedProperties, isUiEvent = false, message) {
let prependMessage = message ? message + ' ' : '';
prependMessage += 'Event fired ';
Expand Down Expand Up @@ -106,6 +107,7 @@ export function assertEventEquals(event, expectedType,
chai.assert.isFalse(event.isUiEvent);
}
}
exports.assertEventEquals = assertEventEquals;

/**
* Asserts that an event with the given values was fired.
Expand All @@ -117,7 +119,7 @@ export function assertEventEquals(event, expectedType,
* @param {string} expectedWorkspaceId Expected workspace id of event fired.
* @param {?string=} expectedBlockId Expected block id of event fired.
*/
export function assertEventFired(spy, instanceType, expectedProperties,
function assertEventFired(spy, instanceType, expectedProperties,
expectedWorkspaceId, expectedBlockId) {
expectedProperties = Object.assign({
workspaceId: expectedWorkspaceId,
Expand All @@ -127,6 +129,7 @@ export function assertEventFired(spy, instanceType, expectedProperties,
sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties));
sinon.assert.calledWith(spy, expectedEvent);
}
exports.assertEventFired = assertEventFired;

/**
* Asserts that an event with the given values was not fired.
Expand All @@ -138,7 +141,7 @@ export function assertEventFired(spy, instanceType, expectedProperties,
* @param {string=} expectedWorkspaceId Expected workspace id of event fired.
* @param {?string=} expectedBlockId Expected block id of event fired.
*/
export function assertEventNotFired(spy, instanceType, expectedProperties,
function assertEventNotFired(spy, instanceType, expectedProperties,
expectedWorkspaceId, expectedBlockId) {
expectedProperties.type = instanceType.prototype.type;
if (expectedWorkspaceId !== undefined) {
Expand All @@ -151,6 +154,7 @@ export function assertEventNotFired(spy, instanceType, expectedProperties,
sinon.match.instanceOf(instanceType).and(sinon.match(expectedProperties));
sinon.assert.neverCalledWith(spy, expectedEvent);
}
exports.assertEventNotFired = assertEventNotFired;

/**
* Filters out xml properties from given object based on key.
Expand Down Expand Up @@ -185,7 +189,7 @@ function splitByXmlProperties_(properties) {
* @param {string} expectedWorkspaceId Expected workspace id of event fired.
* @param {?string=} expectedBlockId Expected block id of event fired.
*/
export function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties,
function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties,
expectedWorkspaceId, expectedBlockId) {
const nthCall = spy.getCall(n);
const splitProperties = splitByXmlProperties_(expectedProperties);
Expand All @@ -197,3 +201,4 @@ export function assertNthCallEventArgEquals(spy, n, instanceType, expectedProper
const eventArg = nthCall.firstArg;
assertXmlProperties_(eventArg, xmlProperties);
}
exports.assertNthCallEventArgEquals = assertNthCallEventArgEquals;
Loading

0 comments on commit 01d4597

Please sign in to comment.