diff --git a/package.json b/package.json index 33b1c04b2a1df3..93a373ce24bc9c 100644 --- a/package.json +++ b/package.json @@ -214,6 +214,7 @@ "packages/*", "docs", "docs/packages/*", - "framer" + "framer", + "test" ] } diff --git a/test/package.json b/test/package.json new file mode 100644 index 00000000000000..75960b96fbe8d6 --- /dev/null +++ b/test/package.json @@ -0,0 +1,8 @@ +{ + "private": true, + "name": "test", + "version": "0.0.1", + "scripts": { + "typescript": "tsc -p tsconfig.json" + } +} diff --git a/test/tsconfig.json b/test/tsconfig.json index 0aafdb7fe8ffa4..fb8f010733462e 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,4 +1,3 @@ -/* Only used to enable the language server */ { "extends": "../tsconfig.json", "include": ["regressions/**/*", "utils/**/*"], diff --git a/test/utils/mochaHooks.js b/test/utils/mochaHooks.js index 5f7b912e4a548b..0ae00f0aa1d2ac 100644 --- a/test/utils/mochaHooks.js +++ b/test/utils/mochaHooks.js @@ -1,22 +1,58 @@ +// @ts-check const formatUtil = require('format-util'); +/** + * @typedef {(this: import('mocha').Context) => void} MochaHook + * + * @typedef {object} MochaHooks + * @property {MochaHook[]} beforeAll + * @property {MochaHook[]} afterAll + * @property {MochaHook[]} beforeEach + * @property {MochaHook[]} afterEach + * + * @typedef {object} Mocha -- custom definition for `const mocha = require('mocha')` + * @property {import('mocha').utils} utils + */ + const isKarma = Boolean(process.env.KARMA); +/** + * @param {Mocha} Mocha + * @param {'warn' | 'error'} methodName + * @param {string} expectedMatcher + * @returns MochaHooks + */ function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher) { + /** + * @type {MochaHooks} + */ const mochaHooks = { beforeAll: [], afterAll: [], beforeEach: [], afterEach: [], }; + /** + * @type {[stack: string, message: string][]} + */ const unexpectedCalls = []; const stackTraceFilter = Mocha.utils.stackTraceFilter(); + /** + * @param {string} format + * @param {...unknown} args + * @returns {void} + */ function logUnexpectedConsoleCalls(format, ...args) { const message = formatUtil(format, ...args); // Safe stack so that test dev can track where the unexpected console message was created. const { stack } = new Error(); + if (stack === undefined) { + throw new TypeError( + `Unable to get stack. Logging unexpected console calls is only supported in environments where Error.prototype.stack is implemented.`, + ); + } if (process.env.NODE_ENV === 'production') { // TODO: mock scheduler @@ -34,6 +70,9 @@ function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher ]); } + /** + * @type {Console['warn' | 'error']} + */ let originalConsoleMethod; mochaHooks.beforeAll.push(function registerConsoleStub() { // eslint-disable-next-line no-console @@ -61,6 +100,7 @@ function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher if (hadUnexpectedCalls) { // In karma `file` is `null`. // We still have the stacktrace though + // @ts-expect-error -- this.currentTest being undefined would be a bug const location = this.currentTest.file ?? '(unknown file)'; const message = `Expected test not to call console.${methodName}()\n\n` + @@ -75,14 +115,13 @@ function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher error.stack = ''; if (isKarma) { - const testPath = `"${this.currentTest.parent - .titlePath() - .concat(this.currentTest.title) - .join('" -> "')}"`; + // @ts-expect-error -- this.currentTest being undefined would be a bug + const testPath = `"${this.currentTest.fullTitle()}"`; error.message += `\n\nin ${testPath}`; throw error; } else { + // @ts-expect-error -- this.test being undefined would be a bug this.test.error(error); } } @@ -91,6 +130,10 @@ function createUnexpectedConsoleMessagesHooks(Mocha, methodName, expectedMatcher return mochaHooks; } +/** + * @param {Mocha} Mocha + * @returns MochaHooks + */ function createMochaHooks(Mocha) { const warnHooks = createUnexpectedConsoleMessagesHooks(Mocha, 'warn', 'toWarnDev'); const errorHooks = createUnexpectedConsoleMessagesHooks(Mocha, 'error', 'toErrorDev');