Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Initial workspace definition #24869

Merged
merged 1 commit into from Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -214,6 +214,7 @@
"packages/*",
"docs",
"docs/packages/*",
"framer"
"framer",
"test"
]
}
8 changes: 8 additions & 0 deletions test/package.json
@@ -0,0 +1,8 @@
{
"private": true,
"name": "test",
"version": "0.0.1",
"scripts": {
"typescript": "tsc -p tsconfig.json"
}
}
1 change: 0 additions & 1 deletion test/tsconfig.json
@@ -1,4 +1,3 @@
/* Only used to enable the language server */
{
"extends": "../tsconfig.json",
"include": ["regressions/**/*", "utils/**/*"],
Expand Down
51 changes: 47 additions & 4 deletions 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
Expand All @@ -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
Expand Down Expand Up @@ -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` +
Expand All @@ -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()}"`;
Comment on lines -78 to +119
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by refactoring. Didn't know at the time that fullTitle existed. It's a bit different to the previous formatting but we use fullTitle in other parts as well so let's not surprise testers with new formatting.


error.message += `\n\nin ${testPath}`;
throw error;
} else {
// @ts-expect-error -- this.test being undefined would be a bug
this.test.error(error);
}
}
Expand All @@ -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');
Expand Down