Files
blockly/tests/mocha/test_helpers/warnings.js
dependabot[bot] bfb5b1dd49 chore(deps): Bump chai from 4.3.10 to 5.1.1 (#8092)
* chore(deps): Bump chai from 4.3.10 to 5.1.1

  Bumps [chai](https://github.com/chaijs/chai) from 4.3.10 to 5.1.1.
  - [Release notes](https://github.com/chaijs/chai/releases)
  - [Changelog](https://github.com/chaijs/chai/blob/main/History.md)
  - [Commits](https://github.com/chaijs/chai/compare/v4.3.10...v5.1.1)

  ---
  updated-dependencies:
  - dependency-name: chai
    dependency-type: direct:development
    update-type: version-update:semver-major
  ...

  Signed-off-by: dependabot[bot] <support@github.com>

* fix(tests): Migrate all usage of chai to ESM (#8216)

* fix(tests): Migrate node tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by that package dropping suppport for CJS in chai v5.0.0.

* fix(tests): Have mocha tests directly import chai

  Previously they relied on obtaining it from the global scope, but it's
  better if imports are explicit.

* fix(tests): Remove broken load of chai as script

  Chai v5.0.0 no longer supports being loaded as a script, so this did
  nothing but emit an syntax error message on the console.

* fix(tests): Migrate browser tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by chai no longer supporting CJS.

* chore(tests): format

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2024-06-17 16:48:21 +01:00

84 lines
2.4 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../../node_modules/chai/chai.js';
/**
* Captures the strings sent to console.warn() when calling a function.
* Copies from core.
* @param {Function} innerFunc The function where warnings may called.
* @return {Array<string>} The warning messages (only the first arguments).
*/
export function captureWarnings(innerFunc) {
const msgs = [];
const nativeConsoleWarn = console.warn;
try {
console.warn = function (msg) {
msgs.push(msg);
};
innerFunc();
} finally {
console.warn = nativeConsoleWarn;
}
return msgs;
}
/**
* Asserts that the given function logs the provided warning messages.
* @param {function()} innerFunc The function to call.
* @param {Array<!RegExp>|!RegExp} messages A list of regex for the expected
* messages (in the expected order).
*/
export function assertWarnings(innerFunc, messages) {
if (!Array.isArray(messages)) {
messages = [messages];
}
const warnings = captureWarnings(innerFunc);
assert.lengthOf(warnings, messages.length);
messages.forEach((message, i) => {
assert.match(warnings[i], message);
});
}
/**
* Asserts that the given function logs no warning messages.
* @param {function()} innerFunc The function to call.
*/
export function assertNoWarnings(innerFunc) {
assertWarnings(innerFunc, []);
}
/**
* Stubs Blockly.utils.deprecation.warn call.
* @return {!SinonStub} The created stub.
*/
export function createDeprecationWarningStub() {
return sinon.stub(Blockly.utils.deprecation, 'warn');
}
/**
* Asserts whether the given deprecation warning stub or call was called with
* the expected functionName.
* @param {!SinonSpy|!SinonSpyCall} spyOrSpyCall The spy or spy call to use.
* @param {string} functionName The function name to check that the given spy or
* spy call was called with.
*/
export function assertDeprecationWarningCall(spyOrSpyCall, functionName) {
sinon.assert.calledWith(spyOrSpyCall, functionName);
}
/**
* Asserts that there was a single deprecation warning call with the given
* functionName passed.
* @param {!SinonSpy} spy The spy to use.
* @param {string} functionName The function name to check that the given spy
* was called with.
*/
export function assertSingleDeprecationWarningCall(spy, functionName) {
sinon.assert.calledOnce(spy);
assertDeprecationWarningCall(spy.getCall(0), functionName);
}