Skip to content

Commit

Permalink
Merge pull request #1652 from ably/test-improvements
Browse files Browse the repository at this point in the history
test: test ux improvements
  • Loading branch information
owenpearson authored Mar 14, 2024
2 parents 8edfc0f + 019de8c commit a32bc6c
Show file tree
Hide file tree
Showing 21 changed files with 216 additions and 579 deletions.
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'],
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',
]);
},
);

(function () {
const baseDir = path.join(__dirname, 'test', 'package', 'browser');
const buildDir = path.join(baseDir, 'build');
Expand Down
Loading

0 comments on commit a32bc6c

Please sign in to comment.