bigint-crypto-utils/build/testing/mocha/mocha-init.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-03-25 12:40:04 +00:00
'use strict'
const path = require('path')
const chai = require('chai')
const rimraf = require('rimraf')
const RollupBuilder = require('./builders/RollupBuilder.js')
const TestsBuilder = require('./builders/TestsBuilder.js')
2022-04-20 21:55:44 +00:00
require('dotenv').config()
2021-03-25 12:40:04 +00:00
const rootDir = path.join(__dirname, '../../../')
global.chai = chai
2022-04-20 21:55:44 +00:00
loadPkgToGlobal()
global.IS_BROWSER = false
2021-03-25 12:40:04 +00:00
const watch = process.argv.includes('--watch') || process.argv.includes('-w')
const tempDir = path.join(rootDir, '.mocha-ts')
const rollupBuilder = new RollupBuilder({ name: 'rollup', configPath: path.join(rootDir, 'build/rollup.config.js'), tempDir, watch })
const testBuilder = new TestsBuilder({ name: 'tsc', tempDir })
rollupBuilder.start() // This should be in exports.mochaGlobalSetup but mocha fails when not in watch mode (DIRT...)
testBuilder.start() // This should be in exports.mochaGlobalSetup but mocha fails when not in watch mode (DIRT...)
exports.mochaHooks = {
beforeAll: [
async function () {
2021-08-06 08:10:32 +00:00
this.timeout('120000')
await Promise.all([rollupBuilder.ready(), testBuilder.ready()])
2021-03-25 12:40:04 +00:00
// 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)]
2022-04-20 21:55:44 +00:00
loadPkgToGlobal()
2021-08-06 08:10:32 +00:00
// And now reset any other transpiled module (just delete the cache so it is fully reloaded)
for (const key in require.cache) {
const relativePath = path.relative(rootDir, key)
if (relativePath.startsWith(`.mocha-ts${path.sep}`)) {
delete require.cache[key]
}
}
2021-03-25 12:40:04 +00:00
}
]
}
// exports.mochaGlobalSetup = async function () {
// await rollupBuilder.start()
// await testBuilder.start()
// }
exports.mochaGlobalTeardown = async function () {
await testBuilder.close()
await rollupBuilder.close()
// I use the sync version of rimraf precisely because it blocks the
// main thread and thus the mocha watcher, which otherwise would complain
// about files being deleted
rimraf.sync(tempDir, { disableGlob: true })
}
2022-04-20 21:55:44 +00:00
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
}
}