Skip to content

Commit

Permalink
WIP: Test NPM package in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Oct 30, 2023
1 parent c508a39 commit ca02abe
Show file tree
Hide file tree
Showing 15 changed files with 2,700 additions and 5 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test NPM package
on:
pull_request:
push:
branches:
- main

jobs:
test-npm-package:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: Use Node.js 16.x
uses: actions/setup-node@v1
with:
node-version: 16.x
- run: npm ci
- run: npx playwright install chromium
- run: npm run test:package
85 changes: 80 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var webpackConfig = require('./webpack.config');
var esbuild = require('esbuild');
var umdWrapper = require('esbuild-plugin-umd-wrapper');
var banner = require('./src/fragments/license');
var process = require('process');

module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
Expand All @@ -27,18 +28,27 @@ module.exports = function (grunt) {
};
}

function execExternal(cmd) {
return function () {
var done = this.async();
grunt.log.ok('Executing ' + cmd);
async function execExternalPromises(cmd) {
grunt.log.ok('Executing ' + cmd);
return new Promise(function (resolve, reject) {
require('child_process').exec(cmd, function (err, stdout, stderr) {
if (err) {
grunt.fatal('Error executing "' + cmd + '": ' + stderr);
reject(err);
}
console.log(stdout);
stderr && console.error(stderr);
done();
resolve();
});
});
}

function execExternal(cmd) {
return function () {
var done = this.async();
execExternalPromises(cmd)
.then(() => done())
.catch((error) => done(error));
};
}

Expand Down Expand Up @@ -204,5 +214,70 @@ module.exports = function (grunt) {
}
);

grunt.registerTask('test:package:browser:prepare-project', function () {
const done = this.async();

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

if (grunt.file.exists(buildDir)) {
grunt.file.delete(buildDir);
}

grunt.file.copy(path.join(baseDir, 'template'), buildDir);

await execExternalPromises('npm run build');

await execExternalPromises('npm pack --pack-destination test/package/browser/build');
const version = grunt.file.readJSON('package.json').version;
const packFileName = `ably-${version}.tgz`;

const buildPackageJsonPath = 'test/package/browser/build/package.json';
const projectPackageJson = grunt.file.readJSON(buildPackageJsonPath);
const dependencies = projectPackageJson.dependencies ?? {};
dependencies.ably = `file:${packFileName}`;
projectPackageJson.dependencies = dependencies;
grunt.file.write(buildPackageJsonPath, JSON.stringify(projectPackageJson));

const pwd = process.cwd();
process.chdir(buildDir);
await execExternalPromises('npm install');
process.chdir(pwd);
})()
.then(() => done(true))
.catch((error) => done(error));
});

grunt.registerTask('test:package:browser:test', function () {
const done = this.async();

(async function () {
grunt.task.requires('test:package:browser:prepare-project');

const baseDir = path.join(__dirname, 'test', 'package', 'browser');
const buildDir = path.join(baseDir, 'build');

const pwd = process.cwd();
process.chdir(buildDir);

// Perform type checking
await execExternalPromises('npm run typecheck');

// Build bundle including ably-js
await execExternalPromises('npm run build');

// Smoke test the package
await execExternalPromises('npm run test');

process.chdir(pwd);
})()
.then(() => done(true))
.catch((error) => done(error));
});

grunt.registerTask('test:package:browser', ['test:package:browser:prepare-project', 'test:package:browser:test']);
grunt.registerTask('test:package', ['test:package:browser']);

grunt.registerTask('default', 'all');
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"test:node": "grunt test:node",
"test:webserver": "grunt test:webserver",
"test:playwright": "node test/support/runPlaywrightTests.js",
"test:package": "grunt test:package",
"concat": "grunt concat",
"build": "grunt build:all",
"build:node": "grunt build:node",
Expand Down
Loading

0 comments on commit ca02abe

Please sign in to comment.