Skip to content
Open

2.3.1 #100

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f83c868
Fix security vulnerabilities by updating dependency
ShadSterling Jan 1, 2019
e045360
Add TypeScript type declarations for programmaticRunner exports
ShadSterling Jan 14, 2019
f0f809a
Merge pull request #1 from ShadSterling/patch-1
ShadSterling Nov 17, 2022
fcde646
Merge pull request #2 from ShadSterling/patch-2
ShadSterling Nov 17, 2022
f7f1800
Update dependencies and bump version
ShadSterling Nov 17, 2022
36575ff
Merge pull request #3 from ShadSterling/2.2.X
ShadSterling Nov 17, 2022
607553f
Fix error in handwritten types
ShadSterling Nov 17, 2022
6811f93
Bump version for release
ShadSterling Nov 17, 2022
543a17a
Merge pull request #4 from ShadSterling/2.2.X
ShadSterling Nov 17, 2022
24c0a8b
Replace tshint with TypeScript, replace handwritten type declarations…
ShadSterling Nov 19, 2022
e2f648f
Add testFiles to make the repo a usable dependency independent of pub…
ShadSterling Nov 19, 2022
7f60e75
Merge pull request #5 from ShadSterling/2.2.X
ShadSterling Nov 19, 2022
3832fca
Replace var with let (outside of tests), nil with null
ShadSterling Nov 20, 2022
795b9d6
Fix TypeScript confusion by standardizing capitalization; confirm by …
ShadSterling Nov 20, 2022
9cb7d60
Update metadata for publication
ShadSterling Dec 28, 2022
9f36ea8
Fix broken links in README
ShadSterling Dec 28, 2022
34eb8e3
Merge branch 'master' into 2.3.X
ShadSterling Dec 28, 2022
0e65cf3
Merge pull request #6 from ShadSterling/2.3.X
ShadSterling Dec 28, 2022
940f6c6
Change NPM publication name to publish unscoped
ShadSterling Dec 28, 2022
ff83351
Merge pull request #7 from ShadSterling/rename
ShadSterling Dec 28, 2022
0e48669
Apply new name to instructions in README
ShadSterling Dec 28, 2022
b215210
Merge pull request #9 from ShadSterling/rename
ShadSterling Dec 28, 2022
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
Prev Previous commit
Next Next commit
Fix TypeScript confusion by standardizing capitalization; confirm by …
…testing on stdlib Promise
  • Loading branch information
ShadSterling committed Nov 20, 2022
commit 795b9d6fde72cad74d6d422ffbca60c38cce271f
4 changes: 2 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

let path = require("path");
let getMochaOpts = require("./getMochaOpts");
let programmaticRunner = require("./programmaticRunner");
let ProgrammaticRunner = require("./programmaticRunner");

let filePath = getAdapterFilePath();
let adapter = adapterObjectFromFilePath(filePath);
let mochaOpts = getMochaOpts(process.argv.slice(3));
programmaticRunner(adapter, mochaOpts, function (err) {
ProgrammaticRunner(adapter, mochaOpts, function (err) {
if (err) {
process.exit(err.failure_count || -1);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/programmaticRunner.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export = promisesAPlusTests;
export = PromisesAPlusTests;
/** @type { PromisesAPlusTests.Export } */
declare let promisesAPlusTests: PromisesAPlusTests.Export;
declare function PromisesAPlusTests(adapter: PromisesAPlusTests.Adapter<any>, mocha_options_or_callback: any | PromisesAPlusTests.Callback, callback_or_omitted: PromisesAPlusTests.Callback | void): void;
declare namespace PromisesAPlusTests {
function mocha(implementation: Adapter<any>): void;
type Adapter<T> = {
resolved?: (value: T) => Promise<T>;
rejected?: (reason: any) => Promise<never>;
Expand All @@ -20,7 +21,7 @@ declare namespace PromisesAPlusTests {
type MochaTester = {
mocha: PromisesAPlusTests.MochaTestMethod;
};
type MochaTestMethod = (impormentation: PromisesAPlusTests.Adapter<any>) => void;
type MochaTestMethod = (implementation: PromisesAPlusTests.Adapter<any>) => void;
type Tester = PromisesAPlusTests.TesterWithOptions & PromisesAPlusTests.TesterWithoutOptions;
type TestError = Error & PromisesAPlusTests.MaybeFailureCount;
type TesterWithOptions = (implementation: PromisesAPlusTests.Adapter<any>, mochaOptions: any, callback?: PromisesAPlusTests.Callback) => void;
Expand Down
146 changes: 69 additions & 77 deletions lib/programmaticRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,106 +6,98 @@
/** @typedef { ( PromisesAPlusTests.TesterWithOptions | PromisesAPlusTests.TesterWithoutOptions ) & PromisesAPlusTests.MochaTester } PromisesAPlusTests.Export */
/** @typedef { { failure_count?: number } } PromisesAPlusTests.MaybeFailureCount */
/** @typedef { { mocha: PromisesAPlusTests.MochaTestMethod } } PromisesAPlusTests.MochaTester */
/** @typedef { ( impormentation: PromisesAPlusTests.Adapter<any> ) => void } PromisesAPlusTests.MochaTestMethod */
/** @typedef { ( implementation: PromisesAPlusTests.Adapter<any> ) => void } PromisesAPlusTests.MochaTestMethod */
/** @typedef { PromisesAPlusTests.TesterWithOptions & PromisesAPlusTests.TesterWithoutOptions } PromisesAPlusTests.Tester */
/** @typedef { Error & PromisesAPlusTests.MaybeFailureCount } PromisesAPlusTests.TestError */
/** @typedef { ( implementation: PromisesAPlusTests.Adapter<any>, mochaOptions: any, callback?: PromisesAPlusTests.Callback ) => void } PromisesAPlusTests.TesterWithOptions */
/** @typedef { ( implementation: PromisesAPlusTests.Adapter<any>, callback?: PromisesAPlusTests.Callback ) => void } PromisesAPlusTests.TesterWithoutOptions */

let mocha_constructor = require("mocha");
let Mocha = require("mocha");
let path = require("path");
let fs = require("fs");
let _ = require("underscore");

let testsDir = path.resolve(__dirname, "tests");

/** @type { (adapter: PromisesAPlusTests.Adapter<any>) => void } */
function normalizeAdapter(adapter) {
if (!adapter.resolved) {
adapter.resolved = function (value) {
let d = adapter.deferred();
d.resolve(value);
return d.promise;
};
}

if (!adapter.rejected) {
adapter.rejected = function (reason) {
let d = adapter.deferred();
d.reject(reason);
return d.promise;
};
}
if (!adapter.resolved) {
adapter.resolved = function (value) {
let d = adapter.deferred();
d.resolve(value);
return d.promise;
};
}
if (!adapter.rejected) {
adapter.rejected = function (reason) {
/** @type { PromisesAPlusTests.Deferred<never> } */
let d = adapter.deferred();
d.reject(reason);
return d.promise;
};
}
}

/** @type { PromisesAPlusTests.Export } */
let promisesAPlusTests = function (
/** @type { PromisesAPlusTests.Adapter<any> } */
adapter,
/** @type { any | PromisesAPlusTests.Callback } */
mochaOpts_or_callback,
/** @type { PromisesAPlusTests.Callback | void } */
callback_or_omitted,
function PromisesAPlusTests (
/** @type { PromisesAPlusTests.Adapter<any> } */
adapter,
/** @type { any | PromisesAPlusTests.Callback } */
mocha_options_or_callback,
/** @type { PromisesAPlusTests.Callback | void } */
callback_or_omitted,
) {
let mochaOpts = {}
/** @type { PromisesAPlusTests.Callback } */
let cb = function () { };
if( mochaOpts_or_callback && typeof mochaOpts_or_callback !== "function" ) {
mochaOpts = mochaOpts_or_callback;
if( typeof callback_or_omitted === "function" ) {
cb = callback_or_omitted;
}
} else if ( typeof mochaOpts_or_callback === "function" ) {
cb = mochaOpts_or_callback
}
let mochaOpts = {}
/** @type { PromisesAPlusTests.Callback } */
let cb = function () { };
if( mocha_options_or_callback && typeof mocha_options_or_callback !== "function" ) {
mochaOpts = mocha_options_or_callback;
if( typeof callback_or_omitted === "function" ) {
cb = callback_or_omitted;
}
} else if ( typeof mocha_options_or_callback === "function" ) {
cb = mocha_options_or_callback
}

normalizeAdapter(adapter);
mochaOpts = _.defaults(mochaOpts, { timeout: 200, slow: Infinity });
normalizeAdapter(adapter);
mochaOpts = _.defaults(mochaOpts, { timeout: 200, slow: Infinity });

fs.readdir(testsDir, function (err, testFileNames) {
if (err) {
cb(err);
return;
}
fs.readdir( testsDir, (err, testFileNames) => {
if (err) {
cb(err);
return;
}

let mocha = new mocha_constructor(mochaOpts);
testFileNames.forEach(function (testFileName) {
if (path.extname(testFileName) === ".js") {
let testFilePath = path.resolve(testsDir, testFileName);
mocha.addFile(testFilePath);
}
});
let mocha = new Mocha(mochaOpts);
testFileNames.forEach( (testFileName) => {
if( path.extname(testFileName) === ".js" ) {
let testFilePath = path.resolve( testsDir, testFileName );
mocha.addFile(testFilePath);
}
});

global.adapter = adapter;
mocha.run(function (failures) {
delete global.adapter;
if (failures > 0) {
/** @type { PromisesAPlusTests.TestError } */
let err = new Error("Test suite failed with " + failures + " failures.");
err.failure_count = failures;
cb(err);
} else {
cb();
}
});
});
global.adapter = adapter;
mocha.run( (failures) => {
delete global.adapter;
if (failures > 0) {
/** @type { PromisesAPlusTests.TestError } */
let err = new Error("Test suite failed with " + failures + " failures.");
err.failure_count = failures;
cb(err);
} else {
cb();
}
} );
} );
};

/** @type { PromisesAPlusTests.MochaTestMethod } */
promisesAPlusTests.mocha = function (adapter) {
normalizeAdapter(adapter);

global.adapter = adapter;

require("./testFiles");
// let testfiles = "./testFiles.js";
// if( fs.existsSync(testfiles) ) {
// require( testfiles );
// } else {
// throw new Error( `Missing required file ${testfiles}` )
// }

delete global.adapter;
PromisesAPlusTests.mocha = function mocha(adapter) {
normalizeAdapter(adapter);
global.adapter = adapter;
require("./testFiles");
delete global.adapter;
};

/** @type { PromisesAPlusTests.Export } */
module.exports = promisesAPlusTests
module.exports = PromisesAPlusTests
2 changes: 1 addition & 1 deletion lib/tests/2.2.1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

require(null); // Using an "import form" tells TypeScript that this is a module that can be required by other scripts
require("./2.2.1"); // Using an "import form" tells TypeScript that this is a module that can be required by other scripts

var adapter = global.adapter;
var resolved = adapter.resolved;
Expand Down
24 changes: 8 additions & 16 deletions lib/tests/2.2.5.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ describe("2.2.5 `onFulfilled` and `onRejected` must be called as functions (i.e.
describe("strict mode", function () {
specify("fulfilled", function (done) {
resolved(dummy).then(
function onFulfilled(
/** @type {typeof globalThis} */
this
) {
/** @type { ( this: typeof globalThis) => void } */
function onFulfilled() {
"use strict";
assert.strictEqual(this, undefined);
done();
Expand All @@ -26,10 +24,8 @@ describe("2.2.5 `onFulfilled` and `onRejected` must be called as functions (i.e.
specify("rejected", function (done) {
rejected(dummy).then(
null,
function onRejected(
/** @type {typeof globalThis} */
this
) {
/** @type { ( this: typeof globalThis) => void } */
function onRejected() {
"use strict";
assert.strictEqual(this, undefined);
done();
Expand All @@ -41,10 +37,8 @@ describe("2.2.5 `onFulfilled` and `onRejected` must be called as functions (i.e.
describe("sloppy mode", function () {
specify("fulfilled", function (done) {
resolved(dummy).then(
function onFulfilled(
/** @type {typeof globalThis} */
this
) {
/** @type { ( this: typeof globalThis) => void } */
function onFulfilled() {
assert.strictEqual(this, global);
done();
}
Expand All @@ -54,10 +48,8 @@ describe("2.2.5 `onFulfilled` and `onRejected` must be called as functions (i.e.
specify("rejected", function (done) {
rejected(dummy).then(
null,
function onRejected(
/** @type {typeof globalThis} */
this
) {
/** @type { ( this: typeof globalThis) => void } */
function onRejected() {
assert.strictEqual(this, global);
done();
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"lib/"
],
"scripts": {
"test": "mocha",
"test": "mocha --reporter dot",
"regenerate": "rm lib/testFiles.js lib/*.d.ts lib/*/*.d.ts lib/*/*/*.d.ts scripts/*.d.ts test/*.d.ts; node ./scripts/generateTestFiles.js && tsc",
"update": "rm -rf package-lock.json node_modules; npm i && npm audit && npm run test && npm run regenerate"
"update": "rm -rf package-lock.json node_modules; npm i && npm audit && npm run regenerate && npm run test"
},
"dependencies": {
"mocha": "latest",
Expand Down
1 change: 1 addition & 0 deletions test/programmaticRunnerTest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
50 changes: 50 additions & 0 deletions test/programmaticRunnerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

let PromisesAPlusTests = require("../lib/programmaticRunner");


/** Test-compatible Wrapper for a pending standard-library Promise */
// /** @type { PromisesAPlusTests.Deferred<any> } */
class DeferredPromise {

/** Instead of passing functions to an executor callback, return them */
constructor() {
let __resolver;
let __rejecter;
this.promise = new Promise( (resolve,reject) => {
__resolver = resolve;
__rejecter = reject;
} );
this._resolver = __resolver;
this._rejecter = __rejecter;
}

/** Settles [[promise]] to fulfilled */
resolve( value ) { this._resolver( value ); }

/** Settles [[promise]] to rejected */
reject( reason ) { this._rejecter( reason ); }

}

/** Test-compatible Adapter for standard-library Promise */
// /** @type { PromisesAPlusTests.Adapter<any> } */
class PromiseAdapter {

constructor() { throw "Do not instantiate, use the class"; }

/** Returns a [[Promise]] which has already resolved */
static resolved(value ) { return new Promise( ( resolve,_reject) => { resolve(value ); } ); };

/** Returns a [[Promise]] which has already rejected */
/** @type { ( reason: any ) => Promise<never> } */
static rejected(reason) { return new Promise( (_resolve, reject) => { reject( reason); } ); };

/** Returns a [[DeferredPromise]] */
static deferred( ) { return new DeferredPromise; };

}

describe( "Test tests on standard-library Promise", () => {
PromisesAPlusTests.mocha( PromiseAdapter );
} );