updated deps

This commit is contained in:
Juanra Dikal 2022-04-20 23:55:44 +02:00
parent 9f9a367d72
commit 66b761d23f
12 changed files with 1806 additions and 902 deletions

View File

@ -1,11 +1,25 @@
const puppeteer = require('puppeteer')
const path = require('path')
const browserTests = async ({ logWarnings = false, serverPort = 38000, keepServerRunning = false, puppeteerOptions = {} }) => {
const puppeteer = require('puppeteer')
const minimatch = require('minimatch')
const glob = require('glob')
const rootDir = path.join(__dirname, '../../..')
const browserTests = async (
{
logWarnings = false,
serverPort = 38000,
keepServerRunning = false,
puppeteerOptions = {
headless: false,
devtools: true
}
}, testFiles) => {
const server = require('./server.js').server
await server.init()
await server.init(testFiles)
await server.listen(serverPort)
const browser = await puppeteer.launch(puppeteerOptions)
const page = await browser.newPage()
const page = (await browser.pages())[0]
page.on('console', function (message) {
let ignore = message.type() === 'warning' && !logWarnings
if (message.type() === 'error' && message.location()) {
@ -39,28 +53,69 @@ const browserTests = async ({ logWarnings = false, serverPort = 38000, keepServe
}
console[consoleType](text, ...args)
})
page.on('error', function (err) { page.emit(new Error(err)) })
await page.goto('http://localhost:38000/')
const watchDog = page.waitForFunction('_mocha.state === \'stopped\'', { timeout: 0 })
await watchDog
page.on('close', async () => {
await close()
})
if (keepServerRunning === false) {
await page.close()
await browser.close()
await server.close()
page.goto('http://localhost:38000/').then(async () => {
const watchDog = page.waitForFunction('_mocha.state === \'stopped\'', { timeout: 0 })
await watchDog.catch(async (reason) => {
console.error(reason)
})
if (puppeteerOptions.headless === true) {
await close()
}
}).catch(async (reason) => {
console.error(reason)
})
async function close () {
console.log('Closing browser tests...')
await browser.close().catch(() => {})
if (keepServerRunning !== true) {
await server.close().catch(() => {})
}
}
}
function processedTestFiles (testFilesStr) {
if (testFilesStr === undefined) {
return undefined
}
// Let us first remove surrounding quotes in string (it gives issues in windows)
testFilesStr = testFilesStr.replace(/^['"]/, '').replace(/['"]$/, '')
const filenames = glob.sync(testFilesStr, { cwd: rootDir, matchBase: true })
if (filenames.length > 0) {
filenames.forEach(file => {
const isTsTestFile = minimatch(file, '{test/**/*.ts,src/**/*.spec.ts}', { matchBase: true })
if (!isTsTestFile) {
throw new Error(`test file '${file}' not found`)
}
})
}
return filenames
}
const opts = {
// puppeteer options
puppeteerOptions: {
headless: true
headless: false,
devtools: true
// slowMo: 100,
// timeout: 10000
},
doNotLogWarnings: true,
logWarnings: false, // log warnings in Node console (usually not needed)
keepServerRunning: false, // keep server running until manually closed with ctrl-c. In combination with puppeteerOptions.headless (or just connecting any browser to the test page) allows debugging in browser
serverPort: 38000
}
browserTests(opts)
const args = process.argv.slice(2)
if (args[0] === 'headless') {
opts.puppeteerOptions.headless = true
args.shift()
}
browserTests(opts, processedTestFiles(args[0]))

View File

@ -4,13 +4,15 @@ const fs = require('fs')
const http = require('http')
const path = require('path')
const pkgJson = require('../../../package.json')
require('dotenv').config()
const rollup = require('rollup')
const resolve = require('@rollup/plugin-node-resolve').nodeResolve
const replace = require('@rollup/plugin-replace')
const multi = require('@rollup/plugin-multi-entry')
const typescript = require('@rollup/plugin-typescript')
const typescriptPlugin = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs')
const json = require('@rollup/plugin-json')
const rootDir = path.join(__dirname, '..', '..', '..')
@ -41,34 +43,48 @@ const indexHtml = `<!DOCTYPE html>
<script type="module">
import * as _pkg from './${name}.esm.js'
self._pkg = _pkg
</script>
<script type="module">
import './tests.js'
window._mocha = mocha.run()
</script>
</html>`
async function buildTests () {
const tsBundleOptions = {
tsconfig: path.join(rootDir, 'tsconfig.json'),
outDir: undefined // ignore outDir in tsconfig.json
}
async function buildTests (testFiles) {
// create a bundle
const input = testFiles ?? [path.join(rootDir, pkgJson.directories.test, '**/*.ts'), path.join(rootDir, pkgJson.directories.src, '**/*.spec.ts')]
const inputOptions = {
input: [path.join(rootDir, pkgJson.directories.test, '**/*.ts'), path.join(rootDir, pkgJson.directories.src, '**/*.spec.ts')],
input,
plugins: [
multi({ exports: true }),
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescript(),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
exportConditions: ['browser', 'module', 'import', 'default']
}),
commonjs()
commonjs(),
json()
],
external: [pkgJson.name]
}
const bundle = await rollup.rollup(inputOptions)
const { output } = await bundle.generate({ format: 'esm' })
await bundle.close()
return output[0].code
let bundledCode = output[0].code
const replacements = _getEnvVarsReplacements(bundledCode)
for (const replacement in replacements) {
bundledCode = bundledCode.replaceAll(replacement, replacements[replacement])
}
return bundledCode
}
class TestServer {
@ -76,8 +92,8 @@ class TestServer {
this.server = http.createServer()
}
async init () {
const tests = await buildTests()
async init (testFiles) {
const tests = await buildTests(testFiles)
this.server.on('request', function (req, res) {
if (req.url === `/${name}.esm.js`) {
fs.readFile(path.join(rootDir, pkgJson.directories.dist, 'bundles/esm.js'), function (err, data) {
@ -139,4 +155,29 @@ class TestServer {
}
}
function _getEnvVarsReplacements (testsCode) {
const replacements = {}
const missingEnvVars = []
for (const match of testsCode.matchAll(/process\.env\.(\w+)/g)) {
const envVar = match[1]
if (process.env[envVar] !== undefined) {
replacements[match[0]] = '`' + process.env[envVar] + '`'
} else {
missingEnvVars.push(envVar)
}
}
for (const match of testsCode.matchAll(/process\.env\[['"](\w+)['"]\]/g)) {
const envVar = match[1]
if (process.env[envVar] !== undefined) {
replacements[match[0]] = '`' + process.env[envVar] + '`'
} else {
missingEnvVars.push(envVar)
}
}
if (missingEnvVars.length > 0) {
throw EvalError('The folloinwg environment variables are missing in your .env file: ' + missingEnvVars)
}
return replacements
}
exports.server = new TestServer()

View File

@ -3,7 +3,7 @@ const fs = require('fs')
const path = require('path')
const rollup = require('rollup')
const loadAndParseConfigFile = require('rollup/dist/loadConfigFile')
const loadAndParseConfigFile = require('rollup/dist/loadConfigFile.js')
const Builder = require('./Builder.js')
@ -28,6 +28,7 @@ module.exports = class RollupBuilder extends Builder {
: bundle.output[0].file
return file === path.join(rootDir, pkgJson.main)
})[0]
delete rollupOptions.output.pop() // remove the second output
this.builder = new RollupBundler(rollupOptions, this.watch)

View File

@ -24,17 +24,36 @@ module.exports = class TestsBuilder extends Builder {
this.tempDir = tempDir
const readFileAndMangle = (path) => { // We need to change the include or file in the original file to only compile the tests
const fileStr = fs.readFileSync(path, 'utf8')
const config = JSON5.parse(fileStr)
if (config.file) delete config.file
config.include.push('node_modules/**/*.d.ts')
config.compilerOptions.module = 'commonjs'
return JSON.stringify(config)
}
const configFile = ts.readJsonConfigFile(configPath, readFileAndMangle)
const tsConfig = JSON5.parse(fs.readFileSync(configPath, 'utf8'))
const parsedTsConfig = ts.parseJsonSourceFileConfigFileContent(configFile, ts.sys, path.dirname(configPath))
tsConfig.file = undefined
// Exclude already transpiled files in src
tsConfig.exclude = ['src/ts/**/!(*.spec).ts']
// "noResolve": true
tsConfig.compilerOptions.noResolve = false
// we don't need declaration files
tsConfig.compilerOptions.declaration = false
// we need to emit files
tsConfig.compilerOptions.noEmit = false
// source mapping eases debuging
tsConfig.compilerOptions.sourceMap = true
// This prevents SyntaxError: Cannot use import statement outside a module
tsConfig.compilerOptions.module = 'commonjs'
// Removed typeroots (it causes issues)
tsConfig.compilerOptions.typeRoots = undefined
tsConfig.compilerOptions.outDir = path.isAbsolute(tempDir) ? path.relative(rootDir, tempDir) : tempDir
this.tempTsConfigPath = path.join(rootDir, '.tsconfig.json')
fs.writeFileSync(this.tempTsConfigPath, JSON.stringify(tsConfig, undefined, 2))
const createProgram = ts.createSemanticDiagnosticsBuilderProgram
@ -64,15 +83,8 @@ module.exports = class TestsBuilder extends Builder {
// Note that there is another overload for `createWatchCompilerHost` that takes
// a set of root files.
this.host = ts.createWatchCompilerHost(
parsedTsConfig.fileNames,
{
...parsedTsConfig.options,
rootDir,
outDir: this.tempDir,
noEmit: false,
noResolve: true,
sourceMap: true
},
this.tempTsConfigPath,
{},
ts.sys,
createProgram,
reportDiagnostic,
@ -91,5 +103,6 @@ module.exports = class TestsBuilder extends Builder {
async close () {
await super.close()
this.watcher.close()
fs.unlinkSync(this.tempTsConfigPath)
}
}

View File

@ -8,9 +8,14 @@ const rimraf = require('rimraf')
const RollupBuilder = require('./builders/RollupBuilder.js')
const TestsBuilder = require('./builders/TestsBuilder.js')
require('dotenv').config()
const rootDir = path.join(__dirname, '../../../')
global.chai = chai
loadPkgToGlobal()
global.IS_BROWSER = false
const watch = process.argv.includes('--watch') || process.argv.includes('-w')
@ -31,7 +36,7 @@ exports.mochaHooks = {
// Just in case our module had been modified. Reload it when the tests are repeated (for mocha watch mode).
delete require.cache[require.resolve(rootDir)]
global._pkg = require(rootDir)
loadPkgToGlobal()
// And now reset any other transpiled module (just delete the cache so it is fully reloaded)
for (const key in require.cache) {
@ -58,3 +63,14 @@ exports.mochaGlobalTeardown = async function () {
// about files being deleted
rimraf.sync(tempDir, { disableGlob: true })
}
function loadPkgToGlobal () {
const _pkg = require(rootDir)
if (typeof _pkg === 'function') { // If it is just a default export, load it as named (for compatibility)
global._pkg = {
default: _pkg
}
} else {
global._pkg = _pkg
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -153,7 +153,7 @@ A promise that resolves to a boolean that is either true (a probably prime numbe
#### Defined in
[src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/isProbablyPrime.ts#L21)
[src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/isProbablyPrime.ts#L21)
___
@ -317,7 +317,7 @@ A promise that resolves to a bigint probable prime of bitLength bits.
#### Defined in
[src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/prime.ts#L21)
[src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/prime.ts#L21)
___
@ -346,7 +346,7 @@ A bigint probable prime of bitLength bits.
#### Defined in
[src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/prime.ts#L100)
[src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/prime.ts#L100)
___
@ -374,7 +374,7 @@ A cryptographically secure random bigint between [min,max]
#### Defined in
[src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/randBetween.ts#L15)
[src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/randBetween.ts#L15)
___
@ -402,7 +402,7 @@ A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in
[src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/randBits.ts#L14)
[src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/randBits.ts#L14)
___
@ -430,7 +430,7 @@ A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in
[src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/randBits.ts#L45)
[src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/randBits.ts#L45)
___
@ -458,7 +458,7 @@ A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cry
#### Defined in
[src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/randBytes.ts#L12)
[src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/randBytes.ts#L12)
___
@ -486,7 +486,7 @@ A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure rando
#### Defined in
[src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/f7ac709/src/ts/randBytes.ts#L46)
[src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/9f9a367/src/ts/randBytes.ts#L46)
___

2438
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -49,20 +49,24 @@
"test": "./test"
},
"scripts": {
"build": "run-s lint build:js docs test:browser coverage",
"build": "run-s lint build:js docs",
"build:js": "rollup -c build/rollup.config.js",
"clean": "rimraf .nyc_output .mocha-ts coverage dist docs",
"coverage": "nyc --check-coverage --exclude build --exclude '{src/ts/**/*.spec.ts,test/**/*.ts}' --reporter=text --reporter=lcov node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"clean": "rimraf .nyc_output .mocha-ts coverage dist types docs",
"coverage": "nyc --check-coverage --exclude build --exclude '{src/ts/**/*.spec.ts,test/**/*.ts,.mocha-ts/**/*}' --reporter=text --reporter=lcov node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"docs": "node build/build.docs.js",
"git:add": "git add -A",
"lint": "ts-standard --fix",
"mocha": "node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js ",
"version": "run-s build git:add",
"mocha-ts": "node ./build/bin/mocha-ts.js --require build/testing/mocha/mocha-init.js ",
"mocha-ts:browser": "node build/testing/browser/index.js ",
"mocha-ts:browser-headless": "node build/testing/browser/index.js headless ",
"preversion": "run-s clean lint build:js coverage test:browser-headless",
"version": "run-s docs git:add",
"postversion": "git push --follow-tags",
"test": "run-s test:browser test:node",
"test:browser": "node build/testing/browser/index.js",
"test:node": "npm run mocha -- '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"watch": "npm run mocha -- --watch '{src/ts/**/*.spec.ts,test/**/*.ts}'"
"test": "run-s test:browser-headless test:node",
"test:browser": "npm run mocha-ts:browser ",
"test:browser-headless": "npm run mocha-ts:browser-headless ",
"test:node": "npm run mocha-ts -- '{src/ts/**/*.spec.ts,test/**/*.ts}'",
"watch": "npm run mocha-ts:node -- --watch '{src/ts/**/*.spec.ts,test/**/*.ts}'"
},
"ts-standard": {
"env": [
@ -88,26 +92,28 @@
},
"devDependencies": {
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-multi-entry": "^4.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^8.2.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^9.0.0",
"chai": "^4.3.3",
"dotenv": "^16.0.0",
"fs-extra": "^10.0.0",
"glob": "^7.1.6",
"glob": "^8.0.1",
"json5": "^2.2.0",
"minimatch": "^3.0.4",
"minimatch": "^5.0.1",
"mocha": "^9.0.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"pirates": "^4.0.1",
"puppeteer": "^10.1.0",
"puppeteer": "^13.6.0",
"rimraf": "^3.0.2",
"rollup": "^2.40.0",
"rollup-plugin-terser": "^7.0.2",
"ts-standard": "^10.0.0",
"ts-standard": "^11.0.0",
"tslib": "^2.1.0",
"typedoc": "^0.22.0",
"typedoc-plugin-markdown": "^3.6.0",