Skip to content

Commit 1b5e912

Browse files
committed
Refactor test framework.
- Test runtime loads test files from a web server. - Allows testing of manifests on remote web servers. - Trading off some performance to align node and browser testing. - Moves some test setup code into config data and manifest.
1 parent a131d41 commit 1b5e912

File tree

8 files changed

+545
-248
lines changed

8 files changed

+545
-248
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# jsonld ChangeLog
22

3-
## 9.0.0 - 2023-xx-xx
3+
## 9.0.0 - 2025-xx-xx
44

55
### Changed
66
- **BREAKING**: Drop support for Node.js < 18.
@@ -23,6 +23,11 @@
2323
- Use newer corejs version.
2424
- Build with modern browserslist defaults and no IE support.
2525
- Support for older browsers requires a custom build.
26+
- Refactor test framework.
27+
- Test runtime loads test files from a web server.
28+
- Allows testing of manifests on remote web servers.
29+
- Trading off some performance to align node and browser testing.
30+
- Moves some test setup code into config data and manifest.
2631

2732
### Removed
2833
- **BREAKING**: Remove `application/nquads` alias for `application/n-quads`.

karma.conf.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,40 @@
1010
*/
1111
const os = require('os');
1212
const webpack = require('webpack');
13+
const {TestServer} = require('./tests/test-server.js');
14+
15+
// karma test server proxy details
16+
const _proxyTestsPrefix = '/tests';
17+
18+
let testServer;
19+
20+
// shutdown test server "reporter" hook
21+
function ShutdownTestServer(baseReporterDecorator) {
22+
baseReporterDecorator(this);
23+
24+
this.onRunComplete = async function() {
25+
await testServer.close();
26+
};
27+
}
28+
29+
// Inject the base reporter
30+
ShutdownTestServer.$inject = ['baseReporterDecorator', 'config'];
31+
32+
// local "reporter" plugin
33+
const shutdownTestServer = {
34+
'reporter:shutdown-test-server': ['type', ShutdownTestServer]
35+
};
36+
37+
module.exports = async function(config) {
38+
testServer = new TestServer({
39+
earlFilename: process.env.EARL
40+
});
41+
await testServer.start();
1342

14-
module.exports = function(config) {
1543
// bundler to test: webpack, browserify
1644
const bundler = process.env.BUNDLER || 'webpack';
1745

18-
const frameworks = ['mocha', 'server-side'];
46+
const frameworks = ['mocha'];
1947
// main bundle preprocessors
2048
const preprocessors = ['babel'];
2149

@@ -66,7 +94,8 @@ module.exports = function(config) {
6694
'process.env.EARL': JSON.stringify(process.env.EARL),
6795
'process.env.TESTS': JSON.stringify(process.env.TESTS),
6896
'process.env.TEST_ENV': JSON.stringify(process.env.TEST_ENV),
69-
'process.env.TEST_ROOT_DIR': JSON.stringify(__dirname),
97+
'process.env.TEST_SERVER_URL': JSON.stringify(_proxyTestsPrefix),
98+
'process.env.AUTH_TOKEN': JSON.stringify(testServer.authToken),
7099
'process.env.VERBOSE_SKIP': JSON.stringify(process.env.VERBOSE_SKIP),
71100
// for 'auto' test env
72101
'process.env._TEST_ENV_ARCH': JSON.stringify(process.arch),
@@ -151,11 +180,20 @@ module.exports = function(config) {
151180
]
152181
},
153182

183+
// local server shutdown plugin
184+
plugins: [
185+
'karma-*',
186+
shutdownTestServer
187+
],
188+
154189
// test results reporter to use
155190
// possible values: 'dots', 'progress'
156191
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
157192
//reporters: ['progress'],
158-
reporters: ['mocha'],
193+
reporters: [
194+
'mocha',
195+
'shutdown-test-server'
196+
],
159197

160198
// web server port
161199
port: 9876,
@@ -197,6 +235,8 @@ module.exports = function(config) {
197235
},
198236

199237
// Proxied paths
200-
proxies: {}
238+
proxies: {
239+
'/tests': testServer.url
240+
}
201241
});
202242
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"karma-mocha": "^2.0.1",
6464
"karma-mocha-reporter": "^2.2.5",
6565
"karma-safari-launcher": "^1.0.0",
66-
"karma-server-side": "^1.8.0",
6766
"karma-sourcemap-loader": "^0.4.0",
6867
"karma-tap-reporter": "0.0.6",
6968
"karma-webpack": "^5.0.1",

tests/test-karma.js

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
* @author Dave Longley
77
* @author David I. Lehn
88
*
9-
* Copyright (c) 2011-2023 Digital Bazaar, Inc. All rights reserved.
9+
* Copyright (c) 2011-2025 Digital Bazaar, Inc. All rights reserved.
1010
*/
11-
/* global serverRequire */
1211
// FIXME: hack to ensure delay is set first
1312
mocha.setup({delay: true, ui: 'bdd'});
1413

1514
const assert = require('chai').assert;
1615
const benchmark = require('benchmark');
1716
const common = require('./test.js');
18-
const server = require('karma-server-side');
1917
const webidl = require('./test-webidl');
20-
const join = require('join-path-js');
2118

2219
// special benchmark setup
2320
const _ = require('lodash');
@@ -26,70 +23,25 @@ window.Benchmark = Benchmark;
2623

2724
const entries = [];
2825

26+
// setup test server url, add localhost if needed
27+
let testServerUrl = process.env.TEST_SERVER_URL;
28+
if(!testServerUrl.endsWith('/')) {
29+
testServerUrl += '/';
30+
}
31+
if(!(testServerUrl.startsWith('http:') || testServerUrl.startsWith('https:'))) {
32+
const pathname = testServerUrl;
33+
testServerUrl = new URL(window.location);
34+
testServerUrl.pathname = pathname;
35+
}
36+
2937
if(process.env.TESTS) {
3038
entries.push(...process.env.TESTS.split(' '));
3139
} else {
32-
const _top = process.env.TEST_ROOT_DIR;
33-
// TODO: support just adding certain entries in EARL mode?
34-
35-
// json-ld-api main test suite
36-
entries.push((async () => {
37-
const testPath = join(_top, 'test-suites/json-ld-api/tests');
38-
const siblingPath = join(_top, '../json-ld-api/tests');
39-
return server.run(testPath, siblingPath, function(testPath, siblingPath) {
40-
const fs = serverRequire('fs-extra');
41-
// use local tests if setup
42-
if(fs.existsSync(testPath)) {
43-
return testPath;
44-
}
45-
// default to sibling dir
46-
return siblingPath;
47-
});
48-
})());
49-
50-
// json-ld-framing main test suite
51-
entries.push((async () => {
52-
const testPath = join(_top, 'test-suites/json-ld-framing/tests');
53-
const siblingPath = join(_top, '../json-ld-framing/tests');
54-
return server.run(testPath, siblingPath, function(testPath, siblingPath) {
55-
const fs = serverRequire('fs-extra');
56-
// use local tests if setup
57-
if(fs.existsSync(testPath)) {
58-
return testPath;
59-
}
60-
// default to sibling dir
61-
return siblingPath;
62-
});
63-
})());
64-
65-
/*
66-
// TODO: use json-ld-framing once tests are moved
67-
// json-ld.org framing test suite
68-
// FIXME: add path detection
69-
entries.push(join(
70-
_top, 'test-suites/json-ld.org/test-suite/tests/frame-manifest.jsonld'));
71-
entries.push(join(
72-
_top, '../json-ld.org/test-suite/tests/frame-manifests.jsonld'));
73-
*/
40+
entries.push(new URL('tests/default/', testServerUrl));
7441

75-
// W3C RDF Dataset Canonicalization "rdf-canon" test suite
76-
entries.push((async () => {
77-
const testPath = join(_top, 'test-suites/rdf-canon/tests');
78-
const siblingPath = join(_top, '../rdf-canon/tests');
79-
return server.run(testPath, siblingPath, function(testPath, siblingPath) {
80-
const fs = serverRequire('fs-extra');
81-
// use local tests if setup
82-
if(fs.existsSync(testPath)) {
83-
return testPath;
84-
}
85-
// default to sibling dir
86-
return siblingPath;
87-
});
88-
})());
42+
// TODO: support just adding certain entries in EARL mode?
8943

90-
// other tests
91-
entries.push(join(_top, 'tests/misc.js'));
92-
entries.push(join(_top, 'tests/graph-container.js'));
44+
// other tests (including js ones) added with options.addExtraTests
9345

9446
// WebIDL tests
9547
entries.push(webidl);
@@ -126,33 +78,35 @@ const options = {
12678
throw new Error('exit not implemented');
12779
},
12880
earl: {
81+
enabled: !!process.env.EARL,
12982
filename: process.env.EARL
13083
},
13184
entries,
85+
addExtraTests: async () => {
86+
// direct load for bundling
87+
// called after handling other entry loading
88+
require('./misc.js');
89+
require('./graph-container.js');
90+
},
13291
testEnvDefaults,
133-
readFile: filename => {
134-
return server.run(filename, function(filename) {
135-
const fs = serverRequire('fs-extra');
136-
return fs.readFile(filename, 'utf8').then(data => {
137-
return data;
138-
});
139-
});
92+
get testServerUrl() {
93+
return testServerUrl;
14094
},
141-
writeFile: (filename, data) => {
142-
return server.run(filename, data, function(filename, data) {
143-
const fs = serverRequire('fs-extra');
144-
return fs.outputFile(filename, data);
145-
});
95+
get authToken() {
96+
return process.env.AUTH_TOKEN;
14697
},
147-
/* eslint-disable-next-line no-unused-vars */
14898
import: f => {
14999
console.error('import not implemented for "' + f + '"');
150-
}
100+
},
101+
cleanup: async () => {}
151102
};
152103

153-
// wait for setup of all tests then run mocha
154-
common(options).then(() => {
104+
async function main() {
105+
// wait for setup of all tests then run mocha
106+
await common.setup(options);
155107
run();
156-
}).catch(err => {
108+
}
109+
110+
main().catch(err => {
157111
console.error(err);
158112
});

0 commit comments

Comments
 (0)