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: test ux improvements #1652

Merged
merged 11 commits into from
Mar 14, 2024
Merged
19 changes: 19 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const config = {
require: ['source-map-support/register', 'test/support/modules_helper.js', 'test/support/test_helper.js'],
Copy link
Contributor

Choose a reason for hiding this comment

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

grunt-mocha.js previously also had test/support/root_hooks.js.
Do we not need it anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

good point! these hooks weren't being run in the browser at all so i made a commit to move them into shared_helper.js in another branch (359c514), i might just rebase this PR onto that one for the sake of simplicity rather than duplicating the commit

Copy link
Member Author

Choose a reason for hiding this comment

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

cool, yeah i'm gonna wait for #1645 to land on the integration branch and rebase this PR (which will resolve this comment) so we can hold off merging until then

Copy link
Contributor

@VeskeR VeskeR Mar 7, 2024

Choose a reason for hiding this comment

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

Oh, I see!
I've noticed those new afterEach hooks in shared_helper and was wondering if that was a replacement for root_hooks.js, and turns out it is.

Now, I believe root_hooks.js was never registered in browser tests, because even though it is listed in browser_file_list.js, in browser_setup.js there are TEST_REGEXP and TEAR_DOWN_REGEXP regexps that filter out anything that is not *.test.js or *tear_down.js -> root_hooks.js doesn't match. And there is no other place where root_hooks.js would be imported in browser setup, so I guess that was the culprit.

I feel like separate root_hooks.js file was a better place for hooks like these, they seem a bit out of place for shared_helper.js.

That being said, current browser test setup is a maze and I don't want to drag on this PR for much longer. I believe Unified Test Suite will be the opportunity for us to re-implement browser tests with more modern setup.

So we can leave this as it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah i agree it's not ideal, we can file an issue to come up with something better but i think this pr is an overall improvement so i agree it's okay to leave it for now 👍

file: ['test/support/root_hooks.js'],
reporter: 'test/support/mocha_reporter.js',
};

// mocha has a ridiculous issue (https://github.com/mochajs/mocha/issues/4100) that command line
// specs don't override config specs; they are merged instead, so you can't run a single test file
// if you've defined specs in your config. therefore we work around it by only adding specs to the
// config if none are passed as arguments
if (!process.argv.slice(2).some(isTestFile)) {
config.spec = ['test/realtime/*.test.js', 'test/rest/*.test.js'];
}

function isTestFile(arg) {
return arg.match(/\.test.js$/);
}

module.exports = config;
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ Run the Mocha test suite

npm run test:node

Or run just one test file
You can pass any Mocha CLI arguments and flags to the test:node script after the `--` separator, for example running one test file:

npm run test:node -- --file=test/realtime/auth.test.js
npm run test:node -- test/realtime/auth.test.js

Or run just one test

npm run test:node -- --file=test/rest/status.test.js --grep=test_name_here
npm run test:node -- --grep=test_name_here

Or run test skipping the build

npm run test:node:skip-build -- --file=test/rest/status.test.js --grep=test_name_here
npm run test:node:skip-build -- test/rest/status.test.js --grep=test_name_here

### Debugging the mocha tests locally with a debugger

Run the following command to launch tests with the debugger enabled. The tests will block until you attach a debugger.

node --inspect-brk=9229 node_modules/.bin/grunt test:node
node --inspect-brk=9229 node_modules/.bin/mocha

Alternatively you can also run the tests for single file

node --inspect-brk=9229 node_modules/.bin/grunt test:node --test=test/realtime/auth.test.js
node --inspect-brk=9229 node_modules/.bin/mocha test/realtime/auth.test.js

The included vscode launch config allows you to launch and attach the debugger in one step, simply open the test
file you want to run and start debugging. Note that breakpoint setting for realtime code will be within the
Expand Down
103 changes: 16 additions & 87 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ var umdWrapper = require('esbuild-plugin-umd-wrapper');
var banner = require('./src/fragments/license');
var process = require('process');
var stripLogsPlugin = require('./grunt/esbuild/strip-logs').default;
var MochaServer = require('./test/web_server');

module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-bump');
grunt.loadNpmTasks('grunt-webpack');

var dirs = {
Expand All @@ -22,13 +21,6 @@ module.exports = function (grunt) {
dest: 'build',
};

function compilerSpec(src, dest) {
return {
src: src,
dest: dest || src.replace(/\.js/, '.min.js'),
};
}

async function execExternalPromises(cmd) {
grunt.log.ok('Executing ' + cmd);
return new Promise(function (resolve, reject) {
Expand All @@ -55,29 +47,13 @@ module.exports = function (grunt) {

var gruntConfig = {
dirs: dirs,
pkgVersion: grunt.file.readJSON('package.json').version,
webpack: {
all: Object.values(webpackConfig),
node: [webpackConfig.node],
browser: [webpackConfig.browser, webpackConfig.browserMin, webpackConfig.mochaJUnitReporterBrowser],
},
};

gruntConfig.bump = {
options: {
files: ['package.json', 'README.md'],
globalReplace: true,
commit: true,
commitMessage: 'Regenerate and release version %VERSION%',
commitFiles: [], // Add files manually as can't add new files with a commit flag
createTag: true,
tagName: '%VERSION%',
tagMessage: 'Version %VERSION%',
push: false,
prereleaseName: 'beta',
},
};

grunt.initConfig(gruntConfig);

grunt.registerTask('checkGitSubmodules', 'Check, if git submodules are properly installed', function () {
Expand Down Expand Up @@ -108,6 +84,21 @@ module.exports = function (grunt) {

grunt.registerTask('all', ['build', 'requirejs']);

grunt.registerTask('mocha:webserver', 'Run the Mocha web server', function () {
const done = this.async();
const server = new MochaServer();
server.listen();

process.on('SIGTERM', () => {
server.close();
done();
});
process.on('SIGINT', () => {
server.close();
done();
});
});

grunt.registerTask('build:browser', function () {
var done = this.async();

Expand Down Expand Up @@ -152,74 +143,12 @@ module.exports = function (grunt) {
});
});

grunt.loadTasks('test/tasks');

grunt.registerTask('test', ['test:node']);
grunt.registerTask(
'test:node',
'Build the library and run the node test suite\nOptions\n --test [tests] e.g. --test test/rest/auth.js',
['build:node', 'mocha'],
);

grunt.registerTask('test:webserver', 'Launch the Mocha test web server on http://localhost:3000/', [
'build:browser',
'checkGitSubmodules',
'mocha:webserver',
]);

grunt.registerTask('release:refresh-pkgVersion', 'Refreshes GruntConfig.pkgVersion', function () {
grunt.config('pkgVersion', grunt.file.readJSON('package.json').version);
grunt.log.ok('pkgVersion updated');
});

grunt.registerTask('release:git-add-generated', 'Adds generated files to the git staging area', function () {
var done = this.async();
var generatedFiles = [
gruntConfig.dirs.common + '/lib/util/defaults.js',
gruntConfig.dirs.fragments + '/license.js',
'package.json',
'package-lock.json',
'README.md',
'test/support/browser_file_list.js',
];
var cmd = 'git add -A ' + generatedFiles.join(' ');
grunt.log.ok('Executing ' + cmd);

require('child_process').exec(cmd, function (err, stdout, stderr) {
if (err) {
grunt.fatal('git add . -A failed with ' + stderr);
}
done();
});
});

grunt.registerTask('release:git-push', 'Pushes to git', execExternal('git push origin main --follow-tags'));

grunt.registerTask('release:ably-deploy', 'Deploys to ably CDN', function () {
var version = grunt.file.readJSON('package.json').version,
cmd = 'node scripts/cdn_deploy.js --skipCheckout --tag ' + version;
console.log('Publishing version ' + version + ' of the library to the CDN');
execExternal(cmd).call(this);
});

grunt.registerTask('release:deploy', 'Pushes a new release to github and then deploys to the Ably CDN', function () {
grunt.task.run(['release:git-push', 'release:ably-deploy']);
});

grunt.registerTask(
'release',
'Increments the version, regenerates, and makes a tagged commit. Run as "grunt release:type", where "type" is "major", "minor", "patch", "prepatch", etc.)',
function (versionType) {
grunt.task.run([
'bump-only:' + versionType,
'release:refresh-pkgVersion',
'all',
'release:git-add-generated',
'bump-commit',
]);
},
);

VeskeR marked this conversation as resolved.
Show resolved Hide resolved
(function () {
const baseDir = path.join(__dirname, 'test', 'package', 'browser');
const buildDir = path.join(baseDir, 'build');
Expand Down
Loading
Loading