Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test suite

on: [push, pull_request]

jobs:
tests:
name: tests
runs-on: ubuntu-latest
strategy:
matrix:
meteor: ['2.3', '2.16', '3.0.4', '3.5.4' ]
steps:
- name: checkout
uses: actions/checkout@v7

- name: Setup meteor
uses: meteorengineer/setup-meteor@v2
with:
meteor-release: ${{ matrix.meteor }}

- name: cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- run: |
meteor npm install
meteor npm run test:ci
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
coverage-source/
.nyc_output/
coverage/
.test-app/
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,36 @@ FlowRouter.go('listsShow', { _id: listId });

### Running tests

```shell
npm ci
npm run test # runs once
npm run test:watch # reruns on changes
```
meteor test-packages --driver-package practicalmeteor:mocha ./

### Package test coverage

Install the development tools and run the same package tests with manual
[Istanbul instrumentation](https://github.com/istanbuljs/istanbuljs/tree/main/packages/istanbul-lib-instrument):

```shell
npm ci
npm run test:coverage
```

The runner instruments a temporary copy of `validated-method.js` before Meteor
compiles it, then runs
`meteor test-packages --once --driver-package meteortesting:mocha ./`.
Only coverage runs select this copy; ordinary package consumers use the original
source. Test-only Mocha hooks save counters in `.nyc_output/`. The runner merges
the counters and writes `coverage/index.html`, `coverage/lcov.info`, and
`coverage/coverage-final.json`, with locations referring to the original source.
It preserves a failing test command's exit status and removes the generated source.
Run only one coverage process at a time in this checkout.

For browser coverage, configure the test driver's browser support (for example,
`TEST_BROWSER_DRIVER=puppeteer npm run test:coverage` with Puppeteer available to
the driver). The client hook awaits transfer of its counters to the test server
before finishing. Without a browser, the runner explicitly reports server-only
coverage; client branches remain uncovered. Reports are also generated after
test failures if the hooks collected counters. Startup failures produce an error
instead of an empty coverage report.
37 changes: 37 additions & 0 deletions coverage-support/collect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Meteor } from 'meteor/meteor';

// Loaded only into the local test package during an instrumented run.
const methodName = '__validatedMethodCoverageReport';
if (Meteor.isServer) {
const fs = Npm.require('fs');
const path = Npm.require('path');
const write = (name, coverage) => {
if (!coverage || !Object.keys(coverage).length) {
throw new Error(`Missing ${name} coverage counters`);
}
fs.writeFileSync(
path.join(process.env.VALIDATED_METHOD_COVERAGE, `${name}.json`),
JSON.stringify(coverage),
);
};
Meteor.methods({
[methodName](coverage) {
write('client', coverage);
// Include server execution caused by client tests, regardless of suite order.
write('server', globalThis.__validatedMethodCoverage__);
},
});
after(function () {
write('server', globalThis.__validatedMethodCoverage__);
});
} else {
after(async function () {
this.timeout(10000);
const coverage = globalThis.__validatedMethodCoverage__;
// Meteor.callAsync only exists since Meteor 2.8
await (Meteor.callAsync
? Meteor.callAsync(methodName, coverage)
: new Promise((resolve, reject) => Meteor.call(methodName, coverage,
(error, result) => (error ? reject(error) : resolve(result)))));
});
}
75 changes: 75 additions & 0 deletions coverage-support/run.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const { createInstrumenter } = require('istanbul-lib-instrument');
const { createCoverageMap } = require('istanbul-lib-coverage');
const { createContext } = require('istanbul-lib-report');
const reports = require('istanbul-reports');

const root = path.resolve(__dirname, '..');
const generated = path.join(root, 'coverage-source');
const raw = path.join(root, '.nyc_output');
const output = path.join(root, 'coverage');
const source = path.join(root, 'validated-method.js');
for (const dir of [generated, raw, output]) {
fs.rmSync(dir, { recursive: true, force: true });
fs.mkdirSync(dir, { recursive: true });
}
const instrumenter = createInstrumenter({
esModules: true,
coverageVariable: '__validatedMethodCoverage__',
coverageGlobalScope: 'globalThis',
coverageGlobalScopeFunc: false,
});
fs.writeFileSync(path.join(generated, 'validated-method.js'),
instrumenter.instrumentSync(fs.readFileSync(source, 'utf8'), source));

// test-packages bundles a separate test app and Npm.require ignores NODE_PATH,
// so the browser driver must be an npm dependency of that app. Meteor runs
// `npm install` there when the app already has a package.json.
// puppeteer 19 is the newest line that still runs on Meteor 2.x (Node 14).
const testApp = path.join(root, '.test-app');
fs.mkdirSync(testApp, { recursive: true });
fs.writeFileSync(path.join(testApp, 'package.json'), `${JSON.stringify({
private: true,
dependencies: {
'@babel/runtime': '^7.23.5',
'@swc/helpers': '^0.5.17',
'jquery': '^3.7.1',
'meteor-node-stubs': '^1.2.5',
'puppeteer': '19.11.1',
},
}, null, 2)}\n`);

try {
const result = spawnSync('meteor',
['test-packages', '--once', '--driver-package', 'meteortesting:mocha',
'--test-app-path', testApp, './'], {
cwd: root,
stdio: 'inherit',
// a driver that fails after the server suite leaves `--once` running forever
timeout: 10 * 60 * 1000,
killSignal: 'SIGKILL',
env: { ...process.env, VALIDATED_METHOD_COVERAGE: raw },
});
if (result.error) throw result.error;
const files = fs.readdirSync(raw).filter(file => file.endsWith('.json'));
if (!files.includes('server.json')) {
throw new Error('No server coverage collected; the package test harness did not finish.');
}
const map = createCoverageMap({});
for (const file of files) {
map.merge(JSON.parse(fs.readFileSync(path.join(raw, file), 'utf8')));
}
const context = createContext({ dir: output, coverageMap: map });
for (const reporter of ['text', 'html', 'lcovonly', 'json']) {
reports.create(reporter).execute(context);
}
console.log(`Coverage reports: ${output}`);
if (!files.includes('client.json')) {
console.warn('Server-only coverage: no browser coverage was received.');
}
process.exitCode = result.status === null ? 1 : result.status;
} finally {
fs.rmSync(generated, { recursive: true, force: true });
}
14 changes: 9 additions & 5 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@ Package.describe({
});

Package.onUse(function (api) {
api.versionsFrom('1.7');
api.versionsFrom(['2.3', '3.0']);

api.use([
'ecmascript',
'check',
'ddp',
]);

api.mainModule('validated-method.js');
api.mainModule(process.env.VALIDATED_METHOD_COVERAGE
? 'coverage-source/validated-method.js'
: 'validated-method.js');
api.export('ValidatedMethod');
});

Package.onTest(function (api) {
api.use([
'ecmascript',
'practicalmeteor:mocha@2.4.5_6',
'practicalmeteor:chai@2.1.0_1',
'aldeed:simple-schema@1.5.4',
'meteortesting:mocha@2.0.0 || 3.0.0',
'aldeed:simple-schema@2.0.0 || 3.0.0', // modern support
'mdg:validated-method',
'random'
]);

if (process.env.VALIDATED_METHOD_COVERAGE) {
api.addFiles('coverage-support/collect.js');
}
api.mainModule('validated-method-tests.js');
});
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"scripts": {
"test": "meteor test-packages --once --driver-package meteortesting:mocha ./",
"test:watch": "meteor test-packages --driver-package meteortesting:mocha ./",
"test:coverage": "node coverage-support/run.cjs",
"test:ci": "TEST_BROWSER_DRIVER=puppeteer npm run test:coverage"
},
"devDependencies": {
"chai": "^4.5.0",
"istanbul-lib-coverage": "3.2.2",
"istanbul-lib-instrument": "6.0.3",
"istanbul-lib-report": "3.0.1",
"istanbul-reports": "3.2.0",
"simpl-schema": "^1.13.1"
}
}
Loading