Skip to content

Commit

Permalink
mocha functioning in browser, jest in client.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonswearingen committed Sep 25, 2020
1 parent b4c28cb commit 9c80a76
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 153 deletions.
8 changes: 8 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//rules for markdown lint: https://github.com/DavidAnson/vscode-markdownlint#configure
{
"default": true,
"MD013": { //line-length
"line_length": 200
},
"MD025": false
}
File renamed without changes.
2 changes: 1 addition & 1 deletion libraries/xlib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"main": "lib/_main.js",
"module": "lib-esm/_main.js",
"browser": "lib-browser/xlib-bundle.js",
"moduleTestEntry": "lib-esm/_internal/_test-main.js",
"webpackBrowserTestEntry": "lib-esm/_internal/_test-main.browser.js",
"typings": "lib/_main.d.ts",
"scripts": {
"build": "heft build",
Expand Down
10 changes: 9 additions & 1 deletion libraries/xlib/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions libraries/xlib/src/_internal/_global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable @typescript-eslint/ban-types */

declare type ANY = typeof any
declare function describe( name: string, fn: Function )
declare function it( name: string, fn?: ( cb: IDoneCallback ) => ANY, timeout?: number ): void;


interface IDoneCallback {
( ...args: ANY[] ): ANY;
//fail( error?: string | { message: string } ): ANY;
}
13 changes: 13 additions & 0 deletions libraries/xlib/src/_internal/_mock-test-scafolding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@






if ( globalThis.describe == null ) {
globalThis.describe = () => { }
}
if ( globalThis.it == null ) {
globalThis.it = () => { }
}

87 changes: 87 additions & 0 deletions libraries/xlib/src/_internal/_test-main.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable dot-notation */
//entrypoint into all tests.


/** see: https://webpack.js.org/guides/dependency-management/#requirecontext */
declare interface IRequireContextResolve {
resolve: ( key: string ) => string;
keys: () => string[];
id: string;

/** A context module exports a (require) function that takes one argument: the request. */
( path: string ): never
}

// eslint-disable-next-line @typescript-eslint/naming-convention
declare interface NodeRequire {
/** for running in a browser viw webpack + mocha-loader.
* @remarks
* see: https://github.com/webpack-contrib/mocha-loader and https://webpack.js.org/guides/dependency-management/#requirecontext
*/
context: ( directory: string, useSubdirectories?: boolean, regExp?: RegExp, mode?: string ) => IRequireContextResolve
}




//bootstrap mocha, as per: https://mochajs.org/#running-mocha-in-the-browser
//import expect from "expect"
//if(globalThis.expect ==null)
// if ( typeof ( expect ) === "undefined" ) {
// window[ "expect" ] = require( "chai" )
// }
const mocha = require( "mocha" )
//import "mocha"
mocha.setup( "bdd" )
mocha.growl() // enable web notification




//import * as chai from "chai-as-promised"

// if ( typeof ( expect ) === "undefined" ) {
// ( globalThis as unknown as any ).expect = require( "chai" )


// }

//sham mocha's it function to match jest's signature
{
const mochaIt: ANY = it
// // eslint-disable-next-line @typescript-eslint/ban-types
// const shamIt = ( name: string, fn?: Function, timeout?: number ): void => {
// // const doneCallback = () => {
// // const isDone: IDoneCallback = ( args ) => {
// // _test
// // }


// // }
// const _test = mochaIt( name, () => { fn() } //fn as never )
// if ( timeout != null ) {
// _test.timeout( timeout )
// }
// }
globalThis.it = ( name, fn, timeout ) => {
const _test = mochaIt( name, fn )
if ( timeout != null ) {
_test.timeout( timeout )
}
}
}

//load up all test files following general advice from: https://stackoverflow.com/questions/32385219/mocha-tests-dont-run-with-webpack-and-mocha-loader
{
const foundTestsRequire = ( require as unknown as NodeRequire ).context( "..", true, /.*\.test\.js?$/ )
//console.log( `keys found = ${ foundTestsRequire.keys().length }` )
for ( const key of foundTestsRequire.keys() ) {
console.log( key )
foundTestsRequire( key )
}
}

//run tests
mocha.run()

40 changes: 0 additions & 40 deletions libraries/xlib/src/_internal/_test-main.ts

This file was deleted.

72 changes: 53 additions & 19 deletions libraries/xlib/src/_main.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-restricted-globals */
// import * as xlib from "./_main"
import * as xlib from "./_main"

// import chai from "chai"
// import chaiAsPromised from "chai-as-promised"
// chai.use( chaiAsPromised )
// import { expect } from "chai"
//import { expect } from "chai"



Expand All @@ -22,29 +22,63 @@

// // } )

// import threads = xlib._imports.threads
// import * as testWorker from "./_internal/_test-worker"
import threads = xlib._imports.threads
import * as testWorker from "./_internal/_test-worker"
const testWorkerSpawn = new threads.Worker( "./_internal/_test-worker" )

// describe( "threads import", () => {
let jobsDone = false
const counterPromise = threads.spawn<testWorker.Counter>( testWorkerSpawn ).finally( () => { jobsDone = true } )

// it( "threads basic e2e", async () => {
describe( "threads import", () => {


// const counter = await threads.spawn<testWorker.Counter>( new threads.Worker( "./_internal/_test-worker" ) )
// const initialCount = await counter.getCount()
// expect( initialCount ).equals( 0 )
// await counter.increment()
// const update1Count = await counter.getCount()
// expect( update1Count ).equals( 1 )
// void counter.increment()
// const update2Count = await counter.getCount()
// expect( update2Count ).equals( 2 )
// await threads.Thread.terminate( counter )

// console.log( `threads! noice! ${ JSON.stringify( { initialCount, update1Count, update2Count } ) }` )
it( "threads basic e2e", async () => {

// } )
// } )

return new Promise( ( resolve, reject ) => {

setInterval( () => {
console.log( `is jobsDone? ${ jobsDone }` )
if ( jobsDone === true ) {
resolve()
}
}, 100 )
// return await counterPromise
} )

// async function getCounter() {
// const counter = await counterPromise
// console.warn( "SHMERE" )
// return counter
// }

// //Promise.any( [async () => { }])

// // const counter = await new Promise( ( resolve, reject ) => {

// // //setTimeout( () => { resolve() }, 100 )
// // return await counterPromise

// // } )
// //const counter = ( await Promise.all( [ counterPromise ] ) )[ 0 ]
// const counter = await getCounter()
// //const counter = await async()=> { return await counterPromise }
// const initialCount = await counter.getCount()
// expect( initialCount ).equals( 0 )
// await counter.increment()
// const update1Count = await counter.getCount()
// expect( update1Count ).equals( 1 )
// void counter.increment()
// const update2Count = await counter.getCount()
// expect( update2Count ).equals( 2 )
// await threads.Thread.terminate( counter )

// console.log( `threads! noice! TEST ${ JSON.stringify( { initialCount, update1Count, update2Count } ) }` )


} )
} )

describe( "Test", () => {
it( "should succeed", ( done ) => {
Expand Down
38 changes: 19 additions & 19 deletions libraries/xlib/src/_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* isomorphic swiss army knife
*/

console.trace( "xlib entrypoint (_main)" )
console.warn( "xlib entrypoint (_main)" )


import * as _imports from "./_imports"
Expand All @@ -20,28 +20,28 @@ export { _imports, net, util, lolo }



//import threads = _imports.threads
import * as testWorker from "./_internal/_test-worker"
// //import threads = _imports.threads
// import * as testWorker from "./_internal/_test-worker"

import { spawn, Thread, Worker } from "threads"
// import { spawn, Thread, Worker } from "threads"



async function asyncHuh(): Promise<void> {
const counter = await spawn<testWorker.Counter>( new Worker( "./_internal/_test-worker" ) )
const initialCount = await counter.getCount()
//expect( initialCount ).toEqual( 0 )
await counter.increment()
const update1Count = await counter.getCount()
//expect( update1Count ).toEqual( 1 )
void counter.increment()
const update2Count = await counter.getCount()
//expect( update2Count ).toEqual( 2 )
await Thread.terminate( counter )
// async function asyncHuh(): Promise<void> {
// const counter = await spawn<testWorker.Counter>( new Worker( "./_internal/_test-worker" ) )
// const initialCount = await counter.getCount()
// //expect( initialCount ).toEqual( 0 )
// await counter.increment()
// const update1Count = await counter.getCount()
// //expect( update1Count ).toEqual( 1 )
// void counter.increment()
// const update2Count = await counter.getCount()
// //expect( update2Count ).toEqual( 2 )
// await Thread.terminate( counter )

console.log( `threads! noice!!!?!! ${ JSON.stringify( { initialCount, update1Count, update2Count } ) }` )
// console.log( `threads! noice!!!?!! ${ JSON.stringify( { initialCount, update1Count, update2Count } ) }` )

}
// }

void asyncHuh()
console.log( "called asyncHuh!!" )
// void asyncHuh()
// console.log( "called asyncHuh!!" )
Loading

0 comments on commit 9c80a76

Please sign in to comment.