2021-03-24 13:04:30 +00:00
|
|
|
'use strict'
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
const fs = require('fs')
|
2021-03-24 13:04:30 +00:00
|
|
|
const path = require('path')
|
|
|
|
const chai = require('chai')
|
|
|
|
const rimraf = require('rimraf')
|
2022-08-01 02:04:00 +00:00
|
|
|
require('dotenv').config()
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
const rollupBuilder = require('./builders/RollupBuilder.cjs').rollupBuilder
|
|
|
|
const testsBuilder = require('./builders/TestsBuilder.cjs').testBuilder
|
|
|
|
|
2021-03-24 13:04:30 +00:00
|
|
|
const rootDir = path.join(__dirname, '../../../')
|
2022-10-03 15:35:35 +00:00
|
|
|
const pkgJson = require(path.join(rootDir, 'package.json'))
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
|
|
|
|
const tempDir = path.join(rootDir, mochaTsRelativeDir)
|
2022-01-17 10:04:55 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
global.chai = chai
|
2022-01-17 10:04:55 +00:00
|
|
|
global.IS_BROWSER = false
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
const watch = process.argv.includes('--watch') || process.argv.includes('-w')
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
const setup = JSON.parse(fs.readFileSync(path.join(tempDir, 'testSetup.json'), 'utf-8'))
|
|
|
|
|
|
|
|
const testFiles = setup.testFiles
|
|
|
|
let commonjs = setup.commonjs
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
commonjs = watch ? true : commonjs // mocha in watch mode only supports commonjs
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2023-04-11 08:43:47 +00:00
|
|
|
global._MODULE_TYPE = commonjs ? 'CJS' : 'ESM'
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
exports.mochaGlobalSetup = async function () {
|
|
|
|
if (watch) {
|
|
|
|
await rollupBuilder.start({ commonjs, watch })
|
|
|
|
testsBuilder.start({ testFiles, commonjs })
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
exports.mochaHooks = {
|
|
|
|
beforeAll: [
|
|
|
|
async function () {
|
2021-08-06 22:14:30 +00:00
|
|
|
this.timeout('120000')
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
if (watch) {
|
|
|
|
await Promise.all([rollupBuilder.ready(), testsBuilder.ready()])
|
2021-08-06 22:14:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
// reset any 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}`) || relativePath.startsWith(`dist${path.sep}`)) {
|
|
|
|
delete require.cache[key]
|
|
|
|
}
|
2021-08-06 22:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.mochaGlobalTeardown = async function () {
|
2022-10-03 15:35:35 +00:00
|
|
|
if (watch) {
|
|
|
|
await testsBuilder.close()
|
|
|
|
await rollupBuilder.close()
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
// 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 })
|
|
|
|
}
|