Skip to content

Commit 5f587f7

Browse files
committedMay 2, 2023
chore(deno): change to start mocha fixtures before mocha
also start mocha in a different process
1 parent 73ef135 commit 5f587f7

File tree

4 files changed

+85
-31
lines changed

4 files changed

+85
-31
lines changed
 

‎test/.eslintrc.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ rules:
44
# In `document.test.js` we sometimes use self assignment to test setters
55
no-self-assign: off
66
ignorePatterns:
7-
- deno.js
7+
- deno.js
8+
- deno*.js

‎test/deno.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,39 @@
22

33
import { createRequire } from "node:module";
44
import process from "node:process";
5+
import { resolve } from "node:path";
6+
import {fileURLToPath} from "node:url";
57

6-
import { parse } from "https://deno.land/std/flags/mod.ts"
7-
const args = parse(Deno.args);
8+
import { spawn } from "node:child_process";
89

910
Error.stackTraceLimit = 100;
1011

1112
const require = createRequire(import.meta.url);
1213

13-
const Mocha = require('mocha');
14-
const fs = require('fs');
15-
const path = require('path');
14+
const fixtures = require('./mocha-fixtures.js')
1615

17-
// const fixtures = require('./mocha-fixtures.js');
16+
await fixtures.mochaGlobalSetup();
1817

19-
const mocha = new Mocha({
20-
timeout: 8000,
21-
...(args.g ? { fgrep: '' + args.g } : {})
22-
});
23-
24-
// the following is required because mocha somehow does not load "require" options and so needs to be manually set-up
25-
// mocha.globalSetup(fixtures.mochaGlobalSetup);
26-
// mocha.globalTeardown(fixtures.mochaGlobalTeardown);
18+
const child_args = [
19+
// args is required to be set manually, because there is currently no way to get all arguments from deno
20+
'--allow-env', '--allow-read', '--allow-net', '--allow-run', '--allow-sys', '--allow-write',
21+
...Deno.args,
22+
resolve(fileURLToPath(import.meta.url), '../deno_mocha.js')
23+
];
2724

28-
const testDir = 'test';
25+
const child = spawn(process.execPath, child_args, { stdio: 'inherit' });
2926

30-
const files = fs.readdirSync(testDir).
31-
concat(fs.readdirSync(path.join(testDir, 'docs')).map(file => path.join('docs', file))).
32-
concat(fs.readdirSync(path.join(testDir, 'helpers')).map(file => path.join('helpers', file)));
33-
34-
const ignoreFiles = new Set(['browser.test.js']);
27+
child.on('exit', (code, signal) => {
28+
signal ? doExit(-100) : doExit(code);
29+
});
3530

36-
for (const file of files) {
37-
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
38-
continue;
39-
}
31+
Deno.addSignalListener("SIGINT", () => {
32+
console.log("SIGINT");
33+
child.kill("SIGINT");
34+
doExit(-2);
35+
});
4036

41-
mocha.addFile(path.join(testDir, file));
37+
async function doExit(code) {
38+
await fixtures.mochaGlobalTeardown();
39+
Deno.exit(code);
4240
}
43-
44-
mocha.run(function(failures) {
45-
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
46-
process.exit(process.exitCode);
47-
});

‎test/deno_mocha.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
import { createRequire } from "node:module";
4+
import process from "node:process";
5+
6+
// Workaround for Mocha getting terminal width, which currently requires `--unstable`
7+
Object.defineProperty(process.stdout, 'getWindowSize', {
8+
value: function() {
9+
return [75, 40];
10+
}
11+
});
12+
13+
import { parse } from "https://deno.land/std/flags/mod.ts"
14+
const args = parse(Deno.args);
15+
16+
Error.stackTraceLimit = 100;
17+
18+
const require = createRequire(import.meta.url);
19+
20+
const Mocha = require('mocha');
21+
const fs = require('fs');
22+
const path = require('path');
23+
24+
// const fixtures = require('./mocha-fixtures.js')
25+
26+
const mocha = new Mocha({
27+
timeout: 8000,
28+
...(args.g ? { fgrep: '' + args.g } : {})
29+
});
30+
31+
// the following is required because mocha somehow does not load "require" options and so needs to be manually set-up
32+
// mocha.globalSetup(fixtures.mochaGlobalSetup);
33+
// mocha.globalTeardown(fixtures.mochaGlobalTeardown);
34+
35+
const testDir = 'test';
36+
37+
const files = fs.readdirSync(testDir).
38+
concat(fs.readdirSync(path.join(testDir, 'docs')).map(file => path.join('docs', file))).
39+
concat(fs.readdirSync(path.join(testDir, 'helpers')).map(file => path.join('helpers', file)));
40+
41+
const ignoreFiles = new Set(['browser.test.js']);
42+
43+
for (const file of files) {
44+
if (!file.endsWith('.test.js') || ignoreFiles.has(file)) {
45+
continue;
46+
}
47+
48+
mocha.addFile(path.join(testDir, file));
49+
}
50+
51+
mocha.run(function(failures) {
52+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
53+
process.exit(process.exitCode);
54+
});

‎test/mocha-fixtures.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
const mms = require('mongodb-memory-server');
44

5+
/*
6+
7+
Note: dont use any mocha-specific things in this file, it may or may not be called without mocha (see deno.js)
8+
9+
*/
10+
511
/*
612
* Default MMS mongodb version is used, unless MONGOMS_VERSION is set (which is set with the matrix in test.yml for CI)
713
*/

0 commit comments

Comments
 (0)
Please sign in to comment.